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.