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
Su KingSu King 

Trigger, update decimal field in Account when number field in Case is changed

Hi everyone, I am tring to write a trigger to update decimal field in Account when number field in Case is changed, any idea ?
Akash Pandey 19Akash Pandey 19
Hi Su King,
I think this code will give you the batter idea for your requirement.
Here I am updating the Account field(Auto_Number_Of_Case__c) from the case field(Auto_Number__c).
We can update this field when we create a new Case or updating existing Case.

trigger UpdateNoField on Case (after insert, after update) {
    List<Account> acList = new List<Account>();
    Set<Id> idSet = new Set<Id>();
    if(Trigger.isAfter){
        for(Case c: Trigger.New){
            if(c.AccountId != null){
                idSet.add(c.AccountId); 
            }
        }
        
        acList = [SELECT Id, Auto_Number_Of_Case__c FROM Account WHERE Id IN: idSet  LIMIT 1];
        for(Case c: Trigger.New){
            if(c.AccountId != null && acList.size() >0){
                acList[0].Auto_Number_Of_Case__c = c.Auto_Number__c;
            }
        }
    }
    update acList; 
}

Please mark this as Best Answer if this helps you.
Thanks & Regards.
Akash Pandey