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
THARUN SRIRAM 10THARUN SRIRAM 10 

I have a formula field in case object "Remaining_amount__C", I want all the values in this field in the case to be summed up and appear on its parent account record field "Pending_Amount__c". Can anyone please help me? Thanks in advance :)

I have a formula field in case object "Remaining_amount__C", I want all the values in this field in the case to be summed up and appear on its parent account record field "Pending_Amount__c". Can anyone please help me? Thanks in advance :)
manasa udupimanasa udupi
Looks like there is no straighforward way to achieve this via configuration : refer - https://success.salesforce.com/answers?id=9063A0000019Q7yQAE
however there is an idea https://success.salesforce.com/ideaView?id=0873000000079UUAAY , looks like Salesforce is evaluating it.

If you would like to go with apex, below is a simple trigger that i wrote and tested which could accomplish the ask:) 


trigger UpdateFieldValues on Case (before update, before insert) {
    Decimal remainingAmountcase;
    Decimal remainingAmountcaseOld;
    Set<Id>pendingAmountAccount=new Set<Id>();
    List<Account> acco=new List<Account>();
    if(Trigger.isInsert)
    {
        for (Case c : Trigger.new) {
            if (c.Remaining_amount__c!=null)
                pendingAmountAccount.add(c.AccountId);
                remainingAmountcase = c.Remaining_amount__c;
                                    }
           for(Account acc : [Select Id,Pending_Amount__c From Account Where Id IN :pendingAmountAccount])
               {
                   acco.add(acc);
                   acc.Pending_Amount__c=acc.Pending_Amount__c+remainingAmountcase;
               }
    }
    
    else if (Trigger.isUpdate)
    {
    for (Case c : Trigger.old) {
            if (c.Remaining_amount__c!=null)
                remainingAmountcaseOld = c.Remaining_amount__c;
    }
    for (Case c : Trigger.new) {
            if (c.Remaining_amount__c!=null)
                pendingAmountAccount.add(c.AccountId);
                remainingAmountcase = c.Remaining_amount__c;
                                    }
           for(Account acc : [Select Id,Pending_Amount__c From Account Where Id IN :pendingAmountAccount])
               {
                   acco.add(acc);
                   acc.Pending_Amount__c=acc.Pending_Amount__c-remainingAmountcaseOld+remainingAmountcase;
               }
    
    }
   update acco;
}
Bhargavi TunuguntlaBhargavi Tunuguntla
You can either try it with process builder and flow or apex trigger .Please check the apex trigger:

trigger:
trigger UpdateCaseAmountOnAccount on Case (before insert,before update,before delete,after insert,after update,after delete) {
    if(trigger.isAfter)
    {
        if(trigger.isInsert)
        {
            UpdateCaseAmountOnAccountClass.UpdateCaseAmountOnAccountMethod(trigger.newMap,null);
        }
        if(trigger.isUpdate)
        {
            UpdateCaseAmountOnAccountClass.UpdateCaseAmountOnAccountMethod(trigger.newMap,Trigger.oldMap);
        }
        
    }
    if(trigger.isBefore)
    {
        if(trigger.isDelete)
        {
            UpdateCaseAmountOnAccountClass.UpdateCaseAmountOnAccountMethod(null,trigger.oldMap);
        }
    }

}

Class:
 
public class UpdateCaseAmountOnAccountClass {
    public static void UpdateCaseAmountOnAccountMethod(Map<Id,Case> newCaseMap,Map<Id,Case> OldCaseMap)
    {
        Map<Id,Decimal> accAmount=new Map<Id,Decimal>();
        List<Account> accList=new List<Account>();
        Set<Id> accId=new Set<Id>();
        if(newCaseMap!=null)
        {
            for(Case c:newCaseMap.values())
                accId.add(c.AccountId);
        }
        else
        {
            for(Case c:OldCaseMap.values())
                accId.add(c.AccountId);
        }
        accList=[select id,Pending_Amount__c from Account where id in: accId];
        for(Account a:accList)
        {
            accAmount.put(a.id,a.Pending_Amount__c);
        }
        system.debug(accAmount);
        //insertion and updation
        if(newCaseMap!=null)
        {
            for(Case c:newCaseMap.values())
                
            {
                if(accAmount.containsKey(c.AccountId)) 
                {
                    if(accAmount.get(c.AccountId)!=null)
                    {
                    accAmount.put(c.AccountId,accAmount.get(c.AccountId)+c.Remaining_amount__c);
                    if(oldCaseMap.containsKey(c.id))
                    {
                        system.debug('in if');
                        accAmount.put(c.AccountId,accAmount.get(c.AccountId)-oldCaseMap.get(c.id).Remaining_amount__c);
                    }
                    }
                    else
                    {
                          accAmount.put(c.AccountId,c.Remaining_amount__c);
                    }
                    
                }
                
            }
            system.debug(accAmount);
        }
        //deletion
        else if(newCaseMap==null)
        {
            for(Case c:OldCaseMap.values())
                
            {
                if(accAmount.containsKey(c.AccountId)) 
                {
                    accAmount.put(c.AccountId,accAmount.get(c.AccountId)-c.Remaining_amount__c);
                }
                
            }  
        }
        for(Account a:accList)
        {
            if(accAmount.containsKey(a.id))
            {
                a.Pending_Amount__c=accAmount.get(a.id);
            }
        }
        update accList;
       
    }
    
}

Hope this helps.

Thanks.​​​​​​​