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
Gheorghe Sima 11Gheorghe Sima 11 

Record Type

Hi,
I have a visulforce page where I create an Account record and in the Controller I set the RecordType for that record, but when I try to insert the record in the database I recive an error message : Attempt to de-reference a null object.
This is my code:

  result.RecordType.Name='Persoane fizice';
  insert result;

Can anybody tell me how can i set the recordtype name?
 
Best Answer chosen by Gheorghe Sima 11
Arunkumar RArunkumar R
Hi Gheroghe,

First get your desired record type id by using the below format,
 
Id accountRecordTypeId = Schema.SObjectType.Account.getRecordTypeInfosByName().get('Record Type Name').getRecordTypeId()
Mention your object name and record type name in the above syntax.

While inserting account record use like below,
 
// Here you can mention all required values.
Account acc = new Account();
acc.RecordTypeId = accountRecordTypeId;
insert acc;

All Answers

Shailendra Singh ParmarShailendra Singh Parmar
Hi Gheorghe,
you can't directly assign RT name instead need to assign ID. So something like following will work.

//query Record type 
RecordType rt = [Select ID from RecordType where Name = 'Persoane fizice']; // can use filter like ObjectName = 'Account' etc..

and then use 
result.RecordTypeID = rt.id; // using ID
  insert result;

 
Arunkumar RArunkumar R
Hi Gheroghe,

First get your desired record type id by using the below format,
 
Id accountRecordTypeId = Schema.SObjectType.Account.getRecordTypeInfosByName().get('Record Type Name').getRecordTypeId()
Mention your object name and record type name in the above syntax.

While inserting account record use like below,
 
// Here you can mention all required values.
Account acc = new Account();
acc.RecordTypeId = accountRecordTypeId;
insert acc;
This was selected as the best answer