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
kali charankali charan 

After deleting the record the parent amount field should be updated with previous amount

HI everyone,

   I have struct with one of the requirement that i have the parent account and multiple child contacts and in the parent account i have the amount field that is populated from the child amount .If the parent amount has 25 and again i have inserted new child contact with 30 .when i have deleted the 30 amount record from contact it should get update as previous 25 in the parent amount Ihave written the trigger for insert and update and am not getting for delete .

Please help me how to write the trigger for delete functionality using trigger ,thanks.
Lokeswara ReddyLokeswara Reddy
Hi kali,

If the objects havae MD relationship then you can easily do it with roll up summary field.
If you have look up relationship then you can try this logic.

trigger <name> (before delete){

List<Objectname> objList = trigger.old;
for(ObjectName obj:objList){
    if(trigger.isdelete){
       // Logic to subtract amount of delete record
      // parentAmount = parentAmount - obj.amount;
    }
}
}
kali charankali charan
its not MD it is lookup
Lokeswara ReddyLokeswara Reddy
Have you updated you trigger to have the logic on delete event as suggested above?
kali charankali charan
That's what am not getting how to update the trigger after deleting the value
Lokeswara ReddyLokeswara Reddy
Looks like you have overlooked my first post, you can achieve this by having this code snippet,
- Add before delete event in your trigger, trigger <name> (before insert, before update, before delete)
- Copy this code into your trigger and add your business logic, similar to the way how it is being handled for Insert and Update.


List<Objectname> objList = trigger.old;
for(ObjectName obj:objList){
    if(trigger.isdelete){
       // Logic to subtract amount of delete record
      // parentAmount = parentAmount - obj.amount;
    }
}