+ Start a Discussion
Nickolay IvanovichNickolay Ivanovich 

Create Trigger calculate the sum from field another object on id

I have Estimates__c,Project_c
Project.Estimated_Hours__c should have sum of Estimates__c.Total_Hours__c on id,
Trigger should be on Estimates__c(after insert)
, Estimates__c.Project__c link to Project_c

PLS HELP
I am begginerUser-added image
Best Answer chosen by Nickolay Ivanovich
Maharajan CMaharajan C
Hi Nickolay,

Try the below trigger:

trigger Test2 on Estimates__c(after insert, after update, after delete) {
    Set<Id> Ids= new Set<Id>();

    if(Trigger.isInsert || Trigger.isUpdate){
        for(Estimates__c estObj: Trigger.new){
            if(estObj.Project__c!=null){
                Ids.add(estObj.Project__c);
            }
        }
    }

    if(Trigger.isDelete){
       for(Estimates__c estObj: Trigger.old){
            if(estObj.Project__c!=null){
                Ids.add(estObj.Project__c);
            }
        }
    }
    
    if (!Ids.isEmpty()) {
        List<Project__c> projectsToUpdate = new List<Project__c>();
                
        for (AggregateResult ar : [SELECT SUM(Total_Hours__c), Project__c  FROM Estimates__c 
                                WHERE Project__c IN :Ids GROUP BY Project__c]) {
            Id proID = (ID)ar.get('Project__c');
            Decimal sum = (Decimal)ar.get('expr0');
            Project__c proj = new Project__c(Id=proID);
            proj.Estimated_Hours__c = sum;
            projectsToUpdate.add(proj);
        }

        if (!projectsToUpdate.isEmpty()) {
            update projectsToUpdate;
        }
    }
}

Can you please Let me know if it helps or not!!!

If it helps don't forget to mark this as a best answer!!!


Thanks,
Maharajan.C

All Answers

Nickolay IvanovichNickolay Ivanovich
trigger Test2 on Estimates__c (after insert,after update) {
    set<ID> ProjIds = new set<ID>();
    for(Estimates__c estObj:Trigger.new)
    {
        ProjIds.add(estObj.Project__c);
    }
    Map(id,Project__c) projMap = new Map(id,Project__c)([select Estimated_Amount__c,Estimated_Hours__c from Project__c where id in :ProjIds]);
    //How do loop for sum like this Project__c.Estimated_Hours__c+=Estimates__c.Total_Hours__c on id
    //
}
I have this
 
Maharajan CMaharajan C
Hi Nickolay,

Try the below trigger:

trigger Test2 on Estimates__c(after insert, after update, after delete) {
    Set<Id> Ids= new Set<Id>();

    if(Trigger.isInsert || Trigger.isUpdate){
        for(Estimates__c estObj: Trigger.new){
            if(estObj.Project__c!=null){
                Ids.add(estObj.Project__c);
            }
        }
    }

    if(Trigger.isDelete){
       for(Estimates__c estObj: Trigger.old){
            if(estObj.Project__c!=null){
                Ids.add(estObj.Project__c);
            }
        }
    }
    
    if (!Ids.isEmpty()) {
        List<Project__c> projectsToUpdate = new List<Project__c>();
                
        for (AggregateResult ar : [SELECT SUM(Total_Hours__c), Project__c  FROM Estimates__c 
                                WHERE Project__c IN :Ids GROUP BY Project__c]) {
            Id proID = (ID)ar.get('Project__c');
            Decimal sum = (Decimal)ar.get('expr0');
            Project__c proj = new Project__c(Id=proID);
            proj.Estimated_Hours__c = sum;
            projectsToUpdate.add(proj);
        }

        if (!projectsToUpdate.isEmpty()) {
            update projectsToUpdate;
        }
    }
}

Can you please Let me know if it helps or not!!!

If it helps don't forget to mark this as a best answer!!!


Thanks,
Maharajan.C
This was selected as the best answer
Nickolay IvanovichNickolay Ivanovich
Hi @Maharajan C, what is "expr0"? Code is work perfuctly, I'm so thankfull to you