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
Churchill19Churchill19 

Apex trigger to update children records on bulk update from parent?

Hi,

I have a problem with my code below... when a update happens on the (Parent object) Invoice__c the trigger links all related (Child object) Inv_Item__c together as a lookup relationship. This works if i manual update a Parent record by my self it links all child records together.

below is a diagram to visual explain what i doing...

Before Trigger
 
Invoice 1
                Inv_item1                Inv_item2                Inv_item3                Inv_item4

After Trigger
 
Invoice 1
                Inv_item1   ------>    Inv_item2   ------>    Inv_item3   ------>    Inv_item4

But when i bulk the trigger i get the following error message
 
Update failed. First exception on row 4 with id a21R0000000LrddIAC; first error: FIELD_FILTER_VALIDATION_EXCEPTION, Value does not exist or does not match filter criteria

When i go to debug this its trying to link the another Inv_item__c with another Invoice__c below diagram: - 

Invoice 1
                Inv_item1   ------>    Inv_item2   ------>    Inv_item3   ------>    Inv_item4

Invoice 2
                Inv_item1               Inv_item2               Inv_item3               Inv_item4

so if you look at the diagram above its trying to link Inv_item1 on Invoice 2 to Inv_item4 on Invoice 1.

Any ideas to help resolve this please?

thanks..

code is below: - 
 
trigger ChildJoin on Invoice__c (after update)


{
     
    Set<ID> par = new Set<ID>();
	List<Inv_item__c> child = new List<Inv_item__c>();
    List<Inv_item__c> child2 = new List<Inv_item__c>();
    
    for(Invoice__c inv1 : Trigger.new){
       
            par.add(inv1.Id);
        
    }
    List<Invoice__c> updateInv = [SELECT Id,(Select Id, Predecessor_item__c, IDNo__c from Invoice_Items__r)  FROM Invoice__c WHERE Id in :par];
 
    List<Inv_item__c> relatedItemsToUpdate = new List<Inv_item__c>();
	
				child = [SELECT ID,Due_Date__c, Invoice_ID__c 
                                                          FROM Inv_item__c
                                                          WHERE Invoice_ID__c IN :par
                                                          AND IDNo__c = 1];
				child2 = [SELECT ID,Due_Date__c, Invoice_ID__c 
                                                          FROM Inv_item__c
                                                          WHERE Invoice_ID__c IN :par
                                                          AND IDNo__c = 2];
    
    for (Invoice__c inv1 : updateInv){
        // Loop through each Related Invoice Item record
        for(Inv_item__c rd : inv1.Invoice_Items__r)
        {
		
		
          
			if(rd.IDNo__c == 2)
            
        {
        
            
            rd.Predecessor_item__c = child[0].id;
        
        }   
        
        if(rd.IDNo__c == 3)
            
        {
        
            
            rd.Predecessor_item__c = child2[0].id;
        	
        }
			
			
			
			
            relatedItemsToUpdate.add(rd);
        
    }
    
    }
    
    update relatedItemsToUpdate;
 }

 
hitesh90hitesh90
Hello Churchill,

I think this issue is because of there is a Lookup filter on "Predecessor_item__c" field of "Inv_item__c" custom object.
and your trigger doesn't meet criteria for that at line "rd.Predecessor_item__c = child[".

This is the standard validation message of Lookup filter.

Thank You,
Hitesh Patel
Email :- hiteshpatel.aspl@gmail.com
http://mrjavascript.blogspot.in/