I want whenever child field is inserted or updated it should update parent field as

Parent field value = child field value

Suppose my parent field value is . When i enter first child and its value 100, then it should update parent field value = 100.

1 Answer 1


1) To bulkify your trigger, you need to gather all used projects in one Map to have an immediate access to it inside the for-loop on child object.

2) To process isUpdate trigger, you need to reduce current project value by the amount of budject hours on task from Trigger.oldMap, so that new value will be added clearly.

Here is the code:

trigger budgetHoursCompleted on Krow__Task__c (after insert, after update, after delete) {
    Set<Id> projectsIds = new Set<Id>();
    List<Krow__Task__c> tasksToRun = (Trigger.isDelete ? Trigger.OLD : Trigger.NEW);

    for (Krow__Task__c task_i : tasksToRun) {

    Map<Id, Krow__Project__c> projectsToUpdate = new Map<Id, Krow__Project__c>([
            SELECT Id, Budget_Completed__c
            FROM Krow__Project__c
            WHERE Id IN :projectsIds

    for (Krow__Task__c task_i : tasksToRun) {
        Krow__Project__c tmp = projectsToUpdate.get(task_i.Krow__Project__c);

        if (Trigger.isUpdate || Trigger.isDelete) {
            tmp.Budget_Completed__c = tmp.Budget_Completed__c - Trigger.oldMap.get(task_i.Id).Budget_Hours_Completed__c;

        if (Trigger.isUpdate || Trigger.isInsert) {
            tmp.Budget_Completed__c = tmp.Budget_Completed__c + task_i.Budget_Hours_Completed__c;

    update projectsToUpdate.values();
  • @John I have updated the answer. The only thing you need to do is specifying the trigger for after delete and remove old value from parent field if trigger is update or delete. Same for update and insert you need to add new value. Oct 15, 2019 at 9:49
  • @John Updated code, delete Trigger does not have Trigger.NEW, so you need to assign appropriate list to the tasksToRun. Oct 15, 2019 at 13:22
  • I have updated mine, you can follow the example or work out what's wrong in your code by yourself. If you encounter some particular problem, feel free to ask, but try to research it on your own. Oct 15, 2019 at 13:48

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.