Triggers 无论我做什么,salesforce触发器代码覆盖率都保持在35%

Triggers 无论我做什么,salesforce触发器代码覆盖率都保持在35%,triggers,salesforce,code-coverage,Triggers,Salesforce,Code Coverage,您好,我是saleforce apex codiing的新手,我已经编写了一个触发器,它在lead conversion Tras上向客户和联系人发送自定义对象。(交易/报价)。我已经为它编写了触发器和测试代码,但是我被困在35%的代码覆盖率上,你能指出我哪里出了问题吗 这是我写的触发器 trigger TransferDeals on Lead (after update) { Map<Id, Lead> leadMap = new Map<Id,Lead>(); Le

您好,我是saleforce apex codiing的新手,我已经编写了一个触发器,它在lead conversion Tras上向客户和联系人发送自定义对象。(交易/报价)。我已经为它编写了触发器和测试代码,但是我被困在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
// }
触发Lead上的TransferDeals(更新后){
Map leadMap=新映射();
主要父母;
for(整数i=0;i0){
设置leadId=leadMap.keySet();
列出所有子项=
[从交易报价中选择Id、账户、联系人、潜在客户,其中潜在客户:潜在客户Id];
系统调试(所有子系统);
针对(交易/报价/儿童:所有儿童){
if(leadMap.containsKey(child.Lead_uc)){
//查找父潜在客户
parent=leadMap.get(child.Lead\uu\c);
//更新子对象上的字段
child.account\uuu c=parent.convertedaccounted;
child.Contact\uu c=parent.ConvertedContactId;
}
}
系统调试(所有子系统);
//试一试{
更新所有儿童;
//}catch(异常e){
//可以在这里放置一些错误通知
//否则它会默默地失败
// }
} }

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

@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);
    }

}
@isTest
私有类Leadtriggertest{
静态测试方法void verifyAccount(){
//执行我们的数据准备。
列表线索=新列表{};
列表交易=新列表{};
对于(整数i=0;i<200;i++){
Lead a=新Lead(LastName='TestLead'+i,Company='TesCompany'+i,IsConverted=false);
交易(Offer)交易(b)新交易(Offer)交易(c)(Name='TestDeal'+i,Lead(c)交易(c)交易;
增加(a);
加入(b);
}
插入导线;
插入交易;
Map leadMap=新映射();
设置leadId=leadMap.keySet();
List allChildren=[选择Id,LastName,从Lead转换,其中Id位于:LeadID];
for(主要子项:所有子项){
if(child.IsConverted==false){
child.Isconverted=true;
}
}
//启动测试,这会将调控器限制上下文更改为
//是指触发而不是测试。
test.startTest();
//插入导致触发器执行的帐户记录。
更新线索;
//停止测试,这会将限制上下文从触发器更改回测试。
test.stopTest();
列表testdealsupdate=
[从交易报价中选择Id、账户、联系人、潜在客户,其中潜在客户:潜在客户Id];
列表testagreementupdate=
[从echosign\u dev1\u签名协议中选择Id、echosign\u dev1\u帐户、echosign\u dev1\u接收人\u领导\u领导,其中echosign\u dev1\u接收人\u领导:领导Id];
列表引导签出=
[从Lead中选择Id、LastName、ConvertedAccountId、ConvertedContactId,其中Id位于:LeadId];
Map leadmap2=新地图();
用于(Lead c:Lead Checkout){
leadmap2.put(c.Id,c);
}
对于(交易报价:testdealsupdate){
铅b=铅map2.get(a.铅c);
系统资产质量(a.账户,b.转换账户);
系统资产质量(a.Contact\uu\c,b.ConvertedContactId);
}
用于(echosign\u dev1\u\u签署协议\u\u c a:测试协议更新){
Lead b=leadMap2.get(a.echosign\u dev1\u接收者\u Lead\u c);
系统资产质量(a.echosign\u dev1\u Account\u c,b.ConvertedAccounted);
}
}
}


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

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

是否可以通过在if语句中添加system.debug语句进行验证? 此外,请尝试打印地图中的所有潜在客户id,以及您在
leadMap.containsKey(child.lead\u c)
行中使用的相应
child.lead\u c


Anup

不要编写代码来提高代码覆盖率;编写提供价值的测试,即您希望代码正确/正确地执行什么?编写这些测试,然后看看覆盖率如何提高。任何未涉及的代码都要问自己“它做了什么?”,“它的目的是什么?”并编写测试来实现目的而不是实现。我同意@Shaun Wilde。尝试并列出将在触发器中执行不同分支的不同场景。将明确尝试此方法..thanx用于快速回复…anup,Shaun…触发器在沙箱中完美工作。。。我必须在测试类中测试超前转换操作,我认为我写的测试逻辑是错误的。请指导如何测试此类场景,最好是批量触发测试。非常感谢。原因当代码崩溃时,我无法在测试触发器中更新时将isConverted设置为true。
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;
}