0

こんにちは、saleforce apex のコーディングは初めてで、リード コンバージョン時にアカウントと連絡先の両方にカスタム オブジェクトを転送するトリガーを作成しました (取引/オファー)。トリガーとそのテスト コードを作成しましたが、コード カバレッジが 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、行で使用している対応する値を印刷してみてくださいleadMap.containsKey (child.Lead__c)

Anup

于 2012-08-02T07:04:24.663 に答える