Skip to main content The Trailblazer Community will be unavailable from 2/1/2025 to 2/2/2025. Please plan your activities accordingly.

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
6 answers
  1. Nov 14, 2016, 12:05 PM

    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;

    }

  2. Nov 14, 2016, 10:21 AM
    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;

    }

    }

     
  3. Nov 14, 2016, 12:00 PM
    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; 

            }

  4. Nov 14, 2016, 11:16 AM
    Hi lalit, 

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

    User-added image
  5. Nov 14, 2016, 10:57 AM
    Thanks a lot lalit, its working fine
0/9000