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
Harry JohnsonHarry Johnson 

Set Opportunity field from Lead on Lead Conversion

Hi Everyone,

When creating an opportunity via lead conversion, I am trying to set the opportunity Primary Campaign Source field as a campaign entered on the lead, with a trigger.

My code is: 
 
trigger UpdatePCS on Lead (after update) {

if (trigger.old[0].isConverted == false && trigger.new[0].isConverted == true)

    if (Trigger.new[0].ConvertedOpportunityId != null) {
    
        Opportunity opp = [SELECT Id FROM Opportunity WHERE Id = :Trigger.new[0].ConvertedOpportunityId];
        opp.CampaignId = trigger.old[0].Primary_Campaign_Source__c;
        
        }
        
    }

No errors, but it fails to fill in the Primary Campaign Source with anything at all. I have tried a few variations, all resulting in errors.

If anyone can help me it would be great, this is my first trigger, and im a little lost..

Thanks.
ManojjenaManojjena
Hi  Harry Johnson,

I think we can use standard functionality to achive this ,Do one thing go to Setup>Customize>Lead >Lead  Mapping (Nead Custom Field) check if you can Map with Primary_Campaign_Source__c with CampainId Of Opportunity .

Please let me know any issue .
 
Harry JohnsonHarry Johnson
Hi Manoj,

Thanks for your answer - unfortunately it seems that the standard functionality maps the most recently associated campaign, but we need to map a specific campaign entered on the lead into the Primary Campaign Source field. Also, the field on the lead which we need to carry over to the opportunity has no option to map to the opportunity Primary Campaign Source field.

I have noticed an issue with my code above - this:
 
Opportunity opp = [SELECT Id FROM Opportunity WHERE Id =:Trigger.new[0].ConvertedOpportunityId];
        opp.CampaignId = trigger.old[0].Primary_Campaign_Source__c;

is assigning the Id from the conversion opportunity, and then attempting to pass it to the Primary Campaign Source, which is definitely wrong.

How can I assign the the conversion opportunity to the variable 'opp', so that I can access the Primary Campaign Source field to assign the lead field?