Skip to main content
I have a custom object Event (to track happy hours, seminars, etc.) and a custom junction object Event Relation where I can associated Leads to Events. When a Lead is converted, I have written this code to automatically populate Account, Contact and Opportunity on the Event Relation junction object.

trigger EventLeadConvert on Lead (after update) {

    Map<Id, Id> populateaccount = new Map<Id, Id>();

    For (Lead l: Trigger.new)

       {

           if (l.isConverted)

           {

               populateaccount.put(l.Id, l.convertedAccountId);

           }

       }

     List<Event_Relation__c> EventAccount = [select lead__c, client__c from Event_Relation__c where lead__c in :populateaccount.keySet()];

     for (Event_Relation__c e : EventAccount)

     {

         e.client__c = populateaccount.get(e.lead__c);

     }

    update EventAccount;

    

    

    Map<Id, Id> populatecontact = new Map<Id, Id>();

    For (Lead l: Trigger.new)

       {

           if (l.isConverted)

           {

               populatecontact.put(l.Id, l.convertedContactId);

           }

       }

     List<Event_Relation__c> EventContact = [select lead__c, contact__c from Event_Relation__c where lead__c in :populatecontact.keySet()];

     for (Event_Relation__c e : EventContact)

     {

         e.contact__c = populatecontact.get(e.lead__c);

     }

    update EventContact;

    

    

    Map<Id, Id> populateopportunity = new Map<Id, Id>();

    For (Lead l: Trigger.new)

       {

           if (l.isConverted)

           {

               populateopportunity.put(l.Id, l.convertedOpportunityId);

           }

       }

     List<Event_Relation__c> EventOpportunity = [select lead__c, opportunity__c from Event_Relation__c where lead__c in :populateopportunity.keySet()];

     for (Event_Relation__c e : EventOpportunity)

     {

         e.opportunity__c = populateopportunity.get(e.lead__c);

     }

    update EventOpportunity;

}

I am having trouble with the test class; I am only getting 70% because the populating of the 3 fields after conversion isn't happening. Can someone help get full coverage?

@IsTest(seealldata=true)

public class TEST_EventLeadConvert {

    

    Static TestMethod void EventLeadConvert(){

        Lead testLead = new Lead(

            FirstName = 'TestFirst',

            LastName = 'TestLast',

            Company = 'TestCompany',

            Status = 'Open',

            LeadSource = 'Advertisement',

            Group__c = 'TestGroup'

        );

        insert  testLead;

        

        Event__c testEvent = new Event__c(

            Name = 'TestEvent'

        );

        insert  testEvent;

        Event_Relation__c testER = new Event_Relation__c(

            Event__c = testEvent.Id,

            Lead__c = testLead.Id

        );

        insert testER;

        

    Test.StartTest();

        Database.LeadConvert lc = new database.LeadConvert();

        lc.setLeadId(testLead.Id);

    Test.StopTest();

    }

}

Any help is much appreciated.
2 个回答
0/9000