+ Start a Discussion
Deepak Sharma 184Deepak Sharma 184 

trigger scenario -



Hi Expert,

I have a scenerio in which i have to update the field "budget hours" of "project portfolio" object with the "budget hour" field of "project" object, hence 1 "project portfolio" have multiple "project", it should have all the sum of "budget hour" field into that "budget hours" of "project portfolio" object. 

But when we cange the "project portfolio" in "project" object. so in the "budget hours" field of "project portfolio" should deacreases, coz we have change the "project portfolio" of that partcular "project" and add the "budget hours" of another "project portfolio" which we have added. but it is not hapenning in my org. 

Code is here- 

public class updateBudgetedHours 
{

  public static List<CloudbyzITPM__Project__c> affectedBudgetedHours = new List<CloudbyzITPM__Project__c>();
 public static set<ID> affc = new set<ID>();
 public static void processAfterUpdateBudgetedHours()
 {
     affectedBudgetedHours = (List<CloudbyzITPM__Project__c>)Trigger.New;
     if(affectedBudgetedHours.size()>0){
     for(CloudbyzITPM__Project__c  affc1 : affectedBudgetedHours)
     {
         if(affc1.CloudbyzITPM__Project_Portfolio__c != null)
         {
      affc.add(affc1.CloudbyzITPM__Project_Portfolio__c);
     }
     }
     
    if(affc.size()>0)
    {
    CloudbyzITPM__Project_Portfolio__c fn = [select id,name, CloudbyzITPM__Budgeted_Hours__c from CloudbyzITPM__Project_Portfolio__c where id IN :affc limit 1];
    list<CloudbyzITPM__Project__c> affectedBudgetedHours1 = [select id,name,CloudbyzITPM__Project_Portfolio__c,CloudbyzITPM__Budgeted_Hours__c from CloudbyzITPM__Project__c where CloudbyzITPM__Project_Portfolio__c = :fn.id];
    Decimal i=0;
       
    if(affectedBudgetedHours1.size()>0)
       {
           for(CloudbyzITPM__Project__C afc1 : affectedBudgetedHours1)
        {
            i = i + afc1.CloudbyzITPM__Budgeted_Hours__c;
        }
    }
    
 if(i>0)
 {
     System.debug('@@pBUFinal@@');
    fn.CloudbyzITPM__Budgeted_Hours__c = i;
    }
     update fn; 
      System.debug('@@fn4@@'+fn);
    }
    
    }
 }
}

Trigger- 

Trigger UpdateBH on CloudbyzITPM__Project__c (before insert, before update, before delete,after insert, after update, after delete) 

{
if((Trigger.isInsert || Trigger.isUpdate) && Trigger.isAfter )   
{
updateBudgetedHours.processAfterUpdateBudgetedHours();
}

if((Trigger.isDelete || Trigger.isUpdate) && Trigger.isBefore)
{
updateBudgetedHours.processAfterUpdateBudgetedHours();
}
}


what to change in the code?

Thanks & Regards,

Deepak
Best Answer chosen by Deepak Sharma 184
Lalit Mistry 21Lalit Mistry 21
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; 
        }

All Answers

Lalit Mistry 21Lalit Mistry 21
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;	
	}
}

 
Deepak Sharma 184Deepak Sharma 184
Thanks a lot lalit, its working fine
Deepak Sharma 184Deepak Sharma 184
Hi lalit, 

when iam not selecting any "Project Portfolio" in "project" object. it should then save but it is giving error- 

User-added image
Lalit Mistry 21Lalit Mistry 21
Looking at the error message it seems trigger named updateBH2 is failing. Can you paste the trigger code to debug your issue.
Deepak Sharma 184Deepak Sharma 184
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; 
        }



Lalit Mistry 21Lalit Mistry 21
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; 
        }
This was selected as the best answer