function readOnly(count){ }
Starting November 20, the site will be set to read-only. On December 4, 2023,
forum discussions will move to the Trailblazer Community.
+ Start a Discussion
sfdc-admin-smsfdc-admin-sm 

Invalid foreign key relationship when trying to add an Account to a map

Hello,

I am getting the following error: Invalid foreign key relationship: License__c.Account__c at line 21 column 23 for my code: mapAcct.put(ObjLic.Account__c.Id, ObjLic.Account__c); in the below trigger code snippet. What should the correct code be to add an Acciount Id and Account record to the mapAcct? The License__c custom object has a lookup relationship to Accounts.

My code:

Set<Id> licIds = new Set<Id>();
   
    for(License__c l : Trigger.new){
        if(l.Expiration_Date__c > Date.Today()){
            licIds.add(l.Id);
        }
    }
   
System.debug('<<<licIds size = ' +licIds.size());

Map<Id, Account> mapAcct;
   
    if(licIds != NULL){
       for(License__c ObjLic : licIds){
          mapAcct.put(ObjLic.Account__c.Id, ObjLic.Account__c);   
       }
    }
kevin lamkevin lam
Updated your code:

Set<Id> licIds = new Set<Id>();
  
    for(License__c l : Trigger.new){
        if(l.Expiration_Date__c > Date.Today()){
//            licIds.add(l.Id);
              licids.add(l.Account__c);
        }
    }
  
System.debug('<<<licIds size = ' +licIds.size());

Map<Id, Account> mapAcct;
  
    if(licIds != NULL){
//       for(License__c ObjLic : licIds){
//          mapAcct.put(ObjLic.Account__c.Id, ObjLic.Account__c);  
//       }
         for (Account acc : [SELECT Id FROM Account WHERE Id IN :licIds]) {
             mapAcct.put(acc.Id, acc);
         }
    }