+ Start a Discussion
isalewisalew 

Help Bulkifying Cross-Object Trigger & SOQL

I wrote some Apex to manage multiple leads from the same company through a custom object, LeadCompany__c. The Apex works great on the surface, but there are a few serious flaws:

1. All the code is in a Trigger, not a class with methods.
2. I am using too many SOQL queries to perform cross-object manipulations.

Bulkifying code and building classes are a little beyond me, so I have two requests:
  • Can anyone show me how to bulkify these triggers?
  • Can anyone show me how to convert these triggers into classes/methods?
Any help would be greatly appreciated.
isalewisalew
// TRIGGER 1 OF 2
// This trigger updates Leads from the LeadCompany__c object

trigger LC_Update_LD on LeadCompany__c (after update){
   for (LeadCompany__c LC: Trigger.new) {

        List<LeadCompany__c> LClist = [SELECT ID, OwnerId, Name, Lead_Count__c, Status__c FROM LeadCompany__c WHERE ID = :LC.Id LIMIT 1];
        List<Lead> LDList = [SELECT Id, OwnerID, Company, LC_Lead_Company__c FROM Lead WHERE LC_Lead_Company__C in :LClist AND isConverted = False];
        LeadCompany__c oldLC = Trigger.oldMap.get(LC.ID);

  if(LC.Lead_Count__c != null){
         // Update Leads Owner
         if(LC.OwnerID != oldLC.OwnerID) {
             System.debug('--*Lead Company Owner is changed*--');
             System.debug('**Old Owner :'+oldLC.OwnerID);
             System.debug('**New Owner :'+LC.OwnerID);

    String OldOwnerId = oldLC.OwnerId;
             String NewOwnerId = LC.OwnerId;
     
             //Logic to change lead owners
             for(integer i = 0; i < LdList.size(); i++){
                     LdList[i].OwnerID = LC.OwnerID;
             }
             update LdList;
         }    

         // Update Leads Company Name
         if(LC.Name != oldLC.Name) {
             System.debug('--*Lead Company Name is changed*--');
             System.debug('**Old Name :'+oldLC.Name);
             System.debug('**New Name :'+LC.Name);

             for(integer i = 0; i < LdList.size(); i++){
                     LdList[i].Company = LC.Name;
             }
             update LdList;
         }    

  }
}
}
isalewisalew
(Forgive the lack of spaces. It's the only way I could fit the trigger)

//TRIGGER2OF2
//ThistriggerupdatesthetriggeringLeadandupdatesorcreatesaLeadCompany__crecord,whichinturnupdatesanyassociatedLeads(seeTrigger2)

triggerLD_Update_LConLead(beforeinsert,beforeupdate,afterdelete,afterinsert,afterupdate){

Set<ID>LeadCompanyIds=newSet<ID>();
Set<ID>OldLcId=newSet<Id>();

if(Trigger.isBefore){

//ForNewLeads
if(Trigger.isInsert){
for(Leadld:Trigger.new){

//Ifnoblankemail,checkblacklist
if(ld.LC_Email_Domain__c!=null){

//Ifdomainnotonblacklist,associateexistingorcreatenew
List<Blacklist_Domain__c>Blacklist=[SELECTNameFROMBlacklist_Domain__cWHEREName=:ld.LC_Email_Domain__c];
if(Blacklist.size()==0){

//Searchforexistingcompany
List<LeadCompany__c>lc=[SELECTID,Name,OwnerId,Status__c,Domain__cFROMLeadCompany__cWHEREDomain__c=:ld.LC_Email_Domain__cLIMIT1];

//Ifcompanyexists,associateLD,changeLDOwner,compareLCStatus
if(lc.size()>0){
for(integeri=0;i<lc.size();i++){

//AssociateLeadtoCompany
ld.LC_Lead_Company__c=lc[i].id;

//ChangeLeadOwnertoCompanyOwner
ld.OwnerId=lc[i].ownerId;

//CompareLeadStatustoCompanyStatus
if(lc[i].Status__c!=Ld.Status){

Map<String,Integer>status_value=newMap<String,Integer>{
'Open-NotContacted'=>2,
'Working-Contacted'=>3,
'Working-InitialMeetingScheduled'=>4,
'Closed-Qualified'=>5,
'Closed-Unqualified'=>1,
null=>0
};

IntegerLC_Status_Value=status_value.get(lc[i].Status__c);
IntegerLD_Status_Value=status_value.get(Ld.Status);

//SetLeadCompanyStatus
if(LD_Status_Value>LC_Status_Value){
lc[i].Status__c=Ld.Status;
}
}
}
updatelc;
}

//Ifnocompanyexists,createnewLC,associateLD
else{

//Createnewcompany
LeadCompany__cnlc=newLeadCompany__c(
Domain__c=ld.LC_Email_Domain__c,
Name=ld.Company,
OwnerId=ld.OwnerId,
Status__c=ld.Status
);
insertnlc;

//Associatenewleadtonewcompany
ld.LC_Lead_Company__c=nlc.id;
}
}
}
}
}

//ForExistingLeads
if(Trigger.isUpdate){
for(Leadld:Trigger.new){

//Getoldrecordvalues
LeadoldLd=Trigger.oldMap.get(Ld.ID);

//Ifnoemail,emailischanged,orcompanyleftblank,checkblacklist
if(ld.LC_Email_Domain__c!=null&&(ld.LC_Email_Domain__c!=oldld.LC_Email_Domain__c||string.isblank(ld.LC_lead_Company__c))){

//Ifdomainnotonblacklist,associateexistingorcreatenew
List<Blacklist_Domain__c>Blacklist=[SELECTNameFROMBlacklist_Domain__cWHEREName=:ld.LC_Email_Domain__c];
if(Blacklist.size()==0){

//Searchforexistingcompany
List<LeadCompany__c>lc=[SELECTID,Name,OwnerId,Status__c,Domain__cFROMLeadCompany__cWHEREDomain__c=:ld.LC_Email_Domain__cLIMIT1];

//Ifcompanyexists,associateLD,changeLDOwner,compareLCStatus
if(lc.size()>0){
for(integeri=0;i<lc.size();i++){

//AssociateLeadtoCompany
ld.LC_Lead_Company__c=lc[i].id;

//ChangeLeadOwnertoCompanyOwner
ld.OwnerId=lc[i].ownerId;

//CompareLeadStatustoOtherLeads,SetCompanyStatus
if(lc[i].Status__c!=Ld.Status){

Map<String,Integer>status_value=newMap<String,Integer>{
'Open-NotContacted'=>2,
'Working-Contacted'=>3,
'Working-InitialMeetingScheduled'=>4,
'Closed-Qualified'=>5,
'Closed-Unqualified'=>1,
null=>0
};

Map<Integer,String>status_name=newMap<Integer,String>{
2=>'Open-NotContacted',
3=>'Working-Contacted',
4=>'Working-InitialMeetingScheduled',
5=>'Closed-Qualified',
1=>'Closed-Unqualified',
0=>null
};

List<Lead>Lds=[SELECTID,StatusFROMLeadWHERELC_Lead_Company__c=:ld.LC_Lead_Company__c];
List<Integer>LD_Stat_Values=newList<Integer>();
IntegerLC_Stat_Value=status_value.get(lc[i].Status__c);

//ConvertPicklistvaluestonumberedlist
for(integerj=0;j<Lds.size();j++){
LD_Stat_Values.add(status_value.get(lds[j].Status));
}

//GetHighestPicklistValue
IntegerLD_Stat_Val=0;
for(integerj=0;j<Lds.size();j++){
if(status_value.get(lds[j].Status)>LD_Stat_Val){
LD_Stat_Val=status_value.get(lds[j].Status);
}
}

//SetLeadCompanyStatus
if(LD_Stat_Val!=LC_Stat_Value){
lc[i].Status__c=status_name.get(LD_Stat_Val);
}
}
}
updatelc;
}

//Ifnocompanyexists,createnewLC,associateLD
else{

//Createnewcompany
LeadCompany__cnlc=newLeadCompany__c(
Domain__c=ld.LC_Email_Domain__c,
Name=ld.Company,
OwnerId=ld.OwnerId,
Status__c=ld.Status
);
insertnlc;

//Associatenewleadtonewcompany
ld.LC_Lead_Company__c=nlc.id;
}
}

//Ifdomainisonblacklist,removeassociation,updateoldcompany
else{
//Removecompanyassociation
Ld.LC_Lead_Company__c=null;

//UpdateOldCompany[HOWDOIDOTHIS!!]
LeadCompanyIds.add(oldld.LC_Lead_Company__c);
}
}
}
}
}

if(Trigger.isAfter){

//ForDeletedLeads,addcompanytoupdatelist
if(trigger.isDelete){
for(Leadld:trigger.old){
if(ld.LC_Lead_Company__c!=null)
LeadCompanyIds.add(ld.LC_Lead_Company__c);
}
}

//ForInsertedLeads,addcompanytoupdatelist
if(trigger.isInsert){
for(Leadld:trigger.new){
if(ld.LC_Lead_Company__c!=null)
LeadCompanyIds.add(ld.LC_Lead_Company__c);
}
}

//ForUpdatedLeads
if(Trigger.isUpdate){
for(LeadLd:Trigger.new){

//ForConvertedLeads
if(ld.IsConverted==True&&ld.LC_Lead_Company__c!=null){
//FindrelatedLeads
IDAccId=ld.ConvertedAccountId;
List<Lead>LeadConvertList=[SELECTId,LC_Lead_Company__cFROMLeadWHERELC_Lead_Company__C=:ld.LC_Lead_Company__cANDIsConverted=False];

//ConvertallrelatedLeads
if(LeadConvertList.size()>0){

for(integeri=0;i<LeadConvertList.size();i++){
Database.LeadConvertlc=newDatabase.LeadConvert();
lc.setLeadId(LeadConvertList[i].id);
lc.setAccountId(AccId);
lc.setDoNotCreateOpportunity(TRUE);

LeadStatusconvertStatus=[SELECTId,MasterLabel,IsConvertedFROMLeadStatusWHEREIsConverted=truelimit1];
lc.setConvertedStatus(convertStatus.MasterLabel);

Database.LeadConvertResultlcr=Database.convertLead(lc);
System.assert(lcr.isSuccess());
}
}
}

//ForUnconvertedLeads
else{

//Getoldvalues
LeadoldLd=Trigger.oldMap.get(Ld.ID);

//CheckforLeadtransfers
if(ld.LC_Lead_Company__c!=oldLd.LC_Lead_Company__c){
if(ld.LC_Lead_Company__c!=null){
LeadCompanyIds.add(ld.LC_Lead_Company__c);
}
if(oldLd.LC_Lead_Company__c!=null){
OldLcId.add(oldLd.LC_Lead_Company__c);
}
}

//UpdateNewLeadCompany
List<LeadCompany__c>NewLC=[SELECTId,Name,OwnerID,Domain__c,Status__cFROMLeadCompany__cWHEREID=:Ld.LC_Lead_Company__cLIMIT1];
if(NewLC.size()>0){

for(integeri=0;i<NewLC.size();i++){

//Updateleadcompanyowner
if(Ld.OwnerID!=oldLd.OwnerID){
NewLC[i].OwnerID=Ld.OwnerID;
}

//Updateleadcompanyname
if(Ld.Company!=oldLd.Company){
NewLC[i].Name=Ld.Company;
}

//Updateleadcompanystatus
if(NewLC[i].Status__c!=Ld.Status){

Map<String,Integer>status_value=newMap<String,Integer>{
'Open-NotContacted'=>2,
'Working-Contacted'=>3,
'Working-InitialMeetingScheduled'=>4,
'Closed-Qualified'=>5,
'Closed-Unqualified'=>1,
null=>0
};

Map<Integer,String>status_name=newMap<Integer,String>{
2=>'Open-NotContacted',
3=>'Working-Contacted',
4=>'Working-InitialMeetingScheduled',
5=>'Closed-Qualified',
1=>'Closed-Unqualified',
0=>null
};

List<Lead>Lds=[SELECTID,StatusFROMLeadWHERELC_Lead_Company__c=:ld.LC_Lead_Company__c];
List<Integer>LD_Stat_Values=newList<Integer>();
IntegerLC_Stat_Val=status_value.get(NewLC[i].Status__c);
IntegerLD_Stat_Val=0;

//ConvertPicklistvaluestonumberedlist
for(integerj=0;j<Lds.size();j++){
LD_Stat_Values.add(status_value.get(lds[j].Status));
}

//GethighestLeadStatus
for(integerj=0;j<Lds.size();j++){
if(status_value.get(lds[j].Status)>LD_Stat_Val){
LD_Stat_Val=status_value.get(lds[j].Status);
}
}
//CompareLeadStatuswithLeadCompanyStatus
if(LD_Stat_Val!=LC_Stat_Val){
NewLC[i].Status__c=status_name.get(LD_Stat_Val);
}
}
}

//Updateleadcompany
updateNewLC;
}
//UpdateOldLeadCompany
List<LeadCompany__c>OldLC=[SELECTId,Name,OwnerID,Domain__c,Status__cFROMLeadCompany__cWHEREID=:oldLd.LC_Lead_Company__cANDID!=:Ld.LC_Lead_Company__cLIMIT1];
if(OldLC.size()>0){
//Updateleadcompanystatus
for(integeri=0;i<OldLC.size();i++){
if(OldLC[i].Status__c==OldLd.Status){

Map<String,Integer>status_value=newMap<String,Integer>{
'Open-NotContacted'=>2,
'Working-Contacted'=>3,
'Working-InitialMeetingScheduled'=>4,
'Closed-Qualified'=>5,
'Closed-Unqualified'=>1,
null=>0
};

Map<Integer,String>status_name=newMap<Integer,String>{
2=>'Open-NotContacted',
3=>'Working-Contacted',
4=>'Working-InitialMeetingScheduled',
5=>'Closed-Qualified',
1=>'Closed-Unqualified',
0=>null
};

List<Lead>Lds=[SELECTID,StatusFROMLeadWHERELC_Lead_Company__c=:oldld.LC_Lead_Company__c];
List<Integer>LD_Stat_Values=newList<Integer>();
IntegerLC_Stat_Value=status_value.get(OldLC[i].Status__c);

//ConvertPicklistvaluestonumberedlist
for(integerj=0;j<Lds.size();j++){
LD_Stat_Values.add(status_value.get(lds[j].Status));
}

IntegerLD_Stat_Val=0;
for(integerj=0;j<Lds.size();j++){
if(status_value.get(lds[j].Status)>LD_Stat_Val){
LD_Stat_Val=status_value.get(lds[j].Status);
}
}

if(LD_Stat_Val!=LC_Stat_Value){
OldLC[i].Status__c=status_name.get(LD_Stat_Val);
}
updateNewLC;
}
}
updateOldLC;
Set<Id>OldLCset=(newMap<Id,LeadCompany__c>(OldLC)).keySet();
LeadCompanyIds.addall(OldLCset);
}
}
}
}
}

if(LeadCompanyIds!=Null){
List<AggregateResult>aggs=[selectLC_Lead_Company__c,count(ID)LeadCountfromLeadwhereLC_Lead_Company__cin:LeadCompanyIdsGroupByLC_Lead_Company__c];
Map<Id,Integer>LeadCountMap=newMap<Id,Integer>();
for(AggregateResultagg:aggs){
LeadCountMap.put(string.valueOf(agg.get('LC_Lead_Company__c')),Integer.valueOf(agg.get('LeadCount')));
}

List<LeadCompany__c>LeadCompaniesToUpdate=newList<LeadCompany__c>();
if(LeadCompanyIds!=null){
for(IdLcId:LeadCompanyIds){
LeadCompany__cnewLc=newLeadCompany__c(id=LcId);
newLc.Lead_Count__c=LeadCountMap.get(LcId);
LeadCompaniesToUpdate.add(newLc);
}
}
updateLeadCompaniesToUpdate;
}

//UpdateLeadCountForOldLeadCompany
if(OldLcId!=Null){
List<AggregateResult>aggsOld=[selectLC_Lead_Company__c,count(ID)LeadCountfromLeadwhereLC_Lead_Company__cin:OldLcIdGroupByLC_Lead_Company__c];
Map<Id,Integer>LeadCountMap=newMap<Id,Integer>();
for(AggregateResultagg:aggsOld){
LeadCountMap.put(string.valueOf(agg.get('LC_Lead_Company__c')),Integer.valueOf(agg.get('LeadCount')));
}
List<LeadCompany__c>LeadCompaniesToUpdateOld=newList<LeadCompany__c>();

if(OldLcId!=null){
for(IdLcId:OldLcId){
LeadCompany__cnewLc=newLeadCompany__c(id=LcId);
newLc.Lead_Count__c=LeadCountMap.get(LcId);
LeadCompaniesToUpdateOld.add(newLc);
}
}
updateLeadCompaniesToUpdateOld;
}
}
isalewisalew
//TEST CLASS
//There are flaws in this test, but I have not been able to test it through due to SOQL overloads

@isTest

private class LC_TestClass{

    static testmethod void LeadCompanyTests(){

  //User Setup
        User u1 = [SELECT Id FROM User WHERE Alias='ilewi'];
        User u2 = [SELECT Id FROM User WHERE Alias='gpark'];

        //Blacklist Setup
        Blacklist_Domain__c Gmail = new Blacklist_Domain__c(
            Name = 'gmail.com'
        );
        Insert Gmail;

        //Insert LD1
        Lead LD1 = new Lead(
            LastName = 'LD1',
            Email = 'LD1@LCA.com',
            Company = 'LCA',
            Status = 'Open - Not Contacted',
            OwnerID = u1.ID
        );
        Insert LD1;
 
        //Verify LCA Create
        LD1 = [SELECT ID, Company, Status, OwnerID, LC_Lead_Company__c, LC_Lead_Company__r.Name, LC_Lead_Company__r.OwnerId, LC_Lead_Company__r.Status__c, LC_Lead_Company__r.Lead_Count__c, LC_Lead_Company__r.Domain__c FROM Lead WHERE ID = :LD1.id];
        System.assertNotEquals( LD1.LC_Lead_Company__c, null );
        System.assertEquals( LD1.OwnerId,   LD1.LC_Lead_Company__r.OwnerId );
        System.assertEquals( LD1.Company,   LD1.LC_Lead_Company__r.Name );
        System.assertEquals( LD1.Status,    LD1.LC_Lead_Company__r.Status__c );
        System.assertEquals( LD1.LC_Lead_Company__r.Lead_Count__c, 1 );
 
        //Insert LD2
        Lead LD2 = new Lead(
            LastName = 'LD2',
            Email = 'LD2@LCA.com',
            Company = 'LCAlpha',
            Status = 'Working - Contacted',
            OwnerID = u2.ID
        );
        Insert LD2;

        //Verify LCA Match & Update
        LD1 = [SELECT ID, Company, Status, OwnerID, LC_Lead_Company__c, LC_Lead_Company__r.Name, LC_Lead_Company__r.OwnerId, LC_Lead_Company__r.Status__c, LC_Lead_Company__r.Lead_Count__c, LC_Lead_Company__r.Domain__c FROM Lead WHERE ID = :LD1.id];
        LD2 = [SELECT ID, Company, Status, OwnerID, LC_Lead_Company__c, LC_Lead_Company__r.Name, LC_Lead_Company__r.OwnerId, LC_Lead_Company__r.Status__c, LC_Lead_Company__r.Lead_Count__c, LC_Lead_Company__r.Domain__c FROM Lead WHERE ID = :LD2.id];

        System.assertNotEquals( LD2.LC_Lead_Company__c,     null );
        System.assertEquals( LD2.LC_Lead_Company__r.ID,    LD1.LC_Lead_Company__r.ID );
        System.assertNotEquals( LD2.LC_Lead_Company__r.Name,   LD2.Company );
        System.assertEquals( LD2.LC_Lead_Company__r.Status__c,  LD2.Status );
        System.assertEquals( LD2.LC_Lead_Company__r.Lead_Count__c, 2 );
        System.assertEquals( LD2.OwnerId,       u1.ID );

        //Update LD2 Email to Blacklist
        LD2.email = 'LD2@gmail.com';
  Update LD2;

        //Verify LD2 Remove & LCA Update
        LD1 = [SELECT ID, Company, Status, OwnerID, LC_Lead_Company__c, LC_Lead_Company__r.Name, LC_Lead_Company__r.OwnerId, LC_Lead_Company__r.Status__c, LC_Lead_Company__r.Lead_Count__c, LC_Lead_Company__r.Domain__c FROM Lead WHERE ID = :LD1.id];
        LD2 = [SELECT ID, Company, Status, OwnerID, LC_Lead_Company__c, LC_Lead_Company__r.Name, LC_Lead_Company__r.OwnerId, LC_Lead_Company__r.Status__c, LC_Lead_Company__r.Lead_Count__c, LC_Lead_Company__r.Domain__c FROM Lead WHERE ID = :LD2.id];

        System.assertEquals( LD2.LC_Lead_Company__c,     null );
        System.assertEquals( LD1.LC_Lead_Company__r.Lead_Count__c, 1 );
        System.assertEquals( LD1.LC_Lead_Company__r.Status__c,  LD1.Status );

        //Update LD2 Email
        LD2.Email = 'LD2@LCA.com';
        Update LD2;

        //Verify LD2 Re-Attach & LCA Update
        LD1 = [SELECT ID, Company, Status, OwnerID, LC_Lead_Company__c, LC_Lead_Company__r.Name, LC_Lead_Company__r.OwnerId, LC_Lead_Company__r.Status__c, LC_Lead_Company__r.Lead_Count__c, LC_Lead_Company__r.Domain__c FROM Lead WHERE ID = :LD1.id];
        LD2 = [SELECT ID, Company, Status, OwnerID, LC_Lead_Company__c, LC_Lead_Company__r.Name, LC_Lead_Company__r.OwnerId, LC_Lead_Company__r.Status__c, LC_Lead_Company__r.Lead_Count__c, LC_Lead_Company__r.Domain__c FROM Lead WHERE ID = :LD2.id];

        System.assertNotEquals( LD2.LC_Lead_Company__c,     null );
        System.assertEquals( LD2.LC_Lead_Company__r.ID,    LD1.LC_Lead_Company__r.ID );
        System.assertEquals( LD2.LC_Lead_Company__r.Status__c,  LD2.Status );
        System.assertEquals( LD2.LC_Lead_Company__r.Lead_Count__c, 2 );

        //Update LD2 Email
        LD2.Email = 'LD2@LCB.com';
        LD2.Company = 'LCB';
        Update LD2;

        //Verify LCB Create & LCA Update
        LD1 = [SELECT ID, Company, Status, OwnerID, LC_Lead_Company__c, LC_Lead_Company__r.Name, LC_Lead_Company__r.OwnerId, LC_Lead_Company__r.Status__c, LC_Lead_Company__r.Lead_Count__c, LC_Lead_Company__r.Domain__c FROM Lead WHERE ID = :LD1.id];
        LD2 = [SELECT ID, Company, Status, OwnerID, LC_Lead_Company__c, LC_Lead_Company__r.Name, LC_Lead_Company__r.OwnerId, LC_Lead_Company__r.Status__c, LC_Lead_Company__r.Lead_Count__c, LC_Lead_Company__r.Domain__c FROM Lead WHERE ID = :LD2.id];

        System.assertNotEquals( LD2.LC_Lead_Company__c,     null );
        System.assertNotEquals( LD2.LC_Lead_Company__r.ID,    LD1.LC_Lead_Company__r.ID );
        System.assertEquals( LD2.LC_Lead_Company__r.Name,   LD2.Company );
        System.assertEquals( LD1.LC_Lead_Company__r.Name,   LD1.Company );
        System.assertEquals( LD2.LC_Lead_Company__r.Status__c,  LD2.Status );
        System.assertEquals( LD1.LC_Lead_Company__r.Status__c,  LD1.Status );
        System.assertEquals( LD2.LC_Lead_Company__r.Lead_Count__c, 1 );
        System.assertEquals( LD1.LC_Lead_Company__r.Lead_Count__c, 1 );
     
        //Update LD1 Email
        LD1.Email = 'LD1@LCB.com';
        LD1.Status = 'Close - Qualified';
        Update LD1;

        //Verify LCB Create & LCA Update
        LD1 = [SELECT ID, Company, Status, OwnerID, LC_Lead_Company__c, LC_Lead_Company__r.Name, LC_Lead_Company__r.OwnerId, LC_Lead_Company__r.Status__c, LC_Lead_Company__r.Lead_Count__c, LC_Lead_Company__r.Domain__c FROM Lead WHERE ID = :LD1.id];
        LD2 = [SELECT ID, Company, Status, OwnerID, LC_Lead_Company__c, LC_Lead_Company__r.Name, LC_Lead_Company__r.OwnerId, LC_Lead_Company__r.Status__c, LC_Lead_Company__r.Lead_Count__c, LC_Lead_Company__r.Domain__c FROM Lead WHERE ID = :LD2.id];

        System.assertEquals( LD1.LC_Lead_Company__r.ID,    LD2.LC_Lead_Company__r.ID );
        System.assertEquals( LD1.LC_Lead_Company__r.Name,   LD2.Company );
        System.assertEquals( LD1.LC_Lead_Company__r.Status__c,  LD1.Status );
        System.assertNotEquals( LD1.LC_Lead_Company__r.Status__c,  LD2.Status );
        System.assertEquals( LD1.LC_Lead_Company__r.Lead_Count__c, 2 );

        //Update LD1 Owner
        LD1.OwnerID = u2.ID;
        Update LD1;

        //Verify LCB Owner Change
        LD1 = [SELECT ID, Company, Status, OwnerID, LC_Lead_Company__c, LC_Lead_Company__r.Name, LC_Lead_Company__r.OwnerId, LC_Lead_Company__r.Status__c, LC_Lead_Company__r.Lead_Count__c, LC_Lead_Company__r.Domain__c FROM Lead WHERE ID = :LD1.id];
        LD2 = [SELECT ID, Company, Status, OwnerID, LC_Lead_Company__c, LC_Lead_Company__r.Name, LC_Lead_Company__r.OwnerId, LC_Lead_Company__r.Status__c, LC_Lead_Company__r.Lead_Count__c, LC_Lead_Company__r.Domain__c FROM Lead WHERE ID = :LD2.id];

        System.assertEquals( LD1.OwnerID,    u2.ID );
        System.assertEquals( LD1.OwnerID,    LD1.LC_Lead_Company__r.OwnerID );
        System.assertEquals( LD2.OwnerID,    LD2.LC_Lead_Company__r.OwnerID );

        //Update LD1 Company
        LD1.Company = 'LCBeta';
        Update LD1;

        //Verify LCB Name Change
        LD1 = [SELECT ID, Company, Status, OwnerID, LC_Lead_Company__c, LC_Lead_Company__r.Name, LC_Lead_Company__r.OwnerId, LC_Lead_Company__r.Status__c, LC_Lead_Company__r.Lead_Count__c, LC_Lead_Company__r.Domain__c FROM Lead WHERE ID = :LD1.id];
        LD2 = [SELECT ID, Company, Status, OwnerID, LC_Lead_Company__c, LC_Lead_Company__r.Name, LC_Lead_Company__r.OwnerId, LC_Lead_Company__r.Status__c, LC_Lead_Company__r.Lead_Count__c, LC_Lead_Company__r.Domain__c FROM Lead WHERE ID = :LD2.id];

        System.assertEquals( LD2.Company,    LD2.LC_Lead_Company__r.Name );
        System.assertEquals( LD2.Company,    LD1.Company );
     
        //Delete LD1
        Delete LD1;

        //Verify LCB Count & Status
        LD2 = [SELECT ID, Company, Status, OwnerID, LC_Lead_Company__c, LC_Lead_Company__r.Name, LC_Lead_Company__r.OwnerId, LC_Lead_Company__r.Status__c, LC_Lead_Company__r.Lead_Count__c, LC_Lead_Company__r.Domain__c FROM Lead WHERE ID = :LD2.id];     

        System.assertEquals( LD2.LC_Lead_Company__r.Status__c,  LD2.Status );
        System.assertEquals( LD2.LC_Lead_Company__r.Lead_Count__c, 1 );

        //Create LD3
        Lead LD3 = new Lead(
            LastName = 'LD3',
            Email = 'LD3@LCB.com',
            Company = 'LCB',
            Status = 'Closed - Qualified',
            OwnerID = u2.ID
        );
        Insert LD3;

  //Convert LD3
        Database.LeadConvert lc = new database.LeadConvert();
  lc.setLeadId(LD3.id);
  lc.setDoNotCreateOpportunity(true);
  lc.setConvertedStatus('Closed - Qualified');
     
        //Verify LD3 Convert
  Database.LeadConvertResult lcr = Database.convertLead(lc);
  System.assert(lcr.isSuccess());

  //Verify LD2 Convert
        LD3 = [SELECT ID, IsConverted, ConvertedAccountId FROM Lead WHERE ID = :LD3.id];     
        LD2 = [SELECT ID, IsConverted, ConvertedAccountId FROM Lead WHERE ID = :LD2.id];     
        System.AssertEquals(LD2.IsConverted,   TRUE);
        System.AssertEquals(LD2.ConvertedAccountId, LD3.ConvertedAccountId);
     
    }
}