
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__cObject: 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 ObjectPlease 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
16 answers