1

I have a complicated trigger (a labor of love for over 2 years now). I've recently discovered one of the problems in it. It ends up with duplicate entries in "updateList" to apply to SF (recently bulkified it with help from StackExchange, thank you very much).

if(updateList.get((Account)accObj) == null )
{ // Avoid DUPES...
    updateList.add((Account)accObj);
}

The first line is not right. The .add line is FINE. How do I reference the accObj in the IF (or another way of doing this please??). I thought this would be simple. Silly me.

Thanks all.

2 Answers 2

11

You're best using a Map to avoid dupes, as you can easily check if the AccountId already exists as a key value in the Map.

Map<String, Account> updateMap = new Map<String, Account>{};

if(!updateMap.containsKey((String)accObj.get('Id')) //check that Acc Id not already added
{ // Avoid DUPES...
    updateMap.put((String)accObj.get('Id'), (Account)accObj);
}

Then when you want to update Accounts, use

Database.update(updateMap.values());
10
  • Thanks... going to try this ASAP, reading through it, it sounds very appropriate. It will be nice to be able to be more of an Answerer as time goes on.... (smile)
    – AMM
    Oct 23, 2012 at 21:54
  • Its only a matter of time :) Please try it, and feel free to reach out for any clarifications. Oct 23, 2012 at 21:55
  • I had to add the third parenthesis on the first IF, no problem... but it's generating the following error:
    – AMM
    Oct 24, 2012 at 13:15
  • Error: Compile Error: Incompatible key type Object for MAP<Id,Account> at line 591 column 17 ((( which is th e"updateMap...." lime of the IF in your logic ))) Any ideas? Thanks!
    – AMM
    Oct 24, 2012 at 13:16
  • ` if( !updateMap.containsKey(accObj.get('Id') ) ) { //check that Acc Id not already added updateMap.put(accObj.get('Id'), (Account)accObj); }
    – AMM
    Oct 24, 2012 at 13:17
1

In this case how to avoid duplicate:

Map> mapSectionAgreement = new Map>();

        // Listing Agreement Section Names in to Map
        for(OISP_Agreement__c argmnts:lstAgreement){

            //List<OISP_Agreement__c> sectionNamesList = mapSectionAgreement.get(argmnts.OISP_Agreement_Section__r.name);

            if(mapSectionAgreement.containskey(argmnts.OISP_Agreement_Section__r.name)){
                List<OISP_Agreement__c> sectionNamesList = mapSectionAgreement.get(argmnts.OISP_Agreement_Section__r.name);
                sectionNamesList.add(argmnts);
                mapSectionAgreement.put(argmnts.OISP_Agreement_Section__r.name,sectionNamesList);
            } 
            else{
                List<OISP_Agreement__c> sectionNamesList = new List<OISP_Agreement__c>();
                sectionNamesList.add(argmnts);
                mapSectionAgreement.put(argmnts.OISP_Agreement_Section__r.name,sectionNamesList);
            }
        }

You must log in to answer this question.

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