function readOnly(count){ }
Starting November 20, the site will be set to read-only. On December 4, 2023,
forum discussions will move to the Trailblazer Community.
+ Start a Discussion
Tad MatjusenkoTad Matjusenko 

Trigger to display most recent value from child records on parent record

Hi All,

We have object Fee which is child of the Account object. Fee is calculated once a year per account and we need to display most recent Fee on Account level. 

Current fee is stored in Fee_Amount__c field and ti is a formula field which holds currence data type.

We need to display Fee_Amount__c value on Account level from most recently created Fee record.

Thank you in advance
v varaprasadv varaprasad

Hi Tad,

Try this:
 
triiger on updatefeeOnAccount Fee__c(after insert){
  set<id> accids = new set<id>();

   for(Fee__c fe : trigger.new){
     if(fe.account__c != null){
	   accids.add(fe.account__c);
	 }
   
   }

   map<id,account> mapAcc = new map<id,account>([select id,recentFee__c from account where id in : accids]);
   
   list<Fee__c> lstFees = [select id,account__c,Fee_Amount__c from Fee__c where account__c
    in : accids order by CreatedDate desc];
	
	for(Fee__c fes : lstFees){
	   if(mapAcc.containsKey(fes.account__c)){
	     mapAcc.get(fes.account__c).recentFee__c = fes.Fee_Amount__c;
	   }
	
	}
	
	update mapAcc.values();

}

Note: I have written in notepad may you will get some syntactical errors.

Hope this helps you!
If my answer helps resolve your query, please mark it as the 'Best Answer' & upvote it to benefit others.


Thanks
Varaprasad
@For SFDC Support: varaprasad4sfdc@gmail.com

Salesforce latest interview questions  :
https://www.youtube.com/channel/UCOcam_Hb4KjeBdYJlJWV_ZA?sub_confirmation=1

 
Tad MatjusenkoTad Matjusenko
Hi Varaprasad,

Thank you for your answer, I re-wrote code and ant line 24 it says missing " at 'update' and I can't understand what is wrong, hopefully that is the only reason and I will be able to test once this error will be gone.

Thank you
ManojjenaManojjena
HI Tad Matjusenko,
If Fee record is created under an account in a year and added the amount then we can write a trigger in fee object and update the amount to the related account's Fee_Amount__c field .
I think u can change the Fee_Amount__c field type to Currency instaed formula .

Try with below trigger ,this will work only if your scenario to create only one Fee record per year after create it will update in account .
 
trigger FreeTrigger on FeeObject(after insert){
	   Map<Id,Decimal> accMap= new Map<Id,Decimal>();
	   for(FeeObject fee : Trigger.new){
	      if(fee.AccountId__c != null ){
		     accMap.put(fee.AccountId__c,fee.Amount__c);
		  }
	   }
	   if(accMap.isEmpty()){
	      List<Account> accListToUpdate= new List<Account>();
	      for( Id accid : accMap.keySet()){
		    Account acc = new Account();
			acc.Id=accid;
			acc.Fee_Amount__c =accMap.get(accid);
			accListToUpdate.add(acc);
		  }
		  if(!accListToUpdate.isEmpty()){
		     try{
			    update accListToUpdate;
			 }catch(DMLException de ){
			     System.debug(de.getMessage());
			 }
		  }
	   }
	}

Let me know if it helps or you need any help !!!

Thanks 
Manoj 



 
v varaprasadv varaprasad
Please post here your code so that we can help you.
Tad MatjusenkoTad Matjusenko
Hi Varaprasad,
 
trigger UpdateFeeOnAccount on Signatory_Fee__c (after insert) {
	set<id> accids = new set<id>();
	
   	for(Signatory_Fee__c fe : trigger.new){
    	 if(fe.Organisation__c != null){
	   		accids.add(fe.Organisation__c);
	 }
   
   }

   map<id,account> mapAcc = new map<id,account>([select id,Current_Fee__c from account where id in : accids]);
   
   list<Signatory_Fee__c> lstFees = [select id,	Organisation__c,Fee_Amount__c from Signatory_Fee__c where Organisation__c
    in : accids order by CreatedDate desc];
	
	for(Signatory_Fee__c fes : lstFees){
	   if(mapAcc.containsKey(fes.Organisation__c)){
	     mapAcc.get(fes.Organisation__c).Current_Fee__c = fes.Fee_Amount__c;
	   }
	
	}
	
}
update mapAcc.values();

 
v varaprasadv varaprasad
close } After this : 

update mapAcc.values();
}


 
Raj VakatiRaj Vakati
Try this 
 
trigger UpdateFeeOnAccount on Signatory_Fee__c (after insert) {
	set<id> accids = new set<id>();
	
   	for(Signatory_Fee__c fe : trigger.new){
    	 if(fe.Organisation__c != null){
	   		accids.add(fe.Organisation__c);
	 }
   
   }

   map<id,account> mapAcc = new map<id,account>([select id,Current_Fee__c from account where id in : accids]);
   
   list<Signatory_Fee__c> lstFees = [select id,	Organisation__c,Fee_Amount__c from Signatory_Fee__c where Organisation__c
    in : accids order by CreatedDate desc];
	
	for(Signatory_Fee__c fes : lstFees){
	   if(mapAcc.containsKey(fes.Organisation__c)){
	     mapAcc.get(fes.Organisation__c).Current_Fee__c =  fes.Fee_Amount__c;
	   }
	
	}
	If(mapAcc.values().size()>0){
	update mapAcc.values();
	}
	
}

 
Tad MatjusenkoTad Matjusenko
Hi Raj,

Code does not fail, but it doesn't work, it populates Current_Fee__c field, but when I add another Signatory_Fee__c it won't update Current_Fee__c field on Account.

I will try to explain what code should do step by step, hopefully that will help:

1) When new Signatory_Fee__c record is created code must check if current record is most recent associated with current account, by CreatedDate
2) When it is identified that this Signatory_Fee__c record is most recent for this Account, then Current_Fee__c field on Account object must be populated with Fee_Amount__c field value from Signatory_Fee__c object

Thank you for all your help