Updated trigger
trigger updateBH2 on CloudbyzITPM__Project__c (before insert, before update, before delete, after insert, after update, after delete)
{
List<CloudbyzITPM__Project__c> projectsToProcess = new List<CloudbyzITPM__Project__c>();
if((Trigger.isInsert || Trigger.isUpdate) && Trigger.isAfter)
{
for(CloudbyzITPM__Project__c project : (List<CloudbyzITPM__Project__c>)Trigger.new)
{
projectsToProcess.add(project);
}
}
if(Trigger.isBefore && Trigger.isDelete){
for(CloudbyzITPM__Project__c project : (List<CloudbyzITPM__Project__c>)Trigger.old){
projectsToProcess.add(project);
}
}
if(Trigger.isUpdate && Trigger.isAfter){
for(CloudbyzITPM__Project__c project : (List<CloudbyzITPM__Project__c>)Trigger.old){
projectsToProcess.add(project);
}
}
Set<Id> impactedPortFolios = new Set<Id>();
for(CloudbyzITPM__Project__c project: projectsToProcess)
{
if(!String.isBlank(project.CloudbyzITPM__Project_Portfolio__c))
impactedPortFolios.add(project.CloudbyzITPM__Project_Portfolio__c);
}
List<AggregateResult> agResults = [SELECT SUM(CloudbyzITPM__Budgeted_Hours__c) budgetedHours, CloudbyzITPM__Project_Portfolio__c portfolio FROM CloudbyzITPM__Project__c WHERE CloudbyzITPM__Project_Portfolio__c IN :impactedPortFolios
GROUP BY CloudbyzITPM__Project_Portfolio__c];
Map<Id, Decimal> portfolioVsBudget = new Map<Id, Decimal>();
for(AggregateResult result : agResults)
{
Id portfolioId = (ID) result.get('portfolio');
Decimal budgetedHours = (Decimal) result.get('budgetedHours');
if(!portfolioVsBudget.containsKey(portfolioId))
{
portfolioVsBudget.put(portfolioId, 0.0);
}
portfolioVsBudget.put(portfolioId, portfolioVsBudget.get(portfolioId) + budgetedHours);
}
List<CloudbyzITPM__Project_Portfolio__c> portfoliosToUpdate = new List<CloudbyzITPM__Project_Portfolio__c>();
for(Id pfId : portfolioVsBudget.keySet())
{
portfoliosToUpdate.add(new CloudbyzITPM__Project_Portfolio__c(Id = pfId, CloudbyzITPM__Budgeted_Hours__c = portfolioVsBudget.get(pfId)));
}
update portfoliosToUpdate;
}
Hi Deepak, Checkout below code which should solve your problem
public class updateBudgetedHours
{
public static void processAfterUpdateBudgetedHours()
{
List<CloudbyzITPM__Project__c> projectsToProcess = new List<CloudbyzITPM__Project__c>();
if((Trigger.isInsert || Trigger.isUpdate) && Trigger.isAfter){
for(CloudbyzITPM__Project__c project : (List<CloudbyzITPM__Project__c>)Trigger.new){
projectsToProcess.add(project);
}
}
if(Trigger.isBefore && Trigger.isDelete){
for(CloudbyzITPM__Project__c project : (List<CloudbyzITPM__Project__c>)Trigger.old){
projectsToProcess.add(project);
}
}
if(Trigger.isUpdate && Trigger.isAfter){
for(CloudbyzITPM__Project__c project : (List<CloudbyzITPM__Project__c>)Trigger.old){
projectsToProcess.add(project);
}
}
Set<Id> impactedPortFolios = new Set<Id>();
for(CloudbyzITPM__Project__c project: projectsToProcess){
impactedPortFolios.add(project.CloudbyzITPM__Project_Portfolio__c);
}
List<AggregateResult> agResults = [ SELECT SUM(CloudbyzITPM__Budgeted_Hours__c) budgetedHours, CloudbyzITPM__Project_Portfolio__c portfolio
FROM CloudbyzITPM__Project__c
WHERE CloudbyzITPM__Project_Portfolio__c IN :impactedPortFolios
GROUP BY CloudbyzITPM__Project_Portfolio__c];
Map<Id, Decimal> portfolioVsBudget = new Map<Id, Decimal>();
for(AggregateResult result : agResults){
Id portfolioId = (ID) result.get('portfolio');
Decimal budgetedHours = (Decimal) result.get('budgetedHours');
if(!portfolioVsBudget.containsKey(portfolioId)){
portfolioVsBudget.put(portfolioId, 0.0);
}
portfolioVsBudget.put(portfolioId, portfolioVsBudget.get(portfolioId) + budgetedHours);
}
List<CloudbyzITPM__Project_Portfolio__c> portfoliosToUpdate = new List<CloudbyzITPM__Project_Portfolio__c>();
for(Id pfId : portfolioVsBudget.keySet()){
portfoliosToUpdate.add(new CloudbyzITPM__Project_Portfolio__c(Id = pfId, CloudbyzITPM__Budgeted_Hours__c = portfolioVsBudget.get(pfId)));
}
update portfoliosToUpdate;
}
}
Here it is- trigger updateBH2 on CloudbyzITPM__Project__c (before insert, before update, before delete, after insert, after update, after delete){ List<CloudbyzITPM__Project__c> projectsToProcess = new List<CloudbyzITPM__Project__c>(); if((Trigger.isInsert || Trigger.isUpdate) && Trigger.isAfter) { for(CloudbyzITPM__Project__c project : (List<CloudbyzITPM__Project__c>)Trigger.new) { projectsToProcess.add(project); } } if(Trigger.isBefore && Trigger.isDelete){ for(CloudbyzITPM__Project__c project : (List<CloudbyzITPM__Project__c>)Trigger.old){ projectsToProcess.add(project); } } if(Trigger.isUpdate && Trigger.isAfter){ for(CloudbyzITPM__Project__c project : (List<CloudbyzITPM__Project__c>)Trigger.old){ projectsToProcess.add(project); } } Set<Id> impactedPortFolios = new Set<Id>(); for(CloudbyzITPM__Project__c project: projectsToProcess) { impactedPortFolios.add(project.CloudbyzITPM__Project_Portfolio__c); } List<AggregateResult> agResults = [SELECT SUM(CloudbyzITPM__Budgeted_Hours__c) budgetedHours, CloudbyzITPM__Project_Portfolio__c portfolio FROM CloudbyzITPM__Project__c WHERE CloudbyzITPM__Project_Portfolio__c IN :impactedPortFolios GROUP BY CloudbyzITPM__Project_Portfolio__c]; Map<Id, Decimal> portfolioVsBudget = new Map<Id, Decimal>(); for(AggregateResult result : agResults) { Id portfolioId = (ID) result.get('portfolio'); Decimal budgetedHours = (Decimal) result.get('budgetedHours'); if(!portfolioVsBudget.containsKey(portfolioId)) { portfolioVsBudget.put(portfolioId, 0.0); } portfolioVsBudget.put(portfolioId, portfolioVsBudget.get(portfolioId) + budgetedHours); } List<CloudbyzITPM__Project_Portfolio__c> portfoliosToUpdate = new List<CloudbyzITPM__Project_Portfolio__c>(); for(Id pfId : portfolioVsBudget.keySet()) { portfoliosToUpdate.add(new CloudbyzITPM__Project_Portfolio__c(Id = pfId, CloudbyzITPM__Budgeted_Hours__c = portfolioVsBudget.get(pfId))); } update portfoliosToUpdate; } Looking at the error message it seems trigger named updateBH2 is failing. Can you paste the trigger code to debug your issue. Hi lalit, when iam not selecting any "Project Portfolio" in "project" object. it should then save but it is giving error-
6 answers