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
Eager-2-LearnEager-2-Learn 

Database.SaveResult[] results = Database.Update(opps, false)

Hi,

 

Using the Database DML property SaveResult along with the for loop to retrieve list of the error messages pertaining to the records that did not get updated does not do me a whole lot of good without knowing the record ID!!!


I have found others having the difficulty of accepting that this is the nature of the DML property SaveResult.  If it is used to Insert I can understand you would not have an ID on a record that did not get inserted but this about existing records with an ID.

 

Does anyone know a work around still using the code:

 

Database.SaveResult[] results = Database.Update(opps, false)

 

that would allow me to include the ID in my email.  I need to be able to research the record in order to troubleshoot the "Why didn't the record update?" question.

 

Thanks in advanced.  Troubles and work arounds exist in all languages but Apex needs to catch up to the big league languages like C# and java.  That is just a want-a-be programmer's perspective.  In the mean time I am having a blast learning SFDC as a whole.

 

You guess make it that much more desirable to learn.

 

 

Best Answer chosen by Admin (Salesforce Developers) 
rscottrscott

If you want the Id, the save results come back in the same order that you send the objects in to get updated. So, if saveResult[3] was not successful, you should be able to use opps[3] to get the Id.

 

According to the documentation for getId():

The ID of the sObject you were trying to insert or update. If this field contains a value, the object was successfully inserted or updated. If this field is empty, the operation was not successful for that object.

 

From the docs for SaveResult:

Each element in the SaveResult array corresponds to the sObject array passed as the sObject[] parameter in the database method, that is, the first element in the SaveResult array matches the first element passed in the sObject array, the second element corresponds with the second element, and so on. If only one sObject is passed in, the SaveResults array contains a single element.

All Answers

Anand@SAASAnand@SAAS

There's a getId() on SaveResult that gives you the record id. For more information go to http://www.salesforce.com/us/developer/docs/apexcode/Content/apex_dml_update.htm. Same method exists in UpsertResult, DeleteResult etc.

Eager-2-LearnEager-2-Learn

The problem is that getID() returns NULL on a failed update attempt.  That is why I call this a bug unless I am misunderstanding something.  If the record already exists then the SFDC class should be able to get me the ID of the record.  It tells me what the error is but without knowing the ID of the record it does no good.  The only time it makes sense to me to have a NULL value is if an Insert failed.

 

Does anyone have a work around for this issue.  I found others having the same concern as I.

 

Thanks

rscottrscott

If you want the Id, the save results come back in the same order that you send the objects in to get updated. So, if saveResult[3] was not successful, you should be able to use opps[3] to get the Id.

 

According to the documentation for getId():

The ID of the sObject you were trying to insert or update. If this field contains a value, the object was successfully inserted or updated. If this field is empty, the operation was not successful for that object.

 

From the docs for SaveResult:

Each element in the SaveResult array corresponds to the sObject array passed as the sObject[] parameter in the database method, that is, the first element in the SaveResult array matches the first element passed in the sObject array, the second element corresponds with the second element, and so on. If only one sObject is passed in, the SaveResults array contains a single element.

This was selected as the best answer