Lead Conversion Field Mapping Standard to Custom Fields (Configuration & Apex Trigger)
Вставка
- Опубліковано 14 кві 2019
- r/Salesforce user asked the question -
"Can someone help me create this trigger?
When I convert a lead to a contact, I want the standard "Rating" picklist field from the lead to carry over to the custom picklist field for the contact (Lead_Rating__c)."
Yes, this can be done in multiple ways. Configuration and Code.
Config Solution: 3:45
Code Solution: 12:45
/ c. .
Comment on what video you would like me to make next. Recommended Books
Advanced Apex Programming amzn.to/2X8hZb4
Clean Code amzn.to/2X8iaTM - Наука та технологія
Hi.. when a lead is converted. it will show a flag once the convertion is done... Can we able to change the flag logo to someother logo? If so how?
Unsure. This may need to be a custom formula field you add with an image.
Will this work when you convert a lead to an existing person account? will the lead "Lead - Rating.." field update the "Rating" field on the Person Account, if a value already exists
This would overwrite values on the person account unless additional logic is added.
Hi if I then wanted that rating to show up on any opportunity associated with that contact how would I do that? Thank you!
It would basically be replicating the contact trigger for opportunity/.
please help me sir.. i have one doubt
in programming.
how to slove this program.gettin errors
create a trigger on Lead.
--> When you convert leads to contacts or accounts, the process sometimes creates duplicate records.
-->Use Contact field to query the related Lead and then merge them with the Contact.
public class DuplicateLeadsWithAccAndContact {
public static void DulpicateMergeLeads(List leadList){
List nameList = new List();
List phoneList = new List();
List emailList = new List();
Set convertedContactIdSet = new Set();
for(Lead listOfLead : leadList){
System.debug('>>>>>listOfLead>>>'+listOfLead);
nameList.add(listOfLead.Name);
phoneList.add(listOfLead.Phone);
emailList.add(listOfLead.Email );
convertedContactIdSet.add(listOfLead.ConvertedContactId);
}
}
public static void convertLeadToContact(List leadList){
// List to store names
List nameList = new List();
// List to store phone numbers
List phoneList = new List();
// List to store email address
List emailList = new List();
// Collect set of ContactIdSet
Set convertedContactIdSet = new Set();
// Iterate through each Lead and add their email and phone number to their respective List
for(Lead leadObj : leadList){
// check every lead in trigger.new that, lead convert status(isconverted).
if(leadObj.IsConverted){
nameList.add(leadObj.Name);
phoneList.add(leadObj.Phone);
emailList.add(leadObj.Email);
convertedContactIdSet.add(leadObj.ConvertedContactId);
}
}
Map emailContactMap = new Map();
System.debug('>>>>>emailContactMap>>>'+emailContactMap);
Map emailConvertedContactMap = new Map();
System.debug('>>>>>emailConvertedContactMap>>>'+emailConvertedContactMap);
Map phoneContactMap = new Map();
System.debug('>>>>>phoneContactMap>>>'+phoneContactMap);
Map phoneConvertedContactMap = new Map();
System.debug('>>>>>phoneConvertedContactMap>>>'+phoneConvertedContactMap);
for(Contact listOfContact : [SELECT Id, Email,Phone
FROM Contact
WHERE Email IN: emailList AND Phone IN:phoneList]){
System.debug('>>>>>listOfContact>>>'+listOfContact);
if(listOfContact.Email != null){
emailContactMap.put(listOfContact.Email, listOfContact);
}
if(convertedContactIdSet.contains(listOfContact.Id)){
emailConvertedContactMap.put(listOfContact.Email, listOfContact);
}
if(listOfContact.Email != null){
phoneContactMap.put(listOfContact.Phone, listOfContact);
}
if(convertedContactIdSet.contains(listOfContact.Id)){
phoneConvertedContactMap.put(listOfContact.Phone, listOfContact);
}
}
for(Lead leadObj : leadList){
if(leadObj.isConverted && leadObj.Email != null && emailContactMap.containsKey(leadObj.Email)
&& emailConvertedContactMap.containsKey(leadObj.Email)){
try{
merge emailContactMap.get(leadObj.Email) emailConvertedContactMap.get(leadObj.Email);
}catch (DmlException e) {
System.debug('An unexpected error has occurred: ' + e.getMessage());
}
}
if(leadObj.isConverted && leadObj.Phone != null && phoneContactMap.containsKey(leadObj.Phone)
&& phoneConvertedContactMap.containsKey(leadObj.Phone)){
try{
merge phoneContactMap.get(leadObj.Phone) phoneConvertedContactMap.get(leadObj.Phone);
}catch (DmlException e) {
System.debug('An unexpected error has occurred: ' + e.getMessage());
}
}
Database.LeadConvert leadConverterObj = new Database.LeadConvert();
if (leadObj.isConverted ==true){
leadConverterObj.setLeadId(leadObj.Id);
//lc.setDoNotCreateOpportunity(True);
LeadStatus convertStatus = [SELECT Id, MasterLabel
FROM LeadStatus
WHERE IsConverted=true LIMIT 1];
leadConverterObj.setConvertedStatus(convertStatus.MasterLabel);
Database.LeadConvertResult leadConverterObjResult = Database.convertLead(leadConverterObj);
System.assert(leadConverterObjResult.isSuccess());
}
}
}
}
trigger.new not Trigger.now
Good catch