You need to sign in to do that
Don't have an account?
Travis Wright
Auto Fill lead source on Opp from OCR
As many companies run into issues with lead source and Sales Changing this on opportunities I have been looking for a Trigger that will allow me to auto populate it if it is blank. I plan on removing from the page and allowing the trigger to update it as it will always be blank when an opportunity is created.
Are sales Reps are forced to create an opportunity from the Contact which gives us the Opportunity Contact Role, ORC. I have the trigger working with lead source but I was asked to add a custom field in it as well and I think I am missing something. Here is the code that I am using if anyone can help that would be great. Custom Field Name is Lead_Source_Name__c.
trigger fillLeadSource on Opportunity (before update) {
Set<Id> oppsToFill = new Set<Id>();
for(Opportunity o : trigger.new){
if(o.LeadSource == null) {
oppsToFill.add(o.Id);
}
}
// Now we'll select all possible contacts wasting only 1 query.
if(!oppsToFill.isEmpty()){
List<OpportunityContactRole> roles = [SELECT OpportunityId, Contact.Name, Contact.LeadSource, Contact.Lead_Source_Name__c
FROM OpportunityContactRole
WHERE isPrimary = true AND Contact.LeadSource != null AND OpportunityId IN :oppsToFill];
if(!roles.isEmpty()){
for(OpportunityContactRole ocr : roles){
Opportunity oppToBeFilled = trigger.newMap.get(ocr.OpportunityId);
System.debug('Changing lead source on ' + oppToBeFilled.Name + ' from ' + oppToBeFilled.LeadSource + ' to ' + ocr.Contact.LeadSource + ' (thx to ' + ocr.Contact.Name + ' being the Primary Contact).');
oppToBeFilled.LeadSource = ocr.Contact.LeadSource;
oppToBeFilled.Lead_Source_Name__c = orc.Contact.Lead_Source_Name__c;
}
}
}
}
Are sales Reps are forced to create an opportunity from the Contact which gives us the Opportunity Contact Role, ORC. I have the trigger working with lead source but I was asked to add a custom field in it as well and I think I am missing something. Here is the code that I am using if anyone can help that would be great. Custom Field Name is Lead_Source_Name__c.
trigger fillLeadSource on Opportunity (before update) {
Set<Id> oppsToFill = new Set<Id>();
for(Opportunity o : trigger.new){
if(o.LeadSource == null) {
oppsToFill.add(o.Id);
}
}
// Now we'll select all possible contacts wasting only 1 query.
if(!oppsToFill.isEmpty()){
List<OpportunityContactRole> roles = [SELECT OpportunityId, Contact.Name, Contact.LeadSource, Contact.Lead_Source_Name__c
FROM OpportunityContactRole
WHERE isPrimary = true AND Contact.LeadSource != null AND OpportunityId IN :oppsToFill];
if(!roles.isEmpty()){
for(OpportunityContactRole ocr : roles){
Opportunity oppToBeFilled = trigger.newMap.get(ocr.OpportunityId);
System.debug('Changing lead source on ' + oppToBeFilled.Name + ' from ' + oppToBeFilled.LeadSource + ' to ' + ocr.Contact.LeadSource + ' (thx to ' + ocr.Contact.Name + ' being the Primary Contact).');
oppToBeFilled.LeadSource = ocr.Contact.LeadSource;
oppToBeFilled.Lead_Source_Name__c = orc.Contact.Lead_Source_Name__c;
}
}
}
}
So if the LeadSource comes as NULL but Lead_Source_Name__c is not NULL, such records might not come up.
The question I have is, is that a likely scenario? Or will the LeadSource and Lead_Source_Name__c will also be filled with values?