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) 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. – Mariia Illarionova Oct 15 at 9:49
  • @John Updated code, delete Trigger does not have Trigger.NEW, so you need to assign appropriate list to the tasksToRun. – Mariia Illarionova Oct 15 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. – Mariia Illarionova Oct 15 at 13:48

Your Answer

