Is there a way to find how many records have been inserted or updated during an upsert operation? If there is a way, can someone please suggest how to get that details?

  • Are you looking for a way to calculate the total number of records during a bulk transaction or during a trigger / apex code?
    – Eric
    Dec 1, 2014 at 0:45

1 Answer 1


I believe you would need to use the Database.upsert method to obtain the results as shown here, in this example:

public class DmlSamples {

   /* This method accepts a collection of lead records and 
      creates a task for the owner(s) of any leads that were 
      created as new, that is, not updated as a result of the upsert
      operation */
   public static List<Database.upsertResult> upsertLeads(List<Lead> leads)  {

      /* Perform the upsert. In this case the unique identifier for the
         insert or update decision is the Salesforce record ID. If the 
         record ID is null the row will be inserted, otherwise an update
         will be attempted. */
      List<Database.upsertResult> uResults = Database.upsert(leads,false);

      /* This is the list for new tasks that will be inserted when new 
         leads are created. */
      List<Task> tasks = new List<Task>();
      for(Database.upsertResult result:uResults) {
         if (result.isSuccess() && result.isCreated()) 
              tasks.add(new Task(Subject = 'Follow-up', WhoId = result.getId()));

      /* If there are tasks to be inserted, insert them */

      return uResults;

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.