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
OCDGeekOCDGeek 

Any way to create ID field for new SObject before insert

Is there a way you can create/set the Id field of a new SObject before performing an insert DML statement?

 

I know you can set it to an existing Id like this:

 

SObject s = Database.query('Select Id from account limit 1')[0].getSObjectType().newSObject([SELECT Id FROM Account LIMIT 1][0].id);

 

 

But is there a way to set it for a newly created SObject?

 

I need to create a few objects, retrieve their Ids and then send them to another system via a web service callout. I cannot issue a web service callout after the DML statement (without using @future call), so I'm trying to get the Ids and send them before actually making the insert DML statements

 

Any help would be appreciated, even it if it's "You can't do that."

 

Thanks,

 

Steven

Best Answer chosen by Admin (Salesforce Developers) 
SuperfellSuperfell

You can't do that. Depending on what the external system is doing, you may want to look into externalId fields, which would allow you to control the value of the externalId before insert, and the external system can do most operations using the externalId instead of the salesforce Id.

All Answers

SuperfellSuperfell

You can't do that. Depending on what the external system is doing, you may want to look into externalId fields, which would allow you to control the value of the externalId before insert, and the external system can do most operations using the externalId instead of the salesforce Id.

This was selected as the best answer
OCDGeekOCDGeek

Thanks for the quick response.