You need to sign in to do that
Don't have an account?
deepak1.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
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
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>
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
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
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
Regards
Deepak
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/