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
akallioWileyakallioWiley 

Help with Partial Processing of Update

Hi All,

I am trying to figure out how do paratial processing of an update for the first time. I have always used dml methods up to this point, but now playing around with database methods.

 

So I understand the following example just fine:

Database.SaveResult[] lsr = Database.update(accounts, false); 

for(Database.SaveResult sr : lsr){ 
    if (!sr.isSuccess()) { 
        myMethodToaddToErrorObjectList(sr);
    }
} 

 This almost works for what I need to do. However, I want to add criteria to the if statement so that I handle certain exceptions in certain ways. So, I want to test for a specific exception, and my question is, is there a better way to do that than to test for the actual message?

 

If the message thrown is this: "Database.Error[getFields=();getMessage=duplicate value found: External_ID__c duplicates value on record with id: a0IZ0000000RCgm;getStatusCode=DUPLICATE_VALUE;]"

 

Then am I supposed to put that in my if stattement if I want to handle those types of exceptions in a different way? Something like this:

 

Database.SaveResult[] saveResults = Database.update(childEvals, false);
        //now I parse the results and find the errors
        for(Database.SaveResult saveResult : saveResults) {
            if(!saveResult.isSuccess()) {
                //system.assert(false,'Your problem is '+err); 
               Database.Error err = saveResult.getErrors()[0];
                if(err = duplicate value found: External_ID__c duplicates value on record with id') {
                    //more code to handle these types of errrors
                }    
               
            }    

 

It seems like there should be a better way.


Best Answer chosen by Admin (Salesforce Developers) 
sfdcfoxsfdcfox

Use getStatusCode() on the error to get an enumerated value (one guaranteed to be the same regardless of the message thrown). A list of status values are available in your WSDL (the Enterprise WSDL, that is: Setup > Develop > API > Generate Enterprise WSDL). You can catch the ones you want and generically handle the remainder. You don't have to handle them yourself; you can simply rollback and place your own error message on the record, if you prefer.

All Answers

sfdcfoxsfdcfox

Use getStatusCode() on the error to get an enumerated value (one guaranteed to be the same regardless of the message thrown). A list of status values are available in your WSDL (the Enterprise WSDL, that is: Setup > Develop > API > Generate Enterprise WSDL). You can catch the ones you want and generically handle the remainder. You don't have to handle them yourself; you can simply rollback and place your own error message on the record, if you prefer.

This was selected as the best answer
akallioWileyakallioWiley

With the help of the post above I think I'm much closer. So, the code below is what I have currently have. It won't compile because I am trying to compare a String to a Status Code. 'Duplicate_Value' is the error that I want, but I don't see a way to either convert the string to a status code or to convert the status code to a string. 

 

Sorry, if this is a dumb question...I know nothing about WSDLs.

 

for(Database.SaveResult saveResult : saveResults) {
            if(!saveResult.isSuccess()) {                
                for(Database.Error err : saveResult.getErrors()) {
                    if(err.getStatusCode() == 'DUPLICATE_VALUE') {
                    }    
                }
            }           
        }

 

akallioWileyakallioWiley

I think I just figured it out....needed to use enum methods:

 

 for(Database.SaveResult saveResult : saveResults) {
            if(!saveResult.isSuccess()) {                
                for(Database.Error err : saveResult.getErrors()) {
                    if(err.getStatusCode() == StatusCode.DUPLICATE_VALUE) {
                    }    
                }
            }           
        }

 

sfdcfoxsfdcfox

They are global static constants, accessed through StatusCode, like this:

 

if(err.getStatusCode() == StatusCode.DUPLICATE_VALUE) {

Since they are never strings, they will never give you the hassle of case sensitivity or changing their values.