I am trying to get record type id's from sub query under map, when I am iterating over map values I am getting "A non foreign key field cannot be referenced in a path expression" error
My requirement is to throw error if more than one record created per record type, so trying to capture Record type id and account Id.
public static void throwerror(list<sObject> dataset){
List<Plan__c > Plans = (List<Plan__c >)dataset;
Set<Id> accIds = new Set<Id>();
for(Plan__c ap:plans){
if(ap.Account__c != null)
accIds.add(ap.Account__c);
}
Map<Id, Account> accMap = new Map<Id, Account>([Select id , (Select Id,RecordType.Name,RecordTypeId From Plan__r) From Account Where Id IN :accIds ]);
map<Id,String> keymap = new map<Id,String>();
for(Account acc : accMap.values()){
keymap.put(acc.Id,acc.Plan__r.RecordTypeId);// Error : A non foreign key field cannot be referenced in a path expression: Plan__r
}
system.debug('--->>>'+keymap);
for(Plan__c ap:plans){
//here I want to include condition to check if accid and plan record id exists
if(accMap.containsKey(ap.Account__c) && !accMap.get(ap.Account__c).Plan__r.isEmpty()){
ap.addError('Only one plan is permitted per Record type');
}
}
}