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
Rachel JonesRachel Jones 

Roll Up Trigger on Custom Objects

I am trying to create a roll up summary from one custom object to another:

Parent = Jobs (Job__c)
Child = Purchase Orders (Purchase_Order__c)
Roll up result field on jobs = Total_PO_Amount__c
Value to roll up on child object = Purchase_Order_Total__c (this is in itself a roll up summary of another custom object 'PO_Items__c)

I am getting the following error but not sure why?....
'Error: Compile Error: Didn't understand relationship 'Purchase_Order__c' in FROM part of query call. If you are attempting to use a custom relationship, be sure to append the '__r' after the custom relationship name. Please reference your WSDL or the describe call for the appropriate names. at line 15 column 20'
 
trigger RollUpValue on Purchase_Order__c (after insert, after delete, after undelete) {

    List<id> JobList = new List<id>();
    if(Trigger.isInsert || Trigger.isUndelete){
        For(Purchase_Order__c  PO1 : Trigger.new){
            JobList.add(PO1.id);
        }
    }
    if(Trigger.isDelete){
        For(Purchase_Order__c  PO1 : Trigger.old){
            JobList.add(PO1.id);
        }
    }
    List<Job__c> JobUpdateList = new List<Job__c>();
    For(Job__c J : [SELECT Total_PO_Amount__c, (SELECT Purchase_Order_Total__c FROM Purchase_Order__c) FROM Job__c WHERE id =: JobList]){
        J.Total_PO_Amount__c = J.Purchase_Order_Total__c.size();
        JobUpdateList.add(J);
    }
    try{
        update JobUpdateList ;
    }Catch(Exception e){
        System.debug('Exception :'+e.getMessage());
    }
}
Thanks in advance for any help (help I am not a coder so laymans terms would be helpful! :-)
 
Best Answer chosen by Rachel Jones
KaranrajKaranraj
I believe the issue is in the line number 6 and 12. You are storing purchase order object id instead of storing job id, try the updated code below
 
trigger RollUpValue on Purchase_Order__c (after insert, after delete, after undelete) {

    List<id> JobList = new List<id>();
    if(Trigger.isInsert || Trigger.isUndelete){
        For(Purchase_Order__c  PO1 : Trigger.new){
            JobList.add(PO1.Job__c);
        }
    }
    if(Trigger.isDelete){
        For(Purchase_Order__c  PO1 : Trigger.old){
            JobList.add(PO1.Job__c);
        }
    }
    List<Job__c> JobUpdateList = new List<Job__c>();
    for(Job__c J : [SELECT Total_PO_Amount__c, (SELECT Purchase_Order_Total__c FROM Purchase_Orders__r) FROM Job__c WHERE id =: JobList]){
        for(Purchase_Order__c pc: j.Purchase_Orders__r){
            J.Total_PO_Amount__c += pc.Purchase_Order_Total__c;
        }        
        JobUpdateList.add(J);
    }
    try{
        update JobUpdateList ;
    }Catch(Exception e){
        System.debug('Exception :'+e.getMessage());
    }
}

 

All Answers

KaranrajKaranraj
If its a custom relationship then you have to use __r in the query while refering the relationalship field in salesforce. Try the below query
SELECT Total_PO_Amount__c, (SELECT Purchase_Order_Total__c FROM Purchase_Orders__r) FROM Job__c WHERE id =: JobList]
Note: You have to use the relationalship field name while mentioning the child object in the sub query. 
Go to - >Setup->Your Child object->Click the relationship field
There you will get the Child Relationship Name, include the __r in that name and use that in your query
 
Rachel JonesRachel Jones
Thanks Karanraj!  That makes perfect sense and works.  However I am now getting the following error message:

'Error: Compile Error: Invalid field purchase_order_total__c for SObject Job__c at line 16 column 32'
J.Total_PO_Amount__c = J.Purchase_Order_Total__c.size();
The Purchase_Order_Total__c is a roll up summary field on the child object (its is rolling up an amount field from another custom object 'PO Items' which is a child of the child (confusing myself now!).  Any further help here would be much appreciated! 

 
KaranrajKaranraj
For one parent record you will multiple child records, so in your case for one job record you will have multiple purchase order. so from which purchase order you are going to the purchase order total value and store it in the parent object? 
Rachel JonesRachel Jones
I want to add up the values from all associated purchase orders (all children) to the parent (Job). 
KaranrajKaranraj
Try the below code
trigger RollUpValue on Purchase_Order__c (after insert, after delete, after undelete) {

    List<id> JobList = new List<id>();
    if(Trigger.isInsert || Trigger.isUndelete){
        For(Purchase_Order__c  PO1 : Trigger.new){
            JobList.add(PO1.id);
        }
    }
    if(Trigger.isDelete){
        For(Purchase_Order__c  PO1 : Trigger.old){
            JobList.add(PO1.id);
        }
    }
    List<Job__c> JobUpdateList = new List<Job__c>();
    for(Job__c J : [SELECT Total_PO_Amount__c, (SELECT Purchase_Order_Total__c FROM Purchase_Orders__r) FROM Job__c WHERE id =: JobList]){
        for(Purchase_Order__c pc: j.Purchase_Orders__r){
            J.Total_PO_Amount__c += pc.Purchase_Order_Total__c;
        }        
        JobUpdateList.add(J);
    }
    try{
        update JobUpdateList ;
    }Catch(Exception e){
        System.debug('Exception :'+e.getMessage());
    }
}

 
Rachel JonesRachel Jones
Really appreciate this help - I can now save the trigger above with no errors, but it is not populating any values in the Total_PO_Amount__c field on the Job (Parent) record.
KaranrajKaranraj
I believe the issue is in the line number 6 and 12. You are storing purchase order object id instead of storing job id, try the updated code below
 
trigger RollUpValue on Purchase_Order__c (after insert, after delete, after undelete) {

    List<id> JobList = new List<id>();
    if(Trigger.isInsert || Trigger.isUndelete){
        For(Purchase_Order__c  PO1 : Trigger.new){
            JobList.add(PO1.Job__c);
        }
    }
    if(Trigger.isDelete){
        For(Purchase_Order__c  PO1 : Trigger.old){
            JobList.add(PO1.Job__c);
        }
    }
    List<Job__c> JobUpdateList = new List<Job__c>();
    for(Job__c J : [SELECT Total_PO_Amount__c, (SELECT Purchase_Order_Total__c FROM Purchase_Orders__r) FROM Job__c WHERE id =: JobList]){
        for(Purchase_Order__c pc: j.Purchase_Orders__r){
            J.Total_PO_Amount__c += pc.Purchase_Order_Total__c;
        }        
        JobUpdateList.add(J);
    }
    try{
        update JobUpdateList ;
    }Catch(Exception e){
        System.debug('Exception :'+e.getMessage());
    }
}

 
This was selected as the best answer
Rachel JonesRachel Jones
Ok - we are making progress here - I now get the following error when trying to create a purchase order:

Apex trigger RollUpValue caused an unexpected exception, contact your administrator: RollUpValue: execution of AfterInsert caused by: System.NullPointerException: Attempt to de-reference a null object: Trigger.RollUpValue: line 17, column 1
Rachel JonesRachel Jones
Ok - it works if there is alreday a value in the Total_PO_Amount__c field, so I will default it to 0 - it works woohoo!!!   Thank you so much!!!!  :-)