I have a class method that creates Campaign Member instances and inserts a list of them. Because the Campaign Member instances need an id before an update can be done, the upsert operation can't be used so I'm using insert instead passing a value of "false" so that the operation won't fail every time a record isn't inserted.

Since upsert can't be used, I'm trying to determined which records are or are not being inserted by iterating the results of the query, checking the value of isSuccess(), adding the unsuccessful ids to a list which can be used to find the actual objects, and then displaying that in a visual force page. Here is the code for this:

List<CampaignMember> returnList = new List<CampaignMember>();
        List<Id> listOfIds = new List<Id>();

               List<Database.Saveresult> results = Database.insert(allMembers, false);
                for(Database.SaveResult sr : results) {
                    if(sr.isSuccess()) {

      returnList = [Select Id, ContactId,CampaignId from CampaignMember where Id in :listOfIds limit 999];

I'm basing my approach partly on code I found online, but I don't know that it will help much as you should only be able to us any of the ids to find the records that were successfully inserted since you can only actually query the Campaign Member object. Is there an actual way to determine which rows were unsuccessfully inserted?

  • Database.SaveResult.getId() only returns a value on success. As we discussed previously, you can use upsert, but you must perform a query for existing records first.
    – David Reed
    Mar 9, 2018 at 16:56
  • @DavidReed but you wouldnt have the ids from the campaign members yet so how would you be able to execute a query using ids? Mar 9, 2018 at 18:38
  • You query based on the fields that identify the Campaign Member as a duplicate: the Contact or Lead Id and the Campaign Id.
    – David Reed
    Mar 9, 2018 at 18:49
  • @DavidReed Right, so assuming that all of the campaign members are new records (new campaigns have been created so Campaign Ids are new) you would still be doing an initial insert operation first followed by the query. The update would have to happen afterwards, if necessary. Mar 9, 2018 at 19:18

1 Answer 1


You can take this strategy to find the results

Create a unique field in the object for which you are inserting the records. Let's say External_Id__c and you will have a logic to populate data which will help you to find which records got failed. Since during insert if records got failed you will not get Ids.

Since, Salesforce List is indexed so it will help you to find out which records got failed to be inserted.

private class JsonInsertResult
    List<Database.Error> Errors {get;set;}
    String ExternalId {get;set;}
    String SFID {get;set;}      
    Boolean isSuccess {get;set;}

List<Database.Saveresult> results = Database.insert(allMembers, false);

//loop through the results and add values from Database.Saveresult
List<JsonInsertResult> lstJsonInsertResult = new List<JsonInsertResult>();
for(Integer index = 0, size = results.size(); index < size; index++) 
    JsonInsertResult resultRow = new JsonInsertResult();
    resultRow.Errors =  results[index].getErrors();
    resultRow.ExternalId = allMembers[index].External_Id__c;
    resultRow.isSuccess = results[index].isSuccess();
        resultRow.SFID = results[index].getId();

Also, refer this answer to find more information.

Database.upsert - how to find which records have inserted/updated based on ExternalId

  • not sure why it was downvoted. So you're saying that another custom field (External_Id__c) needs to be added to the sObject? What would it be populated with? Mar 9, 2018 at 18:28
  • when you are creating list of records, that moment for those records assign some auto-generated number or your own defined logic to put some unique value. Based on this you will pull which record got failed Mar 9, 2018 at 20:37
  • Oh, you mean I'll be able to print out at least some information for each record whether or not it actually has a salesforce assigned id. Mar 9, 2018 at 20:48
  • 1
    Yes, you could track that by this way Mar 10, 2018 at 0:00

You must log in to answer this question.

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