Skip to main content
Hi there,

I am working on a code  to sum up value in a custom field  'Total_Months_in_this_Position__c' of a custom object 

'TargetX_SRMb__Family_Relationship__c' and add the sum to a custom field 'Total_Months_Employed__c' of a related custom object 'TargetX_SRMb__Application__c'. 

One 'TargetX_SRMb__Application__c' can have multiple TargetX_SRMb__Family_Relationship__c. The relationship is a lookup.

Below is the code I wrote, I am having a hard time fixing the error on the last line.

Any help will be very much appreciated.

trigger CalcTotalMonths on TargetX_SRMb__Family_Relationship__c (After insert, After Update) {

    // gets a set of all application ids and application name with relationship

    Set<Id>applicationIds = new Set<Id>();   

    Map<Id,TargetX_SRMb__Application__c>appToUpdate =new Map<Id,TargetX_SRMb__Application__c>();

    Map<decimal,decimal>monthsWorked = new Map<decimal,decimal>();

    

    // collect the application ids of all new relationships with employer name

    if(trigger.isInsert){

    for (TargetX_SRMb__Family_Relationship__c Rel : Trigger.new)

    if(!string.isblank(Rel.TargetX_SRMb__Employer__c) && !string.isBlank(Rel.TargetX_SRMb__Contact__c)

      

      && (Rel.TargetX_SRMb__Relationship__c == 'Current Employer' || Rel.TargetX_SRMb__Relationship__c == 'Previous Employer'))

       

    {applicationIds.add(Rel.TargetX_SRMb__Application__c);

        }

        

        // collect the application ids of all updated relationships with employer name

      if(Trigger.isUpdate){

        

      for(TargetX_SRMb__Family_Relationship__c Rel : Trigger.new)

        {

     if((Trigger.oldMap.get(Rel.id).Total_Months_in_this_Position__c != Trigger.newMap.get(Rel.id).Total_Months_in_this_Position__c) 

      && !string.isBlank(Rel.TargetX_SRMb__Contact__c)

      && (Rel.TargetX_SRMb__Relationship__c == 'Current Employer' || Rel.TargetX_SRMb__Relationship__c == 'Previous Employer') )

            {

            Applicationids.add(Rel.TargetX_SRMb__Application__c); 

            }

        }

        

        //   Use aggregate query to get the total months worked for each relationship

          

          for(AggregateResult result :[Select Count(id) Total,SUM(Total_Months_in_this_Position__c) Months , TargetX_SRMb__Application__c FROM TargetX_SRMb__Family_Relationship__c

                WHERE TargetX_SRMb__Application__c IN :Applicationids GROUP BY TargetX_SRMb__Application__c               

                                      ])

          {  Decimal Months = (Decimal)result.get('Months');

             Decimal Total = (Decimal)result.get('Total');

           System.debug('Total Months Worked'+ 'Total_Months_in_this_Position__c' );

           Decimal field = monthsWorked.get(Months);

           if ( field != null)

               {

               TargetX_SRMb__Application__c app = appToUpdate.get((id)result.get('TargetX_SRMb__Application__c')); 

               app.Total_Months_Employed__c =  field;

               app.put(Total,field);

               }

          }

       

              }

      

}

}

Thanks,

Mariam
16 answers
  1. Feb 27, 2018, 6:05 AM
    Hi Mariam,

    There are some issues in your code. There is no DML in your code which will not update the Parent Object.

    Can you please refer the below code where i have used Account and Contact Object.

    Object: Account, Custom Field: Acc_Cost__c

    Object: Contact, Custom Field: Contact_Cost__c

     

    trigger UpdateAccCost on Contact (after insert, after update, after delete, after undelete) {

    Map<ID,Account> accountMap = new Map<ID,Account>();

    List<ID> accID = new List<ID>();

    //Double TotalCost = 0;

    //Check for type of DML Operation, For Insert, Update and Undelete we have Trigger.New

    if(Trigger.IsInsert || Trigger.IsUpdate || Trigger.IsUndelete){

    for(Contact C1: Trigger.new){

    //Add all new AccountID in the List

    if(C1.AccountID != NULL)

    accID.add(C1.AccountID);

    }

    }

    //Check for type of DML Operation, For Update and Delete we have Trigger.Old

    if(Trigger.IsDelete){

    for(Contact C2: Trigger.old){

    if(C2.AccountID != NULL)

    accID.add(C2.AccountID);

    }

    }

    //Check if List is not empty

    if(accID!=NULL && accID.size()>0){

    //Add all the accounts in the Map, to map ID with the Account Cost

    for(ID AccountID : accID){

    accountMap.put(AccountID, new Account(ID = AccountID, Acc_Cost__c = 0));

    }

    //Calculate the Total Account Cost based on the Contact Cost

    for(Contact C : [SELECT ID, AccountID, Contact_Cost__c FROM Contact WHERE AccountID IN :accID]){

    accountMap.get(C.AccountID).Acc_Cost__c += c.Contact_Cost__c;

    }

    //Commit to the Database

    Database.update(accountMap.values());

    }

    }

    Change the Custom Field and Object name according to your requirement.

    Changes for you will be like:

    * Contact Object to TargetX_SRMb__Family_Relationship__c Custom Object

    * Contact_Cost__c of Contact Object to Total_Months_in_this_Position__c of TargetX_SRMb__Family_Relationship__c Object

    * Account Object to TargetX_SRMb__Application__c Custom Object

    * Acc_Cost__c of Account Object to Total_Months_Employed__c of ​​TargetX_SRMb__Application__c Object

    Please try to implement the same with the changes and let me know if it works.

    Please mark this as the solution if it solved your purpose to help the Community grows.

    Thanks,

    Jainam Contractor,

    Salesforce Consultant,

    Varasi LLC

    www.varasi.com

     
0/9000