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
dvid1.3963852481454365E12dvid1.3963852481454365E12 

Autopopulate a lookup field based on another lookup

Here's the situation - I have a custom object (Referral__c) with a lookup(account) field to (Client__c). That Account has a lookup(contact) field that I want placed into the record on the custom object, field is called Internal_Referral_By__c. I believe I may need to check that the lookup(contact) is not null, but I haven't got there yet. Here's where I am and am getting "Attempt to de-reference a null object" errorsL


trigger autoPopulateReferredBy on Referral__c (before insert, before update){

      set<Id> clientIDSet = new set<Id>();
    for(Referral__c referral: trigger.new){
        if(referral.Client__c != null){
            clientIdSet.add(referral.Client__c);
        }
    }

     map<id, Account> acctMap = new map<ID, Account>([SELECT Id, Name FROM Account where Id IN: clientIdSet]);


    for(Referral__c referral: trigger.new){
        if(acctMap.containsKey(referral.Client__c)){
            referral.Internal_Referral_By__c = acctMap.get(referral.Id).Internal_Referral_By__c;
        }      
    }

}
Ramu_SFDCRamu_SFDC
The problem is with the line acctMap.get(referral.Id) in the definition of map you are keying by Account id's however while retrieving you are using referral id which does not exist hence it is pointing out to a null reference and the issue. You might need to change it as acctmap.get(referral.client__c)

Hope this answers your quesiton !!

Please mark this as the best answer if this resolved your issue for others benefit.
dvid1.3963852481454365E12dvid1.3963852481454365E12
Thanks. I tried what you had suggested and now get the error:

Apex trigger autoPopulateReferredBy caused an unexpected exception, contact your administrator: autoPopulateReferredBy: execution of BeforeInsert caused by: System.SObjectException: SObject row was retrieved via SOQL without querying the requested field: Account.Internal_Referral_By__c: Trigger.autoPopulateReferredBy: line 17, column 1

For reference, here is the updated trigger:

trigger autoPopulateReferredBy on Referral__c (before insert, before update){

    //get the Client ID and store it in a set.
    set<Id> clientIDSet = new set<Id>();
    for(Referral__c referral: trigger.new){
        if(referral.Client__c != null){
            clientIdSet.add(referral.Client__c);
        }
    }

    //query the account (Client) records and get the associated account.
    map<id, Account> acctMap = new map<ID, Account>([SELECT Id, Name FROM Account where Id IN: clientIdSet]);

    //update the Internally referred by value based on the record.
    for(Referral__c referral: trigger.new){
        if(acctMap.containsKey(referral.Client__c)){
            referral.Internal_Referral_By__c = acctMap.get(referral.client__c).Internal_Referral_By__c;
        }      
    }

}
Naidu PothiniNaidu Pothini
Map<Id, Account> acctMap = new Map<Id, Account>([SELECT Id, Name, Internal_Referral_By__c FROM Account where Id IN: clientIdSet]);

You need to include the field you are referring inside the query.
Ramu_SFDCRamu_SFDC
Hi jgerstein1,

I second Naidu Pothini, the error is self explanatory "SObject row was retrieved via SOQL without querying the requested field: Account.Internal_Referral_By__c:". The system is saying that the user is trying to use field that was not queried or fetched from the backend.

This is one of the most common issue which all the developers will face during their initial phase of coding.