You need to sign in to do that
Don't have an account?
Israel 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:
Thank you for the answer
Israel
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)
Thank you for the answer
Israel
All Answers
Let us know if this will help you
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);
Any suggestions?
Thanks,
Israel
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);
}
}
}
}