+ Start a Discussion
kshannonkshannon 

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?

 

Thanks.

bob_buzzardbob_buzzard

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;

 

EnryEnry

opportunity.recordtypeid = chosenRecType;

 

Br