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
MellowYellowMellowYellow 

Trying to populate the RecordType field when creating an Opportunity with Apex trigger

I am creating a new Opportunity record with an Apex trigger.  I'm able to set the field values on the new Opportunity, except for the RecordType, which is a picklist containing 2 values.  I have the id value for the picklist value, I'm just not able to set it.

 

Thanks

Best Answer chosen by Admin (Salesforce Developers) 
Rahul SharmaRahul Sharma

Have you tried this:

 

Opportunity objOpportunity = new Opportunity();
objOpportunity.RecordtypeId = [Select Id from recordtype where sObject = 'Opportunity' and name='Renewal'].Id;

 Replace 'renewal ' with your recordtype name.

Hope it helps.