1

I have a requirement where the child record (Account_Transfer_Policy__c) should be updated with new Parent record (Account_Transfer__c) if the district_from__c of current parent do not match with district_Acct_Code__c of child record.

This new parent update will be done when District_from__c of any Account Transfer record and District_Acct_Code__c of child matches

Now, below is my trigger

trigger ATwithATP on Account_Transfer_Policy__c (after insert) {
    Map<Id,Account_Transfer__c> mapIDWithAccount = new Map<Id,Account_Transfer__c>();
    Map<string,Account_Transfer__c> mapATwithATP = new Map<string,Account_Transfer__c>();

    for (Account_Transfer__c acc : [select id,District_From__c from Account_Transfer__c where District_From__c != Null and createddate = TODAY ORDER BY createddate DESC ]){
       
        mapATwithATP.put(acc.District_From__c,acc);
    }
    for(Account_Transfer_Policy__c c : trigger.new){
        
        if(c.Account_Transfer__c !=null && c.Policy__r.District_Acct_Code__c != c.Account_Transfer__r.District_From__c){
            if(mapATwithATP.containsKey(c.District_Acct_Code__c)){
                c.Account_Transfer__c = mapATwithATP.get(c.District_Acct_Code__c).Id;
            }
        }
    }
    
}

I'm not getting any error but the child is not updating with new parent record. Can anyone please suggest any changes in my code if I missed anything.

1 Answer 1

1

Automatic updates without DML only happens when:

  • You are working in a before trigger context
  • And you are making modifications to the instance of a record stored in Trigger.new or Trigger.newMap

If you need the Id of a newly inserted record to perform a query, then you'd have no choice but to use an after insert trigger. That doesn't appear to be the case here though.

You either need to change your trigger to work on before insert, or make an explicit DML update.

3
  • It didnt work with both before insert and after insert Derek.
    – SK_112
    Commented Jun 24, 2020 at 15:17
  • @SK_112 Then you need to capture some debug logs and do some investigation and basic debugging. If, for example, the soql query feeding your soql for loop that populates mapATwithATP returns 0 rows (the number of rows returned by each query is included in the debug log at the default log levels), then you'll never execute the c.Account_Transfer__c = mapATwithATP.get(c.District_Acct_Code__c).Id; line.
    – Derek F
    Commented Jun 24, 2020 at 15:26
  • I have around 10 records which are created today and those records were returned in the query mapATwithATP. I'm wondering what did i miss here.
    – SK_112
    Commented Jun 24, 2020 at 15:41

You must log in to answer this question.

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