1

I have below code where I face error as incompatible type. Can someone please help me resolve it? I can't get how to populate it.

Apex Code

 Map<Id,List<Account_Territory_Loader_vod__c>> accATLMap = new Map<Id,List<Account_Territory_Loader_vod__c>>();
.........
for(Account_Territory_Loader_vod__c atl: atlList) {

 if(accATLMap.get(atl.account_vod__c) == null)
 accATLMap.put(atl.account_vod__c,atl); // This line gets error out

}

2 Answers 2

4

you can use below code to get your desired Map : -

for(Account_Territory_Loader_vod__c atl: atlList) {
    if(accATLMap.containsKey(atl.account_vod__c) && accATLMap.get(atl.account_vod__c) != null) {
        List<Account_Territory_Loader_vod__c> lst_terr = accATLMap.get(atl.account_vod__c);
        lst_terr.add(atl);
        accATLMap.put(atl.account_vod__c,lst_terr); 
    }   
    else {
        accATLMap.put(atl.account_vod__c, new List<Account_Territory_Loader_vod__c> {atl});
    }
}

hope it solve your purpose.

3

You're trying to put an instance of Account_Territory_Loader_vod__c into a list of Account_Territory_Loader_vod__c.

accATLMap.put(atl.account_vod__c,atl);

You need to instantiate a new list for each key in your map, and then add the element to the list:

// Check whether the value for that key exists
if (accATLMap.get(atl.account_vod__c) == null)
{
    // instantiate a new list
    accATLMap.put(atl.account_vod__c, new List <Account_Territory_Loader_vod__c> ());
}

// add the element to the list
accATLMap.get(atl.account_vod__c).add(atl);

You must log in to answer this question.

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