You need to sign in to do that
Don't have an account?
Jan Julian
Maps and Relationship Queries in Apex
I have the following relationship query instantiated to a Map<>:
Map<Id, Account> accountsMap = new Map<Id, Account>([ select a.id, a.Name, a.Active_Support_Contract__c, (select s.id, s.status__c, s.status, s.name, s.accountId, s.recordType.name from a.ServiceContracts s) from Account a where a.id in :accountIds]);
.
.
.
Account account = accountsMap.get(aid);
if (account.ServiceContracts != null) {...} // "Attempt to de-reference a null object"
if (account.ServiceContracts.size() != 0 {...} // "Attempt to de-reference a null object"
for (ServiceContract sc : account.ServiceContracts) {} // "Attempt to de-reference a null object"
I've tried several ways of accessing the Account's ServiceContracts. Everything works fine in my sandbox but when I'm ready to deploy to production, I get the errors above.
The log files are not helpful either.
Instead try this code to access child records
list<account> accountsMap = [select a.id, a.Name, a.Active_Support_Contract__c,
(select s.id, s.status__c, s.status, s.name, s.accountId, s.recordType.name from a.ServiceContracts s)
from Account a where a.id in :accountIds]);
.
.
for(Account a : accountmap){
for (ServiceContract sc : a.ServiceContracts) {}}
Have you checked if your Map contains an entry for aid?
if(map.containsKey(aid)) {
} else {
// aid does not have an entry in the map
}