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
Karen Davis 1Karen Davis 1 

Can I write a trigger to update a field on account from a child object?

I’m trying to write a before delete trigger on a custom object called  “Dental_Assessment” so that if the ID of the record being deleted is the same as a field on the account it will set that field on the account to null. "Dental_Assessment" is a child object of custom object "Treatment" which is a child object of Account. I thought I had it, but I can’t figure out how to get it to update the account record. can anyone help me with the syntax for doing this?
 
This is my trigger, except line 5 doesn’t work:
 
trigger DeleteID on Dental_Assessment__c (before delete) {
 if(System.Trigger.IsDelete) {
  for (Dental_Assessment__c Assmt: trigger.old) {
    if(Assmt.ID = Assmt.treatment__c.individual__c.Last_Exam_Assmt_Form_ID__c) {
    Assmt.treatment__c.individual__c.Last_Exam_Assmt_Form_ID__c = null; 
   }
   }
   }
}
 
Best Answer chosen by Karen Davis 1
Ankit SehgalAnkit Sehgal
trigger UpdateAccount on Dental_Association__c(before delete)
{
    //getting id of parent treatment
    Id TId=[SELECT Treatment__c,id FROM Dental_Association__c WHERE Id IN: Trigger.old].Treatment__c;
    // getting id of parent Account
	Id AccId=[SELECT Account__c,id FROM Treatment__c WHERE Id =:TId].Account__c;
    //querying the required account
	Account acc=[SELECT Last_Exam_Assmt_Form_ID__c FROM Account WHERE Id =: AccId LIMIT 1];
	for(Dental_Association__c d: Trigger.old)
	{
		if(d.id==Acc.Last_Exam_Assmt_Form_ID__c)
		{
				acc.Last_Exam_Assmt_Form_ID__c=null;
		}
	}
	update acc;
}
i am assuming there is a field on account "Last_Exam_Assmt_Form_ID__c" of text type which has the id of Dental_Association__c  object.
 

All Answers

Ankit SehgalAnkit Sehgal
trigger UpdateAccount on Dental_Association__c(before delete)
{
    //getting id of parent treatment
    Id TId=[SELECT Treatment__c,id FROM Dental_Association__c WHERE Id IN: Trigger.old].Treatment__c;
    // getting id of parent Account
	Id AccId=[SELECT Account__c,id FROM Treatment__c WHERE Id =:TId].Account__c;
    //querying the required account
	Account acc=[SELECT Last_Exam_Assmt_Form_ID__c FROM Account WHERE Id =: AccId LIMIT 1];
	for(Dental_Association__c d: Trigger.old)
	{
		if(d.id==Acc.Last_Exam_Assmt_Form_ID__c)
		{
				acc.Last_Exam_Assmt_Form_ID__c=null;
		}
	}
	update acc;
}
i am assuming there is a field on account "Last_Exam_Assmt_Form_ID__c" of text type which has the id of Dental_Association__c  object.
 
This was selected as the best answer
Karen Davis 1Karen Davis 1
THANK YOU! So very much! Your assumption was correct. Thank you for solving this problem. I have this process that saves the last one so we can clone it with a button but it developed issues if one got deleted so I needed this trigger. I so much appreciate the help.
GauravGargGauravGarg
Hi Karen,

Please find below code for updating Account based on Dental Assessment:
Trigger Delete_Dental_assessment on Dental_Assessment__c (after delete){
	List<Id> treatmentId = new List<Id>();
	List<Id> accountId = new List<ID>();
	List<Account> accountList = new List<Account>();
	for(Dental_Assessment__c dsa : trigger.new)
		treatmentId.add(dsa.treatment__c);
	Map<Id,Treatment__c> treatmentMap= new Map<Id,Treatment__c>();
	for(Treatment__c tt: [select id, Account__c from Treatment__c where Id In: treatmentId]){
		treatmentMap.put(tt.id, tt);
		accountId.add(tt.account__c);
	}
	Map<id,Account> accountMap = new Map<id,Account>([select id, name,Last_Exam_Assmt_Form_ID__c from account where id IN: accountId ]);
	
	
	for(Dental_Assessment__c dsa: trigger.new){
		if(treatmentMap != null && treatmentMap.size() >0 && accountMap != null && accountMap.size() > 0){
			for(treatment__c tt: treatmentMap.values()){
				if(accountMap.get(tt.account__c).Last_Exam_Assmt_Form_ID__c = dsa.id){
					accountMap.get(tt.account__c).Last_Exam_Assmt_Form_ID__c = null;
					accountList.add(accountMap.get(tt.account__c));
				}
			}
		}
	}
	if(accountList != null && accountList.size() >0)
		update accountList;
}

Hope this will help you, also let me know if you find any issues on this.

Thanks,
Gaurav
Email: gauravgarg.nmims@gmail.com
Karen Davis 1Karen Davis 1
Ack, ok, so the trigger works perfectly. I forgot that to upload it I was going to have to have a test class and I cannot seem to write any sort of test class that works! Help.
Karen Davis 1Karen Davis 1
Nevermind, I managed to get the test case to work. Test cases are harder than triggers (for someone who's not really a developer).