+ Start a Discussion
JD SpragueJD Sprague 

Lead Record Type to Opportunity Record Type Apex Trigger

I am looking to map the Lead Record Type to the Opportunity Record Type when I convert the lead. Right now the Opportunity Record Type populates with a default value. I have only written a few apex triggers as of now and am still new to salesforce, I appreciate any help! 
Best Answer chosen by JD Sprague
SonamSonam (Salesforce Developers) 
You can try the following:
1)(Can be done using the out-of-box functionality: Create a custom field on Lead object which stores the value of lead record type and map it to a custom field on the Opportunity side such that it stored the value of the lead record type
2)Write a before insert trigger on opportunity such that it looks at this value of the custom field and sets the record type of the opportunity

All Answers

SonamSonam (Salesforce Developers) 
You can try the following:
1)(Can be done using the out-of-box functionality: Create a custom field on Lead object which stores the value of lead record type and map it to a custom field on the Opportunity side such that it stored the value of the lead record type
2)Write a before insert trigger on opportunity such that it looks at this value of the custom field and sets the record type of the opportunity
This was selected as the best answer
JD SpragueJD Sprague
Is there anyway to do this all within a trigger? Here is my current trigger:

trigger AddPrimaryContactToConvertedOpp on Lead (After Update) {


// THIS TRIGGER WILL OVERWRITE ANY CONTACT DEFINED IN THE CUSTOM FIELD CONTACT__C ON THE OPPORTUNITY OBJECT.
// SET THIS FIELD TO READ ONLY OR CHANGE THE FUNCTIONALITY BELOW TO AVIOD DATA BEING OVERWRITTEN BY MISTAKE...

// [1] Build list of Oppos to update
List<Opportunity> oUpdList = new List<Opportunity> ();
for (Lead l : Trigger.new) 
    if (l.IsConverted && l.convertedOpportunityId != null)
        oUpdList.add(new Opportunity(id = l.convertedOpportunityId, name = l.name));

// [2] Update the converted Oppos
update oUpdList;  // could be Database.update(oUpdList,false) if you want partial successes
}


this trigger updates a field in the new opportunity but will not work with the recordtype for some reason.
Chidanand MChidanand M
Hi JD Sprague,

Here is the Sample Code,
 
trigger assignRecordType on Opportunity (before insert) {
    public string recName;

    for(Opportunity o:Trigger.new){
    
        recName=o.Rec_Type__c;
        
        if(recName!=NULL){
    
            RecordType rt = [SELECT Id FROM RecordType WHERE SObjectType='Opportunity' AND Name=:recName LIMIT 1];
            o.RecordTypeId =  rt.Id;
             
        }     
    
    }  

}

 
Sam Smith 70Sam Smith 70
Our ghostwriting services are also suitable for everything from high school students who need to submit an excellent essay for their assignment through to someone seeking to get their paper or manuscript published capstone project help (https://www.capstoneproject.net/). We offer support with highly professional academic ghostwriting which is able to assist you at any point within your education or career in any subject area. Through us you can get support with all of the following tasks
Waleed Khan 8Waleed Khan 8
I need a perfect trigger. I tried many triggers (Best & Fastest (https://carparkingmultiplayerapk.com/best-fastest-car-in-pixel-car-racer/)) but failed. Can anyone help in this regard? Anyone plz.