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
Suhaas Kapardhi B SSuhaas Kapardhi B S 

Trigger on case to update its related account name

Hi, I have created a trigger on case to update its related account name when I create a new case, but I am not sure about my code.Please help.



trigger modalAccount on Case (after insert) {
    
    Set<id> cas = new Set<id>();
    List <Case> ca = [SELECT AccountId,id From Case Where id in:trigger.new];
    for(Case c:ca ){
    cas.add(c.AccountId);
        }
    List<Account> acc = [Select Name From Account Where id in: cas];
    for(Account temp:acc){
        ca.AccountId=temp.Name;
    }

}



Thanks & Regards
Suhaas 
Sai PraveenSai Praveen (Salesforce Developers) 
Hi Suhaas,

Can I know what you want the account name to be updated to? I have written the code below to update the Account name to some text fied.
 
trigger modalAccount on Case (after insert) {
    
    Set<id> cas = new Set<id>();
    List<Account> acclist = new List<Account>();
   
    for(Case c:Trigger.new ){
    cas.add(c.AccountId);
        }
    List<Account> acc = [Select id, Name From Account Where id in: cas];
    for(Account temp:acc){
temp.Name='sample Account Name';
      acclist.add(temp);  
    }

    update acclist;
}
 

Let me know if you face any issues.

 

If this solution helps, Please mark it as best answer.

 

Thanks,

 

Thanks,
Suhaas Kapardhi B SSuhaas Kapardhi B S
When I create a new case inside account record I need the case to come under that account record.
 
Sai PraveenSai Praveen (Salesforce Developers) 
Hi Suhas,

Do you meant to add the Case number to Account Name. Can you clarify more with an example so I can provide the solution.

Thanks,
 
Suhaas Kapardhi B SSuhaas Kapardhi B S
I have created a custom modal which have only reason, description, priority and type in account object, so when i click on save case record is created on case object, now I need the case Account name to be same as the account record where I create the case.