I have a requirement to update Account_Transfer__c(Child) object with District Name(Parent) when a field value matches on child record.

below is the logic that I've achieved so far

   insert accTransferPolicy; //inserting Account Transfer Policy record (grand child)
    Map<string, District__c> mapATwithDistrict = new Map<string, District__c>();
    for (District__c dst : [
            SELECT id, Name
            FROM District__c
            WHERE Status__c = 'OPEN' ]) {
            mapATwithDistrict.put(dst.Name, dst);
    for (Account_Transfer_Policy__c c : accTransferPolicy) {
        if (c.Account_Transfer__c != null) {
            if (mapATwithDistrict.containsKey(c.Account_Transfer__r.District_From__c)) {
                    c.Account_Transfer__r.Transfer_From__c = mapATwithDistrict.get(Name).Id;
        update c;

From the above code, the hierarchy is as:

Account_Transfer_Policy (grand child) --> Account_Transfer__c (Child) --> District__c (Parent)

Now, from the above code, i'm getting the error as

Variable does not exist: Name

can anyone please let me know on how to update Transfer_From__c field with matching district Name Id when District name on District__c record and District_From__c on Account Transfer record matches

1 Answer 1



Since you're assigning the District's Name as the key for mapATwithDistrict Map, you'll need to use the name to retrieve the Id.

Change From:

if (mapATwithDistrict.containsKey(c.Account_Transfer__r.District_From__c)) {
                c.Account_Transfer__r.Transfer_From__c = mapATwithDistrict.get(<How to get matching district id>).Id;

Change To:

if (mapATwithDistrict.containsKey(c.Account_Transfer__r.**District__r.Name**)) {
                c.Account_Transfer__r.Transfer_From__c = mapATwithDistrict.get(c.Account_Transfer__r.**District__r.Name**).Id;
  • if I use c.Account_Transfer__r.**District__r.Name**, I will be getting the District name only when Account Transfer record has District record. Here, I dont have it and I want Account Transfer to update with District name so that both the records will be related.
    – SK_112
    Commented Jul 2, 2020 at 16:24
  • Also, When I use your suggested code, the Account_Transfer__c record didnt updated with District__c name. Please suggest
    – SK_112
    Commented Jul 2, 2020 at 16:29
  • SK_12, I see now. Is it safe to assume the "District_From__c" values will be an EXACT match to the District's Name. For instance, the value of "District_From__c" will be "Northeast", and there will be a District Record with a Name of "Northeast". If that's true, simply use "c.Account_Transfer__r.Transfer_From__c = mapATwithDistrict.get(c.Account_Transfer__r.District_From__c).Id". If they are not an EXACT match, you'll need to use a leverage a unique key to perform the matching
    – SF Jedi
    Commented Jul 2, 2020 at 16:31
  • @SFJeci, I am getting the error as "Variable does not exists: District_From__c"
    – SK_112
    Commented Jul 2, 2020 at 16:33
  • Can you share the entire class/trigger?
    – SF Jedi
    Commented Jul 2, 2020 at 16:34

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .