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
Steve Thurston 3Steve Thurston 3 

Set new Lead RecordTypeId from API

When creating a new Lead via an API call, I need to set the RecordType.  To do that, it seems as if I need the RecordTypeId.  But for testing, that ID is going to be different in Sandbox vs. Production.  So I don't want to hardcode the RecordTypeId.

When creating a new Lead via API, is there a way to specify the Record Type using its name instead of its ID?  Or at least have the API look up the RecordTypeId based on its name?
Best Answer chosen by Steve Thurston 3
Andrew GAndrew G
Ok, checking the forums, the answers are mixed.
Found this one that seems to think setting a record type on create for a Lead is not possible.
https://developer.salesforce.com/forums/?id=9060G000000Bg34QAC

Or we have an example for Oppty using JSON which seems to use the Record Type name.
https://developer.salesforce.com/forums/?id=9060G000000BduFQAS


Other option, thinking outside the box, could you use that previous code in an before insert trigger and test the lead creation source as the API, or tag a check box/picklist in the lead saying "API created", and fire the trigger on the before insert and the checkbox/picklist?

Hope one of those ideas helps.

Regards
Andrew
 

All Answers

Andrew GAndrew G
Hi Steve

Something like:
 
Id devRecordTypeId = Schema.SObjectType.Lead.getRecordTypeInfosByName().get('myRecordTypeName').getRecordTypeId();
where 'myRecordTypeName' is the name of your record type.  
Note that we also need to state the Object for which we need the record type, in this instance - 'Lead'

HTH

Regards
Andrew
 
Steve Thurston 3Steve Thurston 3
Thaks for responding.  I am familiar with that query - I use it in a couple Apex triggers.  But that is within Salesforce itself - it would be ideal if there was a way to set the record type without having to call a Salesforce Apex class, get the Type ID returned, and then use the result to insert a new Lead record.  i.e. when inserting a new Lead via API (i.e. from an external third party integration), is it possible to set it by name instead of ID?
Andrew GAndrew G
Ok, checking the forums, the answers are mixed.
Found this one that seems to think setting a record type on create for a Lead is not possible.
https://developer.salesforce.com/forums/?id=9060G000000Bg34QAC

Or we have an example for Oppty using JSON which seems to use the Record Type name.
https://developer.salesforce.com/forums/?id=9060G000000BduFQAS


Other option, thinking outside the box, could you use that previous code in an before insert trigger and test the lead creation source as the API, or tag a check box/picklist in the lead saying "API created", and fire the trigger on the before insert and the checkbox/picklist?

Hope one of those ideas helps.

Regards
Andrew
 
This was selected as the best answer
Steve Thurston 3Steve Thurston 3
Hmm, thank you, Andrew!  Very useful information!  I'll see what our developers can do with it!
Andrew Nguyen 61Andrew Nguyen 61

This may be old, but for whoever looking, I was able to resolve this by setting the following header to not auto-assign the record which will set default record type and name.

Set the following header: 

Sforce-Auto-Assign: False