0

I have Map with Account and Account Id. I want to insert that accounts in Queueable and return list of all ids in order as acount is inserted(Or map, it doesnt really matther). How can i keep order if inserting list?

public with sharing class QueueableInsert  implements Queueable {
    Map<String,Account> oldIdToObject = new Map<String,List<SObject>>();
    public QueueableImportRecords(Map<String,List<SObject>> oldIdToObject) {
        this.oldIdToObject = oldIdToObject;
    }

    public void execute(QueueableContext context){      
        
        insert oldIdToObject.values();

    }

}

1 Answer 1

0

I found that I can use Database.insert() and then parse Database.SaveResult.

public with sharing class QueueableInsert  implements Queueable {
    Map<String,Account> oldIdToObject = new Map<String,List<SObject>>();
    public QueueableImportRecords(Map<String,List<SObject>> oldIdToObject) {
        this.oldIdToObject = oldIdToObject;
    }

    public void execute(QueueableContext context) {
        Database.SaveResult sr = Database.insert(oldIdToObject.values(), true);
        // Iterate through each returned result, they're returned in the same order as the sObject collection passed to `Database.insert`
        for (Database.SaveResult sr : srList) {
            if (sr.isSuccess()) {
                // Operation was successful, so get the ID of the record that was processed
                System.debug('Successfully inserted account. Account ID: ' + sr.getId());
            } else {
                // handle error
            }
        }
    }

}
0

You must log in to answer this question.

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