[SalesForce] How to get the ID of the newly created record

How to get the ID that was created in a custom object

Below is my scenario, I'm creating a new record in the Work_Order__c object using the APEX class:

// object instance with memory allocation.
    Work_Order__c wo = new Work_Order__c();
    wo.Est_Hours__c = 89;
    wo.Instructions__c = 'this is a test';
    insert wo;

and it successfully created and I have looked at the system.debug log and this is what I'm getting in the log:

USER_DEBUG|[14]|DEBUG|Work_Order__c:{Instructions__c=this is a test,
Est_Hours__c=89, Id=a0j1a000000cbuFAAQ}

My question is there a way I can hold that newly created ID and update to a different object ?

Best Answer

There are 2 ways to get that.

One as mentioned by Keith, the ID field of the variable wo will be assigned the value.

Other than that you can get the value in After Trigger as well.

for(Your_Object__c  i:trigger.new) {
     ID id = i.Id;
}
Related Topic