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
deepak1.3956330618074822E12deepak1.3956330618074822E12 

Apex trigger to update Account name to a default value in a case

Hi All

I am looking for a sample code that updates the Account name in a case to a default value if certain other field values are selected.
For example, if  i select the Type = ABC, then Account Name should default to ABC when the case is saved.

Any help is appreciated

Regards
Deepak
Vinit_KumarVinit_Kumar
Hi ,

Try below code :-

<pre>

trigger updateAccountName on Case(before insert,before update)
{

List <Id> accIds = new List<Ids>();
List<Account> newaccList = new List<Account>();
//populating AccountIds in the List
for(Case c: trigger.new)
{
if(c.type=='ABC')
{
       accIds.add(c.AccountId);
}

}
List<Account> accList = new List<Account>();
accList = [select id,name from Account where id in:accIds];

for(Account a : accList)

{
a.name='ABC';
newaccList.add(a);
}

if(newaccList.size()>0)
{
   update newaccList;
}


}

</pre>
MagulanDuraipandianMagulanDuraipandian
Hi,
An Account can have multiple cases.

So, if a case is created under an Account with type 'ABC', the name of the Account will change to 'ABC'. Again if you create a case under the same account with type 'XYZ', the name of the Account will change to 'XYZ'.

Consider this scenario and if you require the logic, kindly lemme know.

If this solves your problem, kindly mark it as the best answer.
Hit Like, if it saved your work :-)

Regards,
Magulan
http://www.infallibletechie.com
deepak1.3956330618074822E12deepak1.3956330618074822E12
Hi Vinit

Thank you for providing the code. But this doesnt work. Probably i should have been more specific while giving the example. The problem I am facing is that for Type=ABC and Account ABC are different. Which means, for Type=ABC, the account name is something like "Cisco Systems"
So when i select Type=ABC, the account name has to get default to Cisco Systems. Sorry that i was not clear in my question.

Regards
Deepak
deepak1.3956330618074822E12deepak1.3956330618074822E12
Hi Magulan,

Thanks for the reply. I have clarified my question. The way a case gets created is that a person click on New under cases tab and the case page doesnt have the Account Name filled already. If I go to Account and then create a new case from there, the account name gets auto filled but that is not the case.

Regards
Deepak
deepak1.3956330618074822E12deepak1.3956330618074822E12
Also, Vinit, the above code over writes the existing account name instead of just filling the Account Name field in the case.

Regards
Deepak
Vinit_KumarVinit_Kumar
If you don't want the Trigger to fire on update remove the update event from Trigger.

Also,you need to replcae ABC with cisco systems in my code some thing like below :-


a.name='Cisco Systems';

Everything else remains same.I would suggest you to go through the Apex guide which provides you the documentation as how to create a Trigger.Link to the doc :-

http://www.salesforce.com/us/developer/docs/apexcode/