0

嗨,我是 saleforce 顶点编码的新手,我编写了一个触发器,它在潜在客户转换时将自定义对象传输到帐户和联系人。(交易/报价)。我已经为它编写了触发器和测试代码,但我被困在 35% 的代码覆盖率上,你能指出我哪里出错了吗

这是我写的触发器

trigger TransferDeals on Lead (after update) {
Map<Id, Lead> leadMap = new Map<Id,Lead>();
Lead parent;

for (Integer i = 0; i < Trigger.new.size(); i++){
if (Trigger.new[i].IsConverted == true && Trigger.old[i].isConverted == false) {
  leadMap.put( Trigger.new[i].Id, Trigger.new[i]);
}
}

if( leadMap.size() > 0 ) {
  Set<Id> leadIds = leadMap.keySet();
  List<Deal_Offer__c> allChildren = 
    [select Id, Account__c, Contact__c, Lead__c from Deal_Offer__c where lead__c in :leadIds];

System.debug(allChildren);

  for ( Deal_Offer__c child : allChildren ) {
    if ( leadMap.containsKey( child.Lead__c ) ) {
       // lookup the parent lead
       parent = leadMap.get( child.Lead__c );
       // update the fields on the child object
       child.account__c = parent.ConvertedAccountId;
       child.Contact__c = parent.ConvertedContactId;
    }
  }

System.debug(allChildren);
//try {
update allChildren; 
// } catch( Exception e ) {
      // could put something here to notify on error
     // otherwise it fails silently
// }

} }

这是我为验证代码而编写的测试类

@isTest
private class Leadtriggertest {
static testMethod void verifyAccount(){
    // Perform our data preparation.
    List<Lead> leads = new List<Lead>{};
    List<Deal_Offer__c> deals = new  List<Deal_Offer__c>{};  
    for(Integer i = 0; i < 200; i++){
        Lead a = new Lead(LastName = 'TestLead ' + i,Company = 'TesCompany' + i,IsConverted=false);
        Deal_Offer__c b = new Deal_Offer__c(Name = 'TestDeal' + i,Lead__c = a.Id);
        leads.add(a);
        deals.add(b);
    }
    insert leads;
    insert deals;

    Map<Id, Lead> leadMap = new Map<Id,Lead>();
    Set<Id> leadIds = leadMap.keySet();
    List<Lead> allChildren = [select Id,LastName,IsConverted from Lead where Id in :leadIds];
        for ( Lead child : allChildren ) {
            if(child.IsConverted==false){
                child.Isconverted = true;
            }
        }

    // Start the test, this changes governor limit context to 
    // that of trigger rather than test. 
    test.startTest();

    // Insert the Account records that cause the trigger to execute.
    update Leads; 

    // Stop the test, this changes limit context back to test from trigger.
    test.stopTest();

    List<Deal_Offer__c> testdealsupdate = 
                              [select Id, Account__c, Contact__c, Lead__c from Deal_Offer__c where lead__c in :leadIds];
    List<echosign_dev1__SIGN_Agreement__c> testagreementupdate = 
                            [select Id, echosign_dev1__Account__c, echosign_dev1__Recipient_Lead__c from echosign_dev1__SIGN_Agreement__c where echosign_dev1__Recipient_Lead__c in :leadIds];
    List<Lead> leadcheckout =
                             [select Id,LastName,ConvertedAccountId,ConvertedContactId from Lead where Id in :leadIds];
    Map<Id,Lead> leadmap2= new Map<Id,Lead>();

    for(Lead c: leadcheckout){
        leadmap2.put( c.Id, c);
    }
    for(Deal_offer__c a : testdealsupdate){
        Lead b = leadMap2.get(a.Lead__c);
        System.assertEquals(a.Account__c,b.ConvertedAccountId);
        System.assertEquals(a.Contact__c,b.ConvertedContactId);
    }
    for(echosign_dev1__SIGN_Agreement__c a : testagreementupdate){
        Lead b = leadMap2.get(a.echosign_dev1__Recipient_Lead__c);
        System.assertEquals(a.echosign_dev1__Account__c ,b.ConvertedAccountId);
    }

}

}

请帮我弄清楚如何增加代码覆盖率提前谢谢你

4

1 回答 1

0

我相信这是因为这部分代码没有被执行(或者更确切地说是那里的 if 条件)。

for ( Deal_Offer__c child : allChildren ) {
if ( leadMap.containsKey( child.Lead__c ) ) {
   // lookup the parent lead
   parent = leadMap.get( child.Lead__c );
   // update the fields on the child object
   child.account__c = parent.ConvertedAccountId;
   child.Contact__c = parent.ConvertedContactId;
}

您可以通过在 if 语句中添加 system.debug 语句来验证吗?此外,尝试打印出地图中的所有潜在客户 ID,以及child.Lead__c您在 line 中使用的相应值leadMap.containsKey (child.Lead__c)

阿努普

于 2012-08-02T07:04:24.663 回答