+ Start a Discussion
sf ostsf ost 

Field Update on Parent Record based on a field in all associated Child Records

Hi,

There are two Custom Objects, Object1 (Parent), Object2 (Child). Both are in MD relationship.

Object1 (Parent) has a picklist field "Statuc__c" with values InProgress & Completed.
Object2 (Child) has a field of CheckBox datatype "Completed__c".

Now. When the value of checkbox Completed__c is true in all the records of an associated parent record. then only the filed Status__c in Object1 should change to the value Completed.

How can I achieve this?

Your suggestion will be appreciated.
Ramon PereiraRamon Pereira
Hello,

Create a trigger in your child object and every time after the upgrade you check these fields brethren in objects than has been updated. If all objects meet the criteria, you update the parent object.

Example:
 
YourObjectParent__c parentObject; 
boolean check = false; 
Set<YourObjectChild__c> childsObjects = new Set<YourObjectChild__c>([SELECT Name, Completed__c 
	FROM YourObjectChild__c Where parentID =: 'YOUR PARENT ID' ]);

 	for(YourObjectChild__c child : childsObjects){
		if(child.Completed__c == false){
			check = true; 
		}
	}
	
	if(check == false){
		parentObject.Statuc__c = true; 
	}
Obs: Because it is trigger, be sure to follow in the SFDC standards (https://developer.salesforce.com/page/Trigger_Frameworks_and_Apex_Trigger_Best_Practices):
sf ostsf ost
I got this error.

Error: Compile Error: No such column 'parentID' on entity 'Project__c'. If you are attempting to use a custom field, be sure to append the '__c' after the custom field name. Please reference your WSDL or the describe call for the appropriate names. at line 7 column 13
 
trigger updateParent on Project__c (after insert, after update) {
boolean check = false; 
Plan__c parentObj;
List<Plan__c> somePlan; 

List<Project__c> childsObj = new List<Project__c>();
childsObj = [SELECT Name, Completed__c FROM Project__c Where parentID =:somePlan] ;

    for(Project__c child : childsObj){
        if(child.Completed__c == false){
            check = true; 
        }
    }
    
    if(check == false){
        parentObj.Status__c = 'In Progress'; 
    }
}

 How to overcome this error?
Mathew Andresen 5Mathew Andresen 5
parentID needs to be whatever the field name is that has the parent ID in it.  For example, object2 would normally have a parentID field name of object1__C

 
Niki Glick 1Niki Glick 1
I have a similar requirement but have never worked with triggers before... any help would be greatly appreciated.

I have field on the Order object called Total_Number_of_Canvases__c which is a Roll-Up Summary (SUM Order Product)

I have another field on the Order object called Shipping_Charges__c which is updated via workflow field update (Total_Number_of_Canvases__c * 3)

Problem is, whenever I add/remove order products, which in turn updates the Total Number of Canvases field, the Shipping Charges field is calculating based on the field's prior value.

Can I use a trigger to run when order products/Total Number of Canvases is updated so that Shipping Charges are calculated based on the current value of Total Number of Canvases?

Thanks