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
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.
Hjams HaksHjams Haks

Thank you for sharing this valuable information on mapping Lead Record Type to Opportunity Record Type. The need for an effective solution in this area is evident for many Salesforce developers, and the contributions here provide valuable insights.
I'd like to add a couple of pointers:
Bulk Processing: While the provided triggers seem to work well for individual records, always ensure that they are bulkified to handle the processing of multiple records simultaneously, especially in scenarios involving data migrations or mass updates.
Error Handling: Always include robust error handling in your triggers to manage exceptions. This ensures that your trigger doesn't fail silently, leaving users confused.
Unit Testing: Before deploying any trigger to production, it's essential to have comprehensive test classes written that cover various scenarios to ensure the trigger works as expected. Aim for at least 90% code coverage and make.

There is Another Version for PC as well you can check it on (https://carparkapk.com/car-parking-multiplayer-mod-apk-for-pc/