Sunday 11 September 2016

Apex - Usefull trigger to maintain the relationship between custom objects related to Lead to Account/Contact/Opp after Lead Conversion


trigger LeadConvertCustomObjects on Lead (before update) {
//This trigger will associate Custom Object records with the Account, Contact, and/or      opportunity associated to the
//lead after it has been converted.
//The Custom Object is associated to an opportunity only if an opportunity record exist on the Lead.
for (Integer i = 0; i < Trigger.new.size(); i++){
    if (Trigger.new[i].IsConverted == true && Trigger.old[i].isConverted == false){
        Set<Id> leadIds = new Set<Id>();
        for (Lead lead : Trigger.new)
            leadIds.add(lead.Id);

        Map<Id, DocuSign_Account__c> entries_DSA = new Map<Id, DocuSign_Account__c>([select Account__c, Lead__c from DocuSign_Account__c where lead__c in :leadIds]);      
        if(!Trigger.new.isEmpty()) {
            for (Lead lead : Trigger.new)  {
                for (DocuSign_Account__c DSA : entries_DSA.values()) {
                    if (DSA.Lead__c == lead.Id) {
                        // DSA.contact__c = lead.ConvertedContactId;
                        // DSA.opportunity__c = lead.ConvertedOpportunityId;
                        DSA.account__c = lead.ConvertedAccountId;
                        update DSA;
                        // break;
                    }
                }
            }
        }

        Map<Id, DocuSign_Account_Member__c> entries_DSAM = new Map<Id, DocuSign_Account_Member__c>([select Contact__c, Lead__c from DocuSign_Account_Member__c where lead__c in :leadIds]);      
        if(!Trigger.new.isEmpty()) {
            for (Lead lead : Trigger.new)  {
                for (DocuSign_Account_Member__c DSAM : entries_DSAM.values()) {
                    if (DSAM.Lead__c == lead.Id) {
                        DSAM.Contact__c = lead.ConvertedContactId;
                        // DSAM.opportunity__c = lead.ConvertedOpportunityId;
                        // DSAM.account__c = lead.ConvertedAccountId;
                        update DSAM;
                        // break;
                    }
                }
            }
        }

        Map<Id, Use_Case__c> entries_UC = new Map<Id, Use_Case__c>([select Account__c, Lead__c from Use_Case__c where lead__c in :leadIds]);      
        if(!Trigger.new.isEmpty()) {
            for (Lead lead : Trigger.new)  {
                for (Use_Case__c UC : entries_UC.values()) {
                    if (UC.Lead__c == lead.Id) {
                        // UC.contact__c = lead.ConvertedContactId;
                        // UC.opportunity__c = lead.ConvertedOpportunityId;
                        UC.account__c = lead.ConvertedAccountId;
                        update UC;
                        // break;
                    }
                }
            }
        }

        Map<Id, Ecosystem__c> entries_ECO = new Map<Id, Ecosystem__c>([select Account__c, Lead__c from Ecosystem__c where lead__c in :leadIds]);      
        if(!Trigger.new.isEmpty()) {
            for (Lead lead : Trigger.new)  {
                for (Ecosystem__c ECO : entries_ECO.values()) {
                    if (ECO.Lead__c == lead.Id) {
                        // ECO.contact__c = lead.ConvertedContactId;
                        // ECO.opportunity__c = lead.ConvertedOpportunityId;
                        ECO.account__c = lead.ConvertedAccountId;
                        update ECO;
                        // break;
                    }
                }
            }
        }

        Map<Id, Partner_Influence__c> entries_PI = new Map<Id, Partner_Influence__c>([select Account__c, Lead__c from Partner_Influence__c where lead__c in :leadIds]);      
        if(!Trigger.new.isEmpty()) {
            for (Lead lead : Trigger.new)  {
                for (Partner_Influence__c PI : entries_PI.values()) {
                    if (PI.Lead__c == lead.Id) {
                        // PI.contact__c = lead.ConvertedContactId;
                        PI.opportunity__c = lead.ConvertedOpportunityId;
                        PI.account__c = lead.ConvertedAccountId;
                        update PI;
                        // break;
                    }
                }
            }
        }

        Map<Id, Qualification_Credit__c> entries_QC = new Map<Id, Qualification_Credit__c>([select Account__c, Lead__c from Qualification_Credit__c where lead__c in :leadIds]);      
        if(!Trigger.new.isEmpty()) {
            for (Lead lead : Trigger.new)  {
                for (Qualification_Credit__c QC : entries_QC.values()) {
                    if (QC.Lead__c == lead.Id) {
                        // QC.contact__c = lead.ConvertedContactId;
                        QC.opportunity__c = lead.ConvertedOpportunityId;
                        QC.account__c = lead.ConvertedAccountId;
                        update QC;
                        // break;
                    }
                }
            }
        }


    }
}

}


//---------------
@isTest
//This is a test case for a situation where a lead will be converted.  The developer must explicitly call the convert lead
//method to simulate the user action.

private class TestLeadConvertCustomObjects {
    static testMethod void TestReferralUpdate() {
    // Insert the Lead
    List<Lead> leads = new List<Lead>();
    Lead leadt = new Lead (FirstName ='fname', LastName ='test', Company ='myCompany');
    insert leadt;
    // Insert the DocuSign_Account__c Record
    DocuSign_Account__c DSA = new DocuSign_Account__c (Lead__c = leadt.Id);
    insert DSA;
    // Insert the DocuSign_Account_Member__c Record
    DocuSign_Account_Member__c DSAM = new DocuSign_Account_Member__c (Lead__c = leadt.Id, DocuSign_Account__c = DSA.Id);
    insert DSAM;
    // Insert the Use_Case__c Record
    Use_Case__c UC = new Use_Case__c (Lead__c = leadt.Id, Department__c='Legal', Use_Case__c='NDAs', Status__c='Interest');
    insert UC;
    // Insert a Partner Account Record with a Partner Go To Market to relate Ecosystem & Partner Influence to
    Account PartnerAccount = new Account (Name='PartnerAccount', Partner_Go_To_Market_Count_Active__c=1);
    insert PartnerAccount;
    Partner_Go_To_Market__c PGTM = new Partner_Go_To_Market__c (Name='PartnerGoToMarket', Account__c = PartnerAccount.Id, GTM_Status__c='Agreement Signed');
    insert PGTM;
    // Insert the Ecosystem__c Record
    Ecosystem__c ECO = new Ecosystem__c (Lead__c = leadt.Id, Partner__c = PartnerAccount.Id, Ecosystem_Type__c='CRM', Integration__c='Not Integrated');
    insert ECO;
    // Insert the Partner_Influence__c Record
    Partner_Influence__c PI = new Partner_Influence__c (Lead__c = leadt.Id, Partner__c = PartnerAccount.Id, Role__c='Influencer', Influence_Type__c='Neutral');
    insert PI;
    // Insert the Qualification_Credit__c Record
    Qualification_Credit__c QC = new Qualification_Credit__c (Lead__c = leadt.Id, Status__c='Qualified');
    insert QC;

//test.startTest();

//Convert the Lead
Database.LeadConvert lc = new database.LeadConvert();
lc.setLeadId(leadt.Id);
LeadStatus convertStatus = [Select Id, MasterLabel from LeadStatus where MasterLabel='Contact Created Only' AND IsConverted=true limit 1];
lc.setConvertedStatus(convertStatus.MasterLabel);
Database.LeadConvertResult lcr = Database.convertLead(lc);  

//Requery for the referral record to see if it is updated
DocuSign_Account__c ref_upd_DSA = [select Account__c from DocuSign_Account__c where Lead__c = :leadt.Id];
DocuSign_Account_Member__c ref_upd_DSAM = [select Contact__c from DocuSign_Account_Member__c where Lead__c = :leadt.Id];
Use_Case__c ref_upd_UC = [select Account__c from Use_Case__c where Lead__c = :leadt.Id];
Ecosystem__c ref_upd_ECO = [select Account__c from Ecosystem__c where Lead__c = :leadt.Id];
Partner_Influence__c ref_upd_PI = [select Account__c, Opportunity__c from Partner_Influence__c where Lead__c = :leadt.Id];
Qualification_Credit__c ref_upd_QC = [select Account__c, Opportunity__c, Contact__c from Qualification_Credit__c where Lead__c = :leadt.Id];

//Check that the test passed
    System.assertEquals(ref_upd_DSA.Account__c,[Select ConvertedAccountId From Lead Where Id = :DSA.Lead__c].ConvertedAccountId);
    System.assertEquals(ref_upd_DSAM.Contact__c,[Select ConvertedContactId From Lead Where Id = :DSAM.Lead__c].ConvertedContactId);
    System.assertEquals(ref_upd_UC.Account__c,[Select ConvertedAccountId From Lead Where Id = :UC.Lead__c].ConvertedAccountId);
    System.assertEquals(ref_upd_ECO.Account__c,[Select ConvertedAccountId From Lead Where Id = :ECO.Lead__c].ConvertedAccountId);
    System.assertEquals(ref_upd_PI.Account__c,[Select ConvertedAccountId From Lead Where Id = :PI.Lead__c].ConvertedAccountId);
    System.assertEquals(ref_upd_QC.Account__c,[Select ConvertedAccountId From Lead Where Id = :QC.Lead__c].ConvertedAccountId);
    System.assertEquals(ref_upd_QC.Contact__c,[Select ConvertedContactId From Lead Where Id = :QC.Lead__c].ConvertedContactId);
    System.assertEquals(ref_upd_PI.Opportunity__c,[Select ConvertedOpportunityId From Lead Where Id = :PI.Lead__c].ConvertedOpportunityId);
    System.assertEquals(ref_upd_QC.Opportunity__c,[Select ConvertedOpportunityId From Lead Where Id = :QC.Lead__c].ConvertedOpportunityId);    

//Test if no opty is created
string NoOpty = 'Y';  
if (NoOpty =='Y'){
  Lead leadto = new Lead (FirstName ='fnameo', LastName ='testo', Company ='myCompanyo');
      insert leadto;
      // Insert the custom object record
      // CustomObject__c customobjecto = new CustomObject__c (Lead__c = leadto.Id);
      // insert customobjecto;
      // Insert the DocuSign_Account__c Record
      DocuSign_Account__c DSAo = new DocuSign_Account__c (Lead__c = leadto.Id);
      insert DSAo;
      // Insert the DocuSign_Account_Member__c Record
      DocuSign_Account_Member__c DSAMo = new DocuSign_Account_Member__c (Lead__c = leadto.Id, DocuSign_Account__c = DSAo.Id);
      insert DSAMo;  
      // Insert the Use_Case__c Record
      Use_Case__c UCo = new Use_Case__c (Lead__c = leadto.Id, Department__c='Legal', Use_Case__c='NDAs', Status__c='Interest');
      insert UCo;
      // Insert a Partner Account Record with a Partner Go To Market to relate Ecosystem & Partner Influence to
      Account PartnerAccounto = new Account (Name='PartnerAccounto', Partner_Go_To_Market_Count_Active__c=1);
      insert PartnerAccounto;
      Partner_Go_To_Market__c PGTMo = new Partner_Go_To_Market__c (Name='PartnerGoToMarketo', Account__c = PartnerAccounto.Id, GTM_Status__c='Agreement Signed');
      insert PGTMo;
      // Insert the Ecosystem__c Record
      Ecosystem__c ECOo = new Ecosystem__c (Lead__c = leadto.Id, Partner__c = PartnerAccounto.Id, Ecosystem_Type__c='CRM', Integration__c='Not Integrated');
      insert ECOo;
      // Insert the Partner_Influence__c Record
      Partner_Influence__c PIo = new Partner_Influence__c (Lead__c = leadto.Id, Partner__c = PartnerAccounto.Id, Role__c='Influencer', Influence_Type__c='Neutral');
      insert PIo;
      // Insert the Qualification_Credit__c Record
      Qualification_Credit__c QCo = new Qualification_Credit__c (Lead__c = leadto.Id, Status__c='Qualified');
      insert QCo;




  Database.LeadConvert lco = new database.LeadConvert();
  lco.setLeadId(leadto.Id);
  lco.isDoNotCreateOpportunity();
  lco.setDoNotCreateOpportunity(true);
  LeadStatus convertStatuso = [Select Id, MasterLabel from LeadStatus where MasterLabel='Contact Created Only' AND IsConverted=true limit 1];
  lco.setConvertedStatus(convertStatuso.MasterLabel);
  Database.LeadConvertResult lcro = Database.convertLead(lco);

  DocuSign_Account__c ref_updo_DSA = [select Account__c from DocuSign_Account__c where Lead__c = :leadto.Id];
  DocuSign_Account_Member__c ref_updo_DSAM = [select Contact__c from DocuSign_Account_Member__c where Lead__c = :leadto.Id];
  Use_Case__c ref_updo_UC = [select Account__c from Use_Case__c where Lead__c = :leadto.Id];
  Ecosystem__c ref_updo_ECO = [select Account__c from Ecosystem__c where Lead__c = :leadto.Id];
  Partner_Influence__c ref_updo_PI = [select Account__c, Opportunity__c from Partner_Influence__c where Lead__c = :leadto.Id];
  Qualification_Credit__c ref_updo_QC = [select Account__c, Opportunity__c, Contact__c from Qualification_Credit__c where Lead__c = :leadto.Id];


  //Check that the test passed
      System.assertEquals(ref_updo_DSA.Account__c,[Select ConvertedAccountId From Lead Where Id = :DSAo.Lead__c].ConvertedAccountId);
      System.assertEquals(ref_updo_DSAM.Contact__c,[Select ConvertedContactId From Lead Where Id = :DSAMo.Lead__c].ConvertedContactId);
      System.assertEquals(ref_updo_UC.Account__c,[Select ConvertedAccountId From Lead Where Id = :UCo.Lead__c].ConvertedAccountId);
      System.assertEquals(ref_updo_ECO.Account__c,[Select ConvertedAccountId From Lead Where Id = :ECOo.Lead__c].ConvertedAccountId);
      System.assertEquals(ref_updo_PI.Account__c,[Select ConvertedAccountId From Lead Where Id = :PIo.Lead__c].ConvertedAccountId);
      System.assertEquals(ref_updo_QC.Account__c,[Select ConvertedAccountId From Lead Where Id = :QCo.Lead__c].ConvertedAccountId);
      System.assertEquals(ref_updo_QC.Contact__c,[Select ConvertedContactId From Lead Where Id = :QCo.Lead__c].ConvertedContactId);
      System.assertEquals(ref_updo_PI.Opportunity__c,[Select ConvertedOpportunityId From Lead Where Id = :PIo.Lead__c].ConvertedOpportunityId);
      System.assertEquals(ref_updo_QC.Opportunity__c,[Select ConvertedOpportunityId From Lead Where Id = :QCo.Lead__c].ConvertedOpportunityId);    
      // System.assert(ref_updo__DSA.Opportunity__c == null);

//test.stopTest();
}
}

  static testMethod void testBulkUpdate() {
    List<Lead> leads = new List<Lead>();    
    for (Integer i=0;i<5;i++) {
        Lead l = new Lead (FirstName ='bulk', LastName ='Test', Company ='myCompanyo');
    insert l;
    // Insert the Custom Record
     DocuSign_Account__c DSAr = new DocuSign_Account__c (Lead__c = l.Id);
     insert DSAr;
     DocuSign_Account_Member__c DSAMr = new DocuSign_Account_Member__c (Lead__c = l.Id, DocuSign_Account__c = DSAr.Id);
     insert DSAMr;
     // Insert the Use_Case__c Record
     Use_Case__c UCr = new Use_Case__c (Lead__c = l.Id, Department__c='Legal', Use_Case__c='NDAs', Status__c='Interest');
     insert UCr;
     // Insert a Partner Account Record with a Partner Go To Market to relate Ecosystem & Partner Influence to
     Account PartnerAccountr = new Account (Name='PartnerAccountBulk', Partner_Go_To_Market_Count_Active__c=1);
     insert PartnerAccountr;
     Partner_Go_To_Market__c PGTMr = new Partner_Go_To_Market__c (Name='PartnerGoToMarketBulk', Account__c = PartnerAccountr.Id, GTM_Status__c='Agreement Signed');
     insert PGTMr;
     // Insert the Ecosystem__c Record
     Ecosystem__c ECOr = new Ecosystem__c (Lead__c = l.Id, Partner__c = PartnerAccountr.Id, Ecosystem_Type__c='CRM', Integration__c='Not Integrated');
     insert ECOr;
     // Insert the Partner_Influence__c Record
     Partner_Influence__c PIr = new Partner_Influence__c (Lead__c = l.Id, Partner__c = PartnerAccountr.Id, Role__c='Influencer', Influence_Type__c='Neutral');
     insert PIr;
     // Insert the Qualification_Credit__c Record
     Qualification_Credit__c QCr = new Qualification_Credit__c (Lead__c = l.Id, Status__c='Qualified');
     insert QCr;


      test.startTest();

      //Convert the Lead
  Database.LeadConvert lcb = new database.LeadConvert();
  lcb.setLeadId(l.Id);
  LeadStatus convertStatusb = [Select Id, MasterLabel from LeadStatus where MasterLabel='Contact Created Only' AND IsConverted=true limit 1];
  lcb.setConvertedStatus(convertStatusb.MasterLabel);
  Database.LeadConvertResult lcrb = Database.convertLead(lcb);

  DocuSign_Account__c bulkup_DSA = [select Account__c from DocuSign_Account__c where Lead__c =:l.Id];
  DocuSign_Account_Member__c bulkup_DSAM = [select Contact__c from DocuSign_Account_Member__c where Lead__c =:l.Id];
  Use_Case__c bulkup_UC = [select Account__c from Use_Case__c where Lead__c = :l.Id];
  Ecosystem__c bulkup_ECO = [select Account__c from Ecosystem__c where Lead__c = :l.Id];
  Partner_Influence__c bulkup_PI = [select Account__c, Opportunity__c from Partner_Influence__c where Lead__c = :l.Id];
  Qualification_Credit__c bulkup_QC = [select Account__c, Opportunity__c, Contact__c from Qualification_Credit__c where Lead__c = :l.Id];

  //Check that the test has passed
      System.assertEquals(bulkup_DSA.Account__c,[Select ConvertedAccountId From Lead Where Id = :DSAr.Lead__c].ConvertedAccountId);
      System.assertEquals(bulkup_DSAM.Contact__c,[Select ConvertedContactId From Lead Where Id = :DSAMr.Lead__c].ConvertedContactId);
      System.assertEquals(bulkup_UC.Account__c,[Select ConvertedAccountId From Lead Where Id = :UCr.Lead__c].ConvertedAccountId);
      System.assertEquals(bulkup_ECO.Account__c,[Select ConvertedAccountId From Lead Where Id = :ECOr.Lead__c].ConvertedAccountId);
      System.assertEquals(bulkup_PI.Account__c,[Select ConvertedAccountId From Lead Where Id = :PIr.Lead__c].ConvertedAccountId);
      System.assertEquals(bulkup_QC.Account__c,[Select ConvertedAccountId From Lead Where Id = :QCr.Lead__c].ConvertedAccountId);
      System.assertEquals(bulkup_QC.Contact__c,[Select ConvertedContactId From Lead Where Id = :QCr.Lead__c].ConvertedContactId);
      System.assertEquals(bulkup_PI.Opportunity__c,[Select ConvertedOpportunityId From Lead Where Id = :PIr.Lead__c].ConvertedOpportunityId);
      System.assertEquals(bulkup_QC.Opportunity__c,[Select ConvertedOpportunityId From Lead Where Id = :QCr.Lead__c].ConvertedOpportunityId);    
      // System.assertEquals(bulkup.Opportunity__c,[Select ConvertedOpportunityId From Lead Where Id = :r.Lead__c].ConvertedOpportunityId);


     }
  }
}