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

Trouble Setting RecordType in Trigger

I am setting the recordType of a newly created recorded in apex.


I have it inserting the new opportunity and am receiving an error on this line:


opportunity.recordtype = chosenRecType;


chosenRecType is a value passed to the controller via javascript.


The error is: Error: Compile Error: Illegal assignment from String to SOBJECT:RecordType


Is there a way to convert this string to be read properly?




I think you'll need to set the recordtypeid rather than the record type itself.


Lookup the  record type using something like:



id rectypeid = [SELECT r.Id

                            FROM RecordType r 

                           WHERE r.Name  =:chosenRecType and r.SobjectType = 'Opportunity'][0].ID;



opportunity.recordtypeid = chosenRecType;