1

Can someone explain why I'm getting this error? I'm trying to retrieve the value of 'ownerIdstoNames' map with a key of type Id but the error says that I'm using a String?

map<id, Case> caseOldMap = (map<id, Case>) trigger.oldMap;
map<id, Case> caseNewMap = (map<id, Case>) trigger.newMap; 
Map < Id, String > ownerIdstoNames = new Map < Id, String > ();

 for (Case c: cases) {
     if (caseNewMap.get(c.Id).OwnerId != caseOldMap.get(c.Id).OwnerId) {
         ownerIdstoNames.put(caseNewMap.get(c.Id).OwnerId, ' ');
     }
 }
 List < User > owners = [SELECT Name, Id FROM User WHERE Id IN: ownerIdstoNames.keyset()];
 for (User owner: owners) {
     ownerIdstoNames.get(owner.Id).put(owner.Name); // Method does not exist or incorrect signature: [String].put(String)
 }
3

The problem is not while you are retrieving; it's when you are assigning the value.

ownerIdstoNames.get(owner.Id).put(owner.Name); // Method does not exist or incorrect signature: [String].put(String)

Lets break this into two steps:

  1. ownerIdstoNames.get(owner.Id)

    This will return a String because you have a map

  2. String.put(owner.Name)

    There is no string method called put .Map has a put method but not String .

    Instead just use put on the map and because the key of the map is unique ,your key will have correct value assigned

    for (User owner: owners) {
        ownerIdstoNames.put(owner.Id,owner.Name); //Put into the map
    }
    

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.