0

I am getting the following error on line 12 and 25 and would need some help in resolving it.

Error on line 12:

Method does not exist or incorrect signature: void containsKey(Id) from the type Set

Error on Line 25:

Method does not exist or incorrect signature: void get(Id) from the type List

trigger UpdateContactPhoneNumberOnCases on Contact (after update) {
List <case> affectedCases = [select id, Contact_Phone__c  from case 
                            where contactId in : trigger.newMap.keyset()];

    // A map of contact Ids -> to list of cases

    map<id, list<case>> affectedCasesByContactId = new Map<Id,List<case>>();

    // Now populate the map  (affectedCasesByContactId)

    for (case c: affectedCases) {
**12---->**        if (affectedCasesByContactId.keyset().containsKey(c.contactId)){
            affectedCasesByContactId.get(c.contactId).add(c);

        }else {
            affectedCasesByContactId.put(c.contactId, new List<case>{c});
        }

    }

// create a new list to hold our newly updated cases

    List <case> updatedcases = new List<case>();
    for (Id contactId: trigger.newMap.keyset()) {        
**25--->** if(trigger.old.get(contactId).phone != trigger.new.get(contactId).phone)
           {
            for (case thisCase: affectedCasesByContactId.get(contactId)){
            thisCase.Contact_Phone__c = trigger.new.get(contactId).phone;
            updatedcases.add(thisCase);
        }
    }

}
           try{
           update updatedcases;
           } catch (exception e){
               system.debug(e.getMessage());
           }
           }

2 Answers 2

5

For the first one, instead of:

affectedCasesByContactId.keyset().containsKey(...)

use either the Map method containsKey on the Map:

affectedCasesByContactId.containsKey(...)

or the Set method contains on the Set:

affectedCasesByContactId.keyset().contains(...)

For the second one instead of trying to access from the List:

trigger.new.get(...)

access from the Map:

trigger.newMap.get(...)

Take a look at the available Trigger Context Variables and also do some Googling about lists and maps to get clear about how to use those most effectively.

0
2

The method you're looking for is contains(). containsKey() is in Map, not Set.

1
  • Aidan, Thanks for that. It solved the error on line 12. Could you help me on line 25. The code : if (trigger.old.get(contactId).phone != trigger.new.get(contactId).phone) Error : Method does not exist or incorrect signature: void get(Id) from the type List<Contact> Jan 29, 2018 at 14:59

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.