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
Travis WrightTravis 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;
            }
        }
    }
}
Sforce.NinjaSforce.Ninja
First off kudos to a very well written code. Second, the only problem I can see in your code is if the orc.Contact.Lead_Source_Name__c is NULL. For the  ocr.Contact.LeadSource, the NULL value was handled in the query AND Contact.LeadSource != null.
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?
 
Travis WrightTravis Wright
I will have to confirm but I believe that the Lead_Source_Name__c will always have a value. I am a little confused because when testing this I made sure there was a value in the Lead_Source_Name__c. Do I have to add the Lead_Source_Name__c to the Query to handle NULL? Sorry for the follow up just trying to remember everything from my development class and it is not coming back a quick as I would like. 
Travis WrightTravis Wright
I just confirmed that Lead_Source_Name__c can be balnk so adding it to the Query in the same mannor would not be possible. I am stuck now, my SOQL is a little off and I am not sure how I can add this to the Query any help would be appreciated.