The title says it all. I insert a List into the database and want to pass a list of its Ids in the same order as in the original list. As this test proved using the Map and List constructors does not do the job.
Is there a one-liner so that I don't have to loop over a list?
List<SObject> sobjectList = new List<SObject>();
sobjectList.addAll(SmartFactory.createSObjectList('Contact', false, 2));
sobjectList.addAll(SmartFactory.createSObjectList('Account', false, 2));
sobjectList.addAll(SmartFactory.createSObjectList('Opportunity', false, 2));
insert sobjectList;
List<Id> idList = new List<Id>(new Map<Id, SObject>(sobjectList).keySet());
for(Integer i=0; i<sobjectList.size(); i++) {
Id expectedId = sobjectList.get(i).Id;
Id actualId = idList.get(i);
System.assertEquals(expectedId, actualId);
}
Note: When I say preserved order I DO NOT mean alphabetical order or anything like that. I mean it in that way that if SObject S1 is the first in the List, its Id also needs to be the first element in the id list.
createSObjectList
method?insert
over there :)