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
Inertia1024Inertia1024 

RecordTypeId in create

I can't figure out a "clean" way to assign a RecordType when creating a new Account object.  Is it possible to do some kind of subquery during the create operation that will lookup the RecordTypeId based on the RecordType Name?

 

It just seems weird to me to have these 15 character Ids hard coded...

 

 

$so = new SObject();
$so->type = 'Account';
$so->fields = array(
  'Name' => $someName,
  'Phone' => $someNumber,
  'RecordTypeId' => '01270000000DVD5');

 

Maybe I'm missing something, but it would make more sense if it could be something like "RecordType.Name => Something" instead.

 

Best Answer chosen by Admin (Salesforce Developers) 
Park Walker (TAGL)Park Walker (TAGL)

While you cannot do a subquery to get the RecordTypeId as part of the create you can do a query to obtain it based on the name (DeveloperName) and object type ((SobjectType) and then set the value to the Id that you retrieve. If you are going to be creating multiple records of various type it will be more efficient to grab all of the types at once and cache them.

 

Salesforce does not use SQL so some things are more difficult to accomplish while other simply cannot be done.

 

Park

All Answers

Park Walker (TAGL)Park Walker (TAGL)

While you cannot do a subquery to get the RecordTypeId as part of the create you can do a query to obtain it based on the name (DeveloperName) and object type ((SobjectType) and then set the value to the Id that you retrieve. If you are going to be creating multiple records of various type it will be more efficient to grab all of the types at once and cache them.

 

Salesforce does not use SQL so some things are more difficult to accomplish while other simply cannot be done.

 

Park

This was selected as the best answer
Inertia1024Inertia1024

I see.  So this is a "normal" development practice with Salesforce API, to cache various Ids to use in code?  I'm new to the Salesforce API and just trying to follow best practices... but I couldn't find any online examples that demonstrated a similar use case.

Park Walker (TAGL)Park Walker (TAGL)

Yes, this is the normal practice. You'll find the same practice is necessary for linking objects. If you want to add a new contact associated with a new account you will create the Account first and then use the Id value returned by the create call as the AccountId in the Contact record. Create, update and upsert calls always work with a single object. For a number of years you could not even query more than one object at a time, although that is no longer the case.

 

If you're new to Salesforce but not to programing you may find that the Java code is more valuable in finding examples, even though they will need to be modified to work in PHP.

 

Park