2

How assign List To Set and Set to List? While updating some Existing records I need to avoid the Duplicates , How can I use Set To List and List to Set, While executing the following code getting the above error. can any one help me wt is wrong in my code..

public List<id> sitecontractids = new List<id>();
public List<Engineer_Checklist__c> ssnewList = new List<Engineer_Checklist__c>();
public List<SiteContracts__c> finalscnewlist = new List<SiteContracts__c>();
public List<SiteContracts__c> scList = new List<SiteContracts__c>();
public set<SiteContracts__c> scnewset = new set<SiteContracts__c>();
    scnewset =[SELECT Id,(Select id from Service_Sheets__r WHERE
                    Fire_Extinguisher_Service_complete__c=True ORDER BY  createdDate DESC ) FROM SiteContracts__c LIMIT 200];

 for(SiteContracts__c scnewid :scnewset ){
         sitecontractids.add(scnewid.id);
   }
    ssnewList =[Select Id, X6L_Water_Qty__c, X9L_Water_Qty__c,
            From Engineer_Checklist__c 
            Where Fire_Extinguisher_Service_complete__c=true AND Site_Contract__c !=NULL AND
                 Site_Contract__c IN :sitecontractids  ORDER BY  createdDate DESC LIMIT 200];
 for(Engineer_Checklist__c ssli:ssnewList)
{
    SiteContracts__c newsc = new SiteContracts__c();
newsc.id=ssli.Site_Contract__c;
newsc.X6L_Water__c=ssli.X6L_Water_Qty__c;
newsc.X9L_Water__c=ssli.X9L_Water_Qty__c;

scnewset.add(newsc);
}
finalscnewList.addall(sclist);
update finalscnewList;
3

If you want to avoid duplicates based on the SObject field values you can put the new objects straight into a set but you'll have to turn the set back into a list to do the insert/update/upsert:

Set<SiteContracts__c> newSet = new Set<SiteContracts__c>();
for(Engineer_Checklist__c ssli:ssnewList)
{
    SiteContracts__c newsc = new SiteContracts__c();
    newsc.id=ssli.Site_Contract__c;
    newsc.X6L_Water__c=ssli.X6L_Water_Qty__c;
    newsc.X9L_Water__c=ssli.X9L_Water_Qty__c;
    newSet.add(newsc);
}
upsert new List<SiteContracts__c>(newSet);

or if you already had the new objects in a list you could convert that to a set (and so eliminate the duplicates) like this:

List<SiteContracts__c> newList = new List<SiteContracts__c>();
...
Set<SiteContracts__c> newSet = new Set<SiteContracts__c>(newList);
upsert new List<SiteContracts__c>(newSet);

It is fairly rare to be checking for duplicates like this in Apex code; usually duplicate checks would be based on specific fields not all fields.

PS

From the comments, it appears that the problem is that Engineer_Checklist__c is a child object of SiteContracts__c and that the code is trying to update the same SiteContracts__c object multiple times because there are many Engineer_Checklist__c objects being queried for each SiteContracts__c. So there is also a logical problem here: which of the many child Engineer_Checklist__c values should be pushed into the parent SiteContracts__c object?

A quick fix to the immediate problem would be avoid the duplicates by using a set of the ID values (and arbitrarily taking the values from the first Engineer_Checklist__c):

List<SiteContracts__c> updateList = new List<SiteContracts__c>();
Set<Id> handledContractIds = new Set<Id>();
for(Engineer_Checklist__c ssli:ssnewList)
{
    if (!handledContractIds.contains(ssli.Site_Contract__c)) {
        SiteContracts__c newsc = new SiteContracts__c();
        newsc.id=ssli.Site_Contract__c;
        newsc.X6L_Water__c=ssli.X6L_Water_Qty__c;
        newsc.X9L_Water__c=ssli.X9L_Water_Qty__c;
        updateList.add(newsc);
        handledContractIds.add(ssli.Site_Contract__c);
    }
}
update upddateList;
| improve this answer | |
  • Hi Keith, I did as u suggested but getting Duplicate ID ERROR:System.ListException: Duplicate id in list : .......... – Karti K Oct 14 '14 at 17:25
  • @KartiK See the answer PS. – Keith C Oct 14 '14 at 18:10
0

SOQL query returns List of records.Please change to list instead of set in query.

| improve this answer | |
  • Also Set is only for primitive types. You can't have a set against a complex type like an sobject. – willard Oct 14 '14 at 15:23
  • 2
    @willard If you run System.assertEquals(2, new Set<Contact>{new Contact(LastName = 'Doe'), new Contact(LastName = 'Smith'), new Contact(LastName = 'Doe')}.size()); you will see that sets do have defined behaviour for SObjects. – Keith C Oct 14 '14 at 15:52
  • huh - learn something new every day. thanks keith for the clarification. – willard Oct 14 '14 at 19:34

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.