+ Start a Discussion
NishaCNishaC 

Map.get() problem

Hii 

i am using this list

List<Service__c> srList= [select id, Name, Customer__c,Days_Collection__c,Status__c,
Service_Start_Date__c,s.Customer__r.Grand_Parent_Account__c
from Service__c s where Status__c = 'Active' AND Days_Collection__c!=null AND Customer__c!=null];


map<id,Decimal> mapgrandParentsCounts = new map<id,Decimal>();
for(Service__c sr:srList)
{
if(mapgrandParentsCounts.containskey(sr.Customer__r.Grand_Parent_Account__c) && sr.Name!=null)
{
Decimal tCount = mapgrandParentsCounts.get(sr.Customer__r.Grand_Parent_Account__c);
tCount = tCount + finalCount;
mapgrandParentsCounts.put(sr.Customer__r.Grand_Parent_Account__c,tCount);
}
else
{
mapgrandParentsCounts.put(sr.Customer__r.Grand_Parent_Account__c,finalCount);
}
}

for(id gpAcc : mapgrandParentsCounts.keyset()) {
//here i want Name, city and address of this id
any idea
}

Best Answer chosen by Admin (Salesforce Developers) 
Laxman RaoLaxman Rao

Try this :

 

List<Service__c> srList= [select id, Name, Customer__c,Days_Collection__c,Status__c,
Service_Start_Date__c,s.Customer__r.Grand_Parent_Account__c, s.Customer__r.Grand_Parent_Account__r.Name, s.Customer__r.Grand_Parent_Account__r.City
from Service__c s where Status__c = 'Active' AND Days_Collection__c!=null AND Customer__c!=null];

map<Id,Service__c> map_Id_Service = new map<Id,Service__c>();
map<id,Decimal> mapgrandParentsCounts = new map<id,Decimal>();
for(Service__c sr:srList)
{
map_Id_Service.put(sr.Customer__r.Grand_Parent_Account__c, sr);
if(mapgrandParentsCounts.containskey(sr.Customer__r.Grand_Parent_Account__c) && sr.Name!=null)
{
Decimal tCount = mapgrandParentsCounts.get(sr.Customer__r.Grand_Parent_Account__c);
tCount = tCount + finalCount;
mapgrandParentsCounts.put(sr.Customer__r.Grand_Parent_Account__c,tCount);
}
else
{
mapgrandParentsCounts.put(sr.Customer__r.Grand_Parent_Account__c,finalCount);
}
}
for(id gpAcc : mapgrandParentsCounts.keyset()) {
//here i want Name, city and address of this id
//any idea
Service__c sr = map_Id_Service.get(gpAcc);

system.debug('Values are ' + sr.Customer__r.Grand_Parent_Account__r.Name)
system.debug('Values are ' + sr.Customer__r.Grand_Parent_Account__r.City)
}