You need to sign in to do that
Don't have an account?
Dipa87
Merging values in a list with duplicate ids
Hi All,
I have a list which will have values like dis:
(Account:{Open_LCS_Opportunities_rollup__c=1, Id=001J000000WmAWIIA3}, Account:{Id=001J000000WmAWIIA3, Start_Date__c=2013-03-14 00:00:00},Account:{Id=001J000000WmAYTU2, Start_Date__c=2013-03-14 00:00:00})
I want to perform an update operation on this list. But before that I want to merge the values of the same ids into one and then update so that I dont loose any data.
I tried using Map,but it overrides the values:(
Please help me.
Thanks a lot for the reply.
But I have found out another way.
Something like this worked for me:
Map<Id,Sobject> mapOfSobjects = new Map<Id,Sobject>();
for(Sobject sOjbs :List of Sobject records)
{
if(!mapOfSobjects.containsKey(sOjbs.id)){
mapOfSobjects.put(sOjbs.id,sOjbs);
}
else{
Sobject actualObj = mapOfSobjects.get(sOjbs.id);
//adding the new field and its value with the existing map for the same record.
actualObj.put(AggregateResult__c,sOjbs.get(AggregateResult__c));
mapOfSobjects.put(sOjbs.id,actualObj);
}
}//end
update mapOfSobjects.values();
All Answers
You can use a Set(), then back to a List():
Set<Account> sa = new Set<Account>();
sa.addAll(list);
list.clear;
list.addAll(sa);
upsert list;
Thanks a lot for the reply.
But I have found out another way.
Something like this worked for me:
Map<Id,Sobject> mapOfSobjects = new Map<Id,Sobject>();
for(Sobject sOjbs :List of Sobject records)
{
if(!mapOfSobjects.containsKey(sOjbs.id)){
mapOfSobjects.put(sOjbs.id,sOjbs);
}
else{
Sobject actualObj = mapOfSobjects.get(sOjbs.id);
//adding the new field and its value with the existing map for the same record.
actualObj.put(AggregateResult__c,sOjbs.get(AggregateResult__c));
mapOfSobjects.put(sOjbs.id,actualObj);
}
}//end
update mapOfSobjects.values();