0

I'm upserting some records using Database.upsert, after that, I'm supposed to get the errors and pass them to a custom logger.

The problem is, the way this class works, I'd need to get a field value from the object that failed to upsert, is there any way to do so?

As far as I know, resultUpsert.getErrors won't return me any of the upserted fields.

Here's a code snippet of what I'm trying to do, it's not really doing anything, but anyway:

   if(lstNota.size() > 0){
            List<Database.upsertResult> resultUpsert = Database.upsert(lstNota, Nota_Fiscal__c.ID_externo_JDE__c, false);
            
            for(Integer i = 0; i < resultUpsert.size(); i++){
                ResponseNota res = new ResponseNota();
                if(!resultUpsert[i].isSuccess()){
                    if(resultUpsert.isCreated){

                    } else {
                       // here I'd like to access the fields to pass to my logger class, if possible.                       
                    }
                }
            }          
        }

I'm fine with any approaches to this, as long as it gets done.

1 Answer 1

0

The array of List<Database.upsertResult> will be in the same order as the records in lstNota. In other words:

for(Integer i = 0; i < resultUpsert.size(); i++){
  ResponseNota res = new ResponseNota();
  if(!resultUpsert[i].isSuccess()){
    if(resultUpsert[i].isCreated){
      // ...
    } else {
      sObject record = lstNota[i];
      doSomethingWith(record, resultUpsert[i]);
    }
  }
}
2
  • Thanks! I didn't know that they were in the same order, and couldn't really find this information anywhere, this works perfectly. Commented Apr 7, 2022 at 2:36
  • @JuliadeLuccaRibeiro It's not in the documentation, as far as I can tell, but this is how it's always worked. Glad I was able to help!
    – sfdcfox
    Commented Apr 7, 2022 at 11:10

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .