You need to sign in to do that
Don't have an account?
Mayank Srivastava (Salesforce fan)
Updating a lookup field on Case based on a custom field value on Account
Hello folks,
This is my first attempt at writing an Apex trigger and any help would be greatly appreciated. This is what I'm trying to accomplish:
Case object has a standard field called Entitlement Name that should be automatically populated depending upon the Client Service Group field value on the Account.
If Client_Service_Group__c == 'US-Danbury or US-Houston', EntitlementId should = 'ÚS Client Support'
else Entitlement Id = 'EMEA Client Support'.
This is what I wrote and it's not working:
This is my first attempt at writing an Apex trigger and any help would be greatly appreciated. This is what I'm trying to accomplish:
Case object has a standard field called Entitlement Name that should be automatically populated depending upon the Client Service Group field value on the Account.
If Client_Service_Group__c == 'US-Danbury or US-Houston', EntitlementId should = 'ÚS Client Support'
else Entitlement Id = 'EMEA Client Support'.
This is what I wrote and it's not working:
trigger UpdateEntitlementsName on Case (before insert) { for (Case caseInLoop : Trigger.new) { List<Account> accounts = [SELECT Client_Service_Group__c FROM Account WHERE Name = :caseInLoop.Account.Id]; For(Account currentAccount : accounts) { if (currentAccount.Client_Service_Group__c == 'US - Danbury'|| currentAccount.Client_Service_Group__c == 'US - Houston') caseInLoop.EntitlementId= 'US Client Support'; else caseInLoop.EntitlementId= 'EMEA Client Support'; } } }
All Answers
- Avoid using SOQL queries within for loop which might hit governor limit.
- Try using map, set variable where ever possible.
- Always use id for relationship fields like lookup/master detail
The below code might work
<US CLIENT SUPPORT ID> with the id of that record.
This is what I see now:
Error: Compile Error: Method does not exist or incorrect signature: [MAP<Id,String>].contains(Id) at line 15 column 8
if(accmap.containskey(caseInLoop.Accountid)){
I am able to save the code now but it isn't working for some reason. I don't know what's wrong with it.
List<Account> accounts = [SELECT id,Client_Service_Group__c FROM Account WHERE Name = :accids];
Name should be replaced with Id. Thanks for all the help! This is my first time writing a trigger hence the spoonfeeding. I will now see if I can write the test class.
List<Account> accounts = [SELECT id,Client_Service_Group__c FROM Account WHERE Id= :accids];