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
DVSDVS 

Trying to update parent from Child. What am I doing wrong?

 

When my custom object (which is the child) is updated, I want to update the Case (parent) which is in the custom object's lookup field.  Here is what I have so far but it doesn't seem to be kicking off when I go in and update the custom object.

 

trigger updateRelatedCasesFromCR on Case_CR__c (after update){

Set <Id> Case_Ids = new Set<Id>();
for (Case_CR__c child: trigger.new) 
	Case_Ids.add(child.Case__c);
List<Case> Cases_To_Update = [select o.id, o.CR_Updated__c from Case o where o.id in :Case_Ids];
for(Case o: Cases_To_Update)
{ 
	o.CR_Updated__c = datetime.now();
}
update Cases_To_Update;
}

 

Best Answer chosen by Admin (Salesforce Developers) 
souvik9086souvik9086

Hi just add this blue colored text in your code

 


trigger updateRelatedCasesFromCR on Case_CR__c (after update){

Set <Id> Case_Ids = new Set<Id>();
List<Case> updateCaseList = new List<Case>();
for (Case_CR__c child: trigger.new)
Case_Ids.add(child.Case__c);
List<Case> Cases_To_Update = [select o.id, o.CR_Updated__c from Case o where o.id in :Case_Ids];
for(Case o: Cases_To_Update)
{
o.CR_Updated__c = datetime.now();
updateCaseList.add(o);
}

if(updateCaseList.size()>0){

update updateCaseList;

}
}

 

If this post solves your problem kindly mark it as solution. if this post is helpful please throw Kudos.

Thanks

All Answers

sushant sussushant sus
Hi,
do this
Case_Ids.add(child.id); line no 5
sushant sussushant sus
no need of writing this

update Cases_To_Update;
souvik9086souvik9086

Hi just add this blue colored text in your code

 


trigger updateRelatedCasesFromCR on Case_CR__c (after update){

Set <Id> Case_Ids = new Set<Id>();
List<Case> updateCaseList = new List<Case>();
for (Case_CR__c child: trigger.new)
Case_Ids.add(child.Case__c);
List<Case> Cases_To_Update = [select o.id, o.CR_Updated__c from Case o where o.id in :Case_Ids];
for(Case o: Cases_To_Update)
{
o.CR_Updated__c = datetime.now();
updateCaseList.add(o);
}

if(updateCaseList.size()>0){

update updateCaseList;

}
}

 

If this post solves your problem kindly mark it as solution. if this post is helpful please throw Kudos.

Thanks

This was selected as the best answer