1

We have a unique business request to have a lead duplicated when it is converted in order for a secondary sales rep try selling to the lead. below is the code that grabs the lead when it falls into a specific category, and re-creates the lead and assigns it to a secondary user ID already specified on the Lead record.

How can I test that this second record is create when there is no relation to the first one?

private void leadDupConvert()
{


    List<Lead> LeadsToDup = new list<Lead>(); 
    List<Lead> duplicatedLead = new list <Lead>(); 

    for( Lead myLead : (List<Lead>)Trigger.new)
    {
    Lead oldLeadDetails = ((Map<Id, Lead>)Trigger.oldMap).get(myLead.ID);        
    if(myLead.Dual_Intent__c == TRUE && myLead.IsConverted && myLead.Lead_Secondary_Owner_ID__c != NULL && !oldLeadDetails.isConverted && firstRun) 
    {
       LeadsToDup.add(myLead); 
    }
    }
  if(LeadsToDup.size() > 0)
  {        
    for(Lead lea: LeadsToDup)
    {
        Lead l = new Lead();
        l.FirstName = lea.FirstName;
        l.LastName = lea.LastName;
        l.Company = lea.Company;
        l.Email = lea.Email;
        l.Phone = lea.Phone;

        l.Dual_Intent__c = FALSE; 
        l.OwnerId = lea.Lead_Secondary_Owner_ID__c;
        l.Lead_Assigned__c = TRUE;
        l.Status = 'Open';
        l.RecordTypeId = lea.RecordTypeId;

        duplicatedLead.add(l);
    }

    if(duplicatedLead.size()>0)
     {
        insert duplicatedLead;
     }        
    }
}

So far the test code portion I have is as follows:

Database.LeadConvert le = new database.LeadConvert();
    le.setLeadId(lea.id);
    le.setDoNotCreateOpportunity(false);
    le.setConvertedStatus('Existing Opportunity');

Database.LeadConvertResult lcr = Database.convertLead(le);
    System.assert(lcr.isSuccess());

Can't figure out anything after this...

1 Answer 1

0

Just query for the latest lead; assuming you're using data isolation, you'll find your duplicate.

Lead dupLead = [SELECT Id FROM Lead ORDER BY Id DESC LIMIT 1];
System.assertNotEquals(lea.Id, dupLead.Id);

Your code would be easier to maintain if you just cut out all the fluff in your code. Here's how I'd write it:

// On First run
if(firstRun) {
    // Trigger.old and Trigger.new as Lead[]
    Lead[] oldL = (Lead[])Trigger.old, newL = (Lead[])Trigger.new;
    // Leads to duplicate
    Id[] dupId = new Id[0];
    // Much faster loop
    for(Integer index = 0, size = Trigger.old.size(); index < size; index++) {
        Lead oldR = oldL[index], newR = newL[index];
        if(newR.Dual_Intent__c && !oldR.IsConverted && newR.IsConverted &&
           newR.Lead_Secondary_Owner_Id__c != null) {
        dupId.add(newR.Id);
    }
    // We have work to do
    if(!dupId.isEmpty()) {
        // Query and clone in memory without Id
        Lead[] dupLeads = [SELECT FirstName, LastName, Company, Email, Phone,
                                  Lead_Secondary_Owner_Id__c, RecordTypeId
                           FROM Lead WHERE Id IN :dupId]
                           .deepClone(false, false, false, false);
        // Set additional fields
        for(Lead dupLead: dupLeads) {
            dupLead.OwnerId = dupLead.Lead_Secondary_Owner_Id__c;
            dupLead.Assigned__c = true;
            dupLead.Dual_Intent__c = false;
            dupLead.Status = 'Open';
        }
        // Create new records
        insert dupLeads;
    }
}
3
  • Thanks. Although I'm still hitting "Too many SOQL queries: 101" for the limit 1 sql query "Lead dupLead = [SELECT Id FROM Lead ORDER BY Id DESC LIMIT 1];" Commented Nov 7, 2016 at 15:03
  • @dannycheeko In live code, or just your unit test?
    – sfdcfox
    Commented Nov 7, 2016 at 15:26
  • The lead in the test code is created and converted, but I think the query fires too soon before the duplicate lead can be created, so I keep getting errors on assertions. Test.startTest(); Lead dupLead = [SELECT Id, PrimeLeadID__c, OwnerID, Dual_Intent__c FROM Lead ORDER BY Id DESC LIMIT 1]; System.assertNotEquals(lea.Id, dupLead.Id); <--- WORKS System.assertEquals(lea.Lead_Secondary_Owner_ID__c, dupLead.OwnerID); <--- Doesn't Work System.assertEquals(False, dupLead.Dual_Intent__c); Test.stopTest(); Commented Nov 9, 2016 at 15:48

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .