I have a List of String. This list consist of accountNumbers from Account Object. List<String> accountNumList
. I have a child object XYZ__c
, which is child of Account Object. XYZ__c
object has a custom field called status__c
and flag__c
I am looking at an efficient way to build a Map of type Map<String,String>
. where Key is the accountNumber
and Value is the status__c
field of the child object of the corresponding Account. There could be multiple children of a account. I need to get only the child whose flag__c
field has 'Active' text in it and use it as the value.
What I think I should do to build this Map:
List<String> accountNumList; // has all the accountNumbers
List<Account> acc =[select id,accountNumber,(select id,status__c,flag__c from child_relation__r where flag__c=='Active') from Account where accountnumber in:accountNumList];
Map<String,String> accountMap = new Map<String,String>();
For(Account a : acc){
accountMap.put(a.accountNumber,a.child_relation__r.status__c);
}
Is this an efficient way to build the Map? Any Suggestions!
Best Answer
You need to index into your list. Before you do so, however, you have to check if the list is empty.
If you omit the empty check, than any iteration over a record without an active child would throw this error: