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
TheReportDoctorTheReportDoctor 

What is the ID of the new record I just added?

Here is a small sample of my code.  In the line that is commented out for the AddError (used for debugging) I am asking for the ID of the newFulfillment record I just added.  How do I get the ID?

 

 

                                   List<productProcesses__c> processes = [SELECT Name, predecessorStep__c, processStep__c, successorStep__c, workStepName__c FROM productProcesses__c];
                                   //for each one add to fulfillment and fulfillmentDependencies
                                   for (productProcesses__c pp: processes) {
                                       List<fulfillment__c> newFulfillment = new List<fulfillment__c>();
                                       List<fulfillmentDependencies__c> newDependencies = new List<fulfillmentDependencies__c>();
                                       if (pp.predecessorStep__c == 0) {
                                           newStatus = 'current';
                                       } else {
                                           newStatus = 'waiting';
                                       }
                                       newFulfillment.add( new fulfillment__c (Opportunity__c = o.Id , Status__c = newStatus, workStepName__c = pp.workStepName__c));
                                       try {
                                            insert newfulfillment;
                                            //o.AddError('ID: ' + newfulfillment.Id);
                                       }
                                       catch (DmlException de) {
                                           for (Integer I = 0; I < de.getNumDml(); i++) {
                                                o.addError(de.getDmlMessage(i));
                                           }
                                       }

Best Answer chosen by Admin (Salesforce Developers) 
TheReportDoctorTheReportDoctor
The AddErro is used for tracing and has nothing to do with the problem I requested help on.  The answer is newFilfullment[0].Id

All Answers

rscottrscott
When you do an insert, the new Id gets populated automatically. In this case, you are inserting a list of fulfillment records, so you will need to access each fulfillment separately. Either in a loop, or like this: newFulfullment[0].Id
Anand SinghAnand Singh

It looks like you are trying to insert record in "fulfillment__c" and then explicity adding error in trigger with the newly created record ID. 

However the transaction is explicity aborted using "AddError" which causes the trigger to rollback the transaction including the insert operation.

The insert operation is not completed successfully, hence record ID is not available.

 

TheReportDoctorTheReportDoctor
The AddErro is used for tracing and has nothing to do with the problem I requested help on.  The answer is newFilfullment[0].Id
This was selected as the best answer