function readOnly(count){ }
Starting November 20, the site will be set to read-only. On December 4, 2023,
forum discussions will move to the Trailblazer Community.
+ Start a Discussion
Israel Silvestre 9Israel Silvestre 9 

Updating a field in Contact after a lead is converted with created Opportunity

Hi All,

I'm quite new into development and I was looking for answer. We need to have a particular picklist field(Lead_Status__c) in Contact be updated to "Qualified" after the lead was converted with an Opportunity (during conversion of the lead, optionally adding new Opportunity). 

We tried to use workflow and process builder but it end up as "false" in the debug log, so the option is via apex. I manage to do some search in sites and found this code:
 
Trigger Lead on Lead (after update) {

    if (Trigger.isAfter) {

        if (Trigger.isUpdate) { 

            Map<ID,ID> oppToContact = new Map<ID,ID>(); 

            for (Lead ld : Trigger.new) {
                if(ld.isConverted && trigger.oldMap.get(ld.id).isConverted) continue; //only process newly converted leads

                // Find all converted Leads with Opportunitiy and add ConvertedOpportunityId to setConvertedOppIds
                if (ld.ConvertedOpportunityId != null && ld.ConvertedContactId != null){                    
                    oppToContact.put(ld.ConvertedOpportunityId,ld.ConvertedContactID);  

                }

            }
I might need to find the additional code to add here to update the Lead Status in Contact if this particular record was converted from a lead and with added opportunity upon conversion, can anyone please help me on this code? Im quite new in this development and would really like to explore further. Please see image below of the supposed field to update when the Lead converted to Contact and was added opportunity during conversion. field name is Lead_Status__c (picklist field)


User-added image


Thank you for the answer

Israel
Best Answer chosen by Israel Silvestre 9
Amit Chaudhary 8Amit Chaudhary 8
Please update your code like below
Trigger Lead on Lead (after update) 
{
    if (Trigger.isAfter) 
    {

        if (Trigger.isUpdate) 
        { 
	    Set<Id> setContact = new Set<Id>();
	    Set<Id> setOpp = new Set<Id>();

	    //Map<ID,ID> oppToContact = new Map<ID,ID>(); 

            for (Lead ld : Trigger.new) 
            {
                // Find all converted Leads with Opportunitiy and add ConvertedOpportunityId to setConvertedOppIds
                if ( ld.isConverted && ld.ConvertedOpportunityId != null && ld.ConvertedContactId != null)
                {   
		    setContact.add(ld.ConvertedContactId );
		    setOpp.add(ld.ConvertedOpportunityId );
                    //oppToContact.put(ld.ConvertedOpportunityId,ld.ConvertedContactID);  
                }
            }

	    if(setContact.size() > 0 )
            {
		List<Contact> lstContact = [select id , Lead_Status__c from contact where id in:setContact];
		for(Contact cont : lstContact)
		{
			cont.Lead_Status__c ='Qualified';
		}
		update lstContact ;
            }


        }
    }

}

 

All Answers

Amit Chaudhary 8Amit Chaudhary 8
Please try to update your code like below
Trigger Lead on Lead (after update) 
{
    if (Trigger.isAfter) 
    {

        if (Trigger.isUpdate) 
        { 
	    Set<Id> setContact = new Set<Id>();
	    Set<Id> setOpp = new Set<Id>();

	    //Map<ID,ID> oppToContact = new Map<ID,ID>(); 

            for (Lead ld : Trigger.new) 
            {
                // Find all converted Leads with Opportunitiy and add ConvertedOpportunityId to setConvertedOppIds
                if ( ld.isConverted && ld.ConvertedOpportunityId != null && ld.ConvertedContactId != null)
                {   
		    setContact.add(ld.ConvertedContactId );
		    setOpp.add(ld.ConvertedOpportunityId );
                    oppToContact.put(ld.ConvertedOpportunityId,ld.ConvertedContactID);  
                }
            }

	    if(setContact.size() > 0 )
            {
		List<Contact> lstContact = [select id , Lead_Status__c from contact where id in:setContact];
		for(Contact cont : lstContact)
		{
			cont.Lead_Status__c ='Qualified';
		}
		update lstContact ;
            }


        }
    }

}

Let us know if this will help you
 
Israel Silvestre 9Israel Silvestre 9
Hi Amit,
Thank you for your reply, I tried to check the code you've provided but encounter an error:

Error: Compile Error: Variable does not exist: oppToContact at line 20 column 21
line 20:  oppToContact.put(ld.ConvertedOpportunityId,ld.ConvertedContactID);

User-added image

Any suggestions?

Thanks,

Israel
Amit Chaudhary 8Amit Chaudhary 8
Please update your code like below
Trigger Lead on Lead (after update) 
{
    if (Trigger.isAfter) 
    {

        if (Trigger.isUpdate) 
        { 
	    Set<Id> setContact = new Set<Id>();
	    Set<Id> setOpp = new Set<Id>();

	    //Map<ID,ID> oppToContact = new Map<ID,ID>(); 

            for (Lead ld : Trigger.new) 
            {
                // Find all converted Leads with Opportunitiy and add ConvertedOpportunityId to setConvertedOppIds
                if ( ld.isConverted && ld.ConvertedOpportunityId != null && ld.ConvertedContactId != null)
                {   
		    setContact.add(ld.ConvertedContactId );
		    setOpp.add(ld.ConvertedOpportunityId );
                    //oppToContact.put(ld.ConvertedOpportunityId,ld.ConvertedContactID);  
                }
            }

	    if(setContact.size() > 0 )
            {
		List<Contact> lstContact = [select id , Lead_Status__c from contact where id in:setContact];
		for(Contact cont : lstContact)
		{
			cont.Lead_Status__c ='Qualified';
		}
		update lstContact ;
            }


        }
    }

}

 
This was selected as the best answer
Israel Silvestre 9Israel Silvestre 9
Thanks Amit, it's working perfectly now
Rafael Sanchez 4Rafael Sanchez 4
Has someone created the test class for this code: I have some issues deploying to production from SandBox:

trigger Lead on Lead (before insert, before update) {
//Reference: https://developer.salesforce.com/forums/?id=906F000000093JmIAI
        
    if (Trigger.isBefore) {
        
        if (Trigger.isUpdate) { 
                    
            Set<Id> setLeadIds     = new Set<Id>();  
            Set<Id> setConvertedOppIds   = new Set<Id>();  
            
            for (Lead ld : Trigger.new) {

                // Find all converted Leads with Opportunitiy and add ConvertedOpportunityId to setConvertedOppIds
                if (ld.isConverted && ld.ConvertedOpportunityId != null 
                    && Trigger.oldMap.get(ld.Id).ConvertedOpportunityId != ld.ConvertedOpportunityId) 
                    setConvertedOppIds.add(ld.ConvertedOpportunityId);          
            }       

            if (!Statics.inFutureContext) {
                if (!setConvertedOppIds.isEmpty()) OpportunityTeamProcessor.createOpportunityTeamMember(setConvertedOppIds);
            }               
        }       
    }   
}