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
duke1duke1 

Trouble finding ID of parent in a look-up relationship in trigger

I am showing two triggers below; Trigger 2 at the bottom of the page works, except it performs the operation on all records, so I am trying in Trigger 1 to only access those records that have changed.  I have a lookup relationship between two custom objects Master_Lookup (parent) and Child_Detail (child), where a Master_Lookup could have many Child_Details, but a Child_Detail has only one Master_Lookup.  The first for loop using Trigger.new fills the set with a null value for the ID of the Master_Lookup instead of a valid ID.  I know there is a Master_Lookup associated with this Child_Detail so I can't understand why it has a null ID.  Can someone help me out.  Thanks.

 

Trigger 1:

trigger updateOnQtyCountTotal on Child_Detail__c (after insert, after update) {
    // Form a set of affected MasterLookupRecords to avoid duplicates
    Set<id> masterLookupIds = new Set<id>();
    for (Child_Detail__c myChildDetailRecord : Trigger.new){
        masterLookupIds.add(myChildDetailRecord.Master_Lookup__r.Id);
    }
    for (id value : masterLookupIds) {
        System.debug('Set = ' + value);
    }
    String queryString = 'SELECT Id, Total_Quantity__c, Total_Price__c, ' +
                         'Total_Count__c, (SELECT Quantity__c, Total_Price__c ' +
                         'from Child_Detail__r) from Master_Lookup__c where Id in :masterLookupIds';
    Master_Lookup__c[] myMasterLookup = Database.query(queryString);
    for (Master_Lookup__c masterLookupRecord : myMasterLookup) {
        sObject[] childRecordsFromMaster = masterLookupRecord.getSObjects('Child_Detail__r');
        Decimal runningQuantityTotal = 0;
        Decimal runningPriceTotal = 0;
        Decimal runningItemsTotal = 0;
        // Prevent a null relationship from being accessed
        if (childRecordsFromMaster != null) {
            for (sObject childRecord : childRecordsFromMaster){
                runningQuantityTotal += (Decimal)childRecord.get('Quantity__c');
                runningPriceTotal += (Decimal)childRecord.get('Total_Price__c');
                runningItemsTotal++;
            }
        }
        masterLookupRecord.Total_Quantity__c = runningQuantityTotal;
        masterLookupRecord.Total_Price__c = runningPriceTotal;
        masterLookupRecord.Total_Count__c = runningItemsTotal;
    }
    upsert myMasterLookup;
}

 

Trigger 2:
/*trigger updateOnQtyCountTotal on Child_Detail__c (after insert, after update) {
    // Use parent-child relationship to find all quantities for each Master_Lookup
    String queryString = 'SELECT Id, Total_Quantity__c, Total_Price__c, ' +
                         'Total_Count__c, (SELECT Quantity__c, Total_Price__c ' +
                         'from Child_Detail__r) from Master_Lookup__c';
    Master_Lookup__c[] myMasterLookup = Database.query(queryString);
    for (Master_Lookup__c masterLookupRecord : myMasterLookup){
        sObject[] childRecordsFromMaster = masterLookupRecord.getSObjects('Child_Detail__r');
        Decimal runningQuantityTotal = 0;
        Decimal runningPriceTotal = 0;
        Decimal runningItemsTotal = 0;
        // Prevent a null relationship from being accessed
        if (childRecordsFromMaster != null) {
            for (sObject childRecord : childRecordsFromMaster){
                runningQuantityTotal += (Decimal)childRecord.get('Quantity__c');
                runningPriceTotal += (Decimal)childRecord.get('Total_Price__c');
                runningItemsTotal++;
            }
        }
        masterLookupRecord.Total_Quantity__c = runningQuantityTotal;
        masterLookupRecord.Total_Price__c = runningPriceTotal;
        masterLookupRecord.Total_Count__c = runningItemsTotal;
    }
    upsert myMasterLookup;
}*/

Best Answer chosen by Admin (Salesforce Developers) 
Jeremy.NottinghJeremy.Nottingh

I guess is seems like your For loop would work, but try this variation: the ID itself is in a field on ChildDetail__c.Master_Lookup__c.

 

    Set<id> masterLookupIds = new Set<id>();
    for (Child_Detail__c myChildDetailRecord : Trigger.new){
        masterLookupIds.add(myChildDetailRecord.Master_Lookup__c);
    }
    

 

Jeremy

All Answers

Jeremy.NottinghJeremy.Nottingh

I guess is seems like your For loop would work, but try this variation: the ID itself is in a field on ChildDetail__c.Master_Lookup__c.

 

    Set<id> masterLookupIds = new Set<id>();
    for (Child_Detail__c myChildDetailRecord : Trigger.new){
        masterLookupIds.add(myChildDetailRecord.Master_Lookup__c);
    }
    

 

Jeremy

This was selected as the best answer
duke1duke1

That was it.  Thanks for your help on both issues, Jeremy.

duke1duke1

I guess I posted my reply in the wrong place.  Anyway, that was the detail I needed.  Thanks for your help on both this and the previous issue, Jeremy.