You need to sign in to do that
Don't have an account?
3421
I am writing a trigger on a child (M-D relationship) where if the child object had duplicate records, update the count of duplicate on the Master. The duplicate child is determined if there is a lookup field match. The Lookup field here is Itm__c
Here is the code:
Duplicate Child Count on Master
I am writing a trigger on a child (M-D relationship) where if the child object had duplicate records, update the count of duplicate on the Master. The duplicate child is determined if there is a lookup field match. The Lookup field here is Itm__c
Here is the code:
trigger DuplicateInvceLns on Invce_Lne__c (before insert,before update,after insert, After Delete, After Undelete) { Set<Id> setInvceIds = new Set<Id>(); Set<Id> ivtmp = new Set<Id>(); Map<Id,Invce_Lne__c> inl = new Map<Id,Invce_Lne__c>(); for(Invce_Lne__c iv : Trigger.new){ ivtmp.add(Invce__c); } for(Invce_Lne__c invcelne : [select Id,Name,Itm__c, Invce__c from Invce_Lne__c where Invce__c IN : ivtmp]) inl.put(invcelne.Itm__c, invcelne); for(Invce_Lne__c invcelne : Trigger.new) { if(invl.containsKey(invcelne.Itm__c)){ if(Trigger.isInsert || Trigger.isUndelete){ setInvceIds.add(invcelne.Invce__c); system.debug(invcelne.Invce__c); system.debug(setInvceIds); } } else if(Trigger.isDelete){ setInvceIds.add(invcelne.Invce__c); system.debug(setInvceIds); } } List<Invce__c> lstInvce = [Select Id,name, (select id from Invce_Lns__r) from Invce__c where Id IN :setInvceIds]; for(Invce__c inv : lstInvce) { inv.Duplicate__c = inv.Invce_lns__r.size(); system.debug(inv.OrderApi__Invoice_lines__r.size()); } update lstInvce; }
Am I missing something? you have not mentioned your problem/issue which needs help
from your code,
08 for(Invce_Lne__c iv : Trigger.new){
09 ivtmp.add(Invce__c); // Is not supposed to be iv.Invce__c)
10 }
Two issues I had here is when I try deleting any of the child record It is throwing me an error saying " Attempt to de-reference null object" and the line number is for(Invce_Lne__c iv : Trigger.new){
09 ivtmp.add(Invce__c);
10 }
The other issue is the duplicate count on the trigger is having the count of all the child records, rather than just the count of duplicate records.