+ Start a Discussion

adding value to the record type

I am using Visualforce and APEX to save records for a Custom object Obj__c. I would like to add the recordtype in the save. Could someone tell me how to do so. Currently I am trying

Obj__c o = new Obj__c(f1__c = 'xxxx', f2__c = 'xxxx', RecordType.Name = 'xxxx');

Insert o;

I get some errorrs "Error: Compile Error: Invalid field initializer: RecordType.Name".

Please help me, how can I insert the recordtype name.



Best Answer chosen by Admin (Salesforce Developers) 
You need to supply it with the ID for the record type you want.  The Obj__c field will be called recordTypeId and you can query first to find the appropriate id value from the RecordType table, perhaps like [select id, name from RecordType where name = 'xxx']