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
Eric Blaxton 11Eric Blaxton 11 

How to get Database.UpsertResult to show work

Hi and thanks in advance.

I want to see the records that were created or updated via Database.UpsertResult.  The records are being updated and/or created, but I am not getting ANY results from the for loop. 

Any ideas?
 
Schema.SObjectField f = Case.Ticket_Number__c; 

       Database.UpsertResult [] cr = Database.upsert(toBeUpsertCases, f, false);  
        
        // I want to see what was updated / created        
        for(Integer index = 0; index < cr.size(); index++ )
        {
            if(cr[index].isSuccess())
            {
                if(cr[index].isCreated())
                {
                    System.debug(toBeUpsertCases[index].Ticket_Number__c + ' was created');
                } else 
                 {
                   System.debug(toBeUpsertCases[index].Ticket_Number__c + ' was updated'); 
                 }
            }
        }// end for loop

Eric
Best Answer chosen by Eric Blaxton 11
Daniel AhlDaniel Ahl

Hello Eric!
I altered your code a little.
Also, please note that if the Ticket_Number__c isn't set on creation, it would print "null was created".
 

Database.UpsertResult [] cr = Database.upsert(toBeUpsertCases, false);
        Map<Id, Case> caseMap = new Map<Id, Case>(toBeUpsertCases);
        // I want to see what was updated / created        
        for(Database.upsertResult result : cr){
            System.debug(result);
            if(result.isSuccess()){
                if(result.isCreated()){
                    System.debug(caseMap.get(result.getId()).Ticket_Number__c + ' was created');
                } else {
                    System.debug(caseMap.get(result.getId()).Ticket_Number__c + ' was updated');
                }
            }
        }

All Answers

AbhishekAbhishek (Salesforce Developers) 
Hi Eric,

Database.upsertResult contains the result of an upsert DML operation returned by the Database. upsert method. If you want to check the status of Database.Upsert transaction, that information will be available here. Also, the number of errors, sobject id where you performed the operation and all these details will be logged in this table.


Please read the below article.
https://developer.salesforce.com/docs/atlas.en-us.apexcode.meta/apexcode/apex_methods_system_database_upsertresult.htm#apex_Database_UpsertResult_getErrors

and for example, you can prefer the below one.

https://developer.salesforce.com/docs/atlas.en-us.apexcode.meta/apexcode/langCon_apex_dml_examples_upsert.htm

Let me know if it helps you and close your query by marking it as solved so that it can help others in the future.

Thanks!
Daniel AhlDaniel Ahl

Hello Eric!
I altered your code a little.
Also, please note that if the Ticket_Number__c isn't set on creation, it would print "null was created".
 

Database.UpsertResult [] cr = Database.upsert(toBeUpsertCases, false);
        Map<Id, Case> caseMap = new Map<Id, Case>(toBeUpsertCases);
        // I want to see what was updated / created        
        for(Database.upsertResult result : cr){
            System.debug(result);
            if(result.isSuccess()){
                if(result.isCreated()){
                    System.debug(caseMap.get(result.getId()).Ticket_Number__c + ' was created');
                } else {
                    System.debug(caseMap.get(result.getId()).Ticket_Number__c + ' was updated');
                }
            }
        }
This was selected as the best answer
Eric Blaxton 11Eric Blaxton 11
Daniel,

Appreciate your help on this.

Eric