+ Start a Discussion
Mayank Srivastava (Salesforce fan)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'.

User-added image

User-added image


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';
}
}
}

 
Best Answer chosen by Mayank Srivastava (Salesforce fan)
RamuRamu (Salesforce Developers) 
I suspect there is no value under Client service group. However, i suggest that you enable debug logs for yourself and do the testing by creating new case record. Before testing please copy the existing code into a notepad as a backup and replace it with the below code that consists of debug lines. You will be able to trace where it went wrong. 
 
trigger UpdateEntitlementsName on Case (before insert) {

set<id> accids=new set<id>();
For(case cs:trigger.new){
	accids.add(cs.Accountid);
}
system.debug('size of accids = ' +accids.size());
List<Account> accounts = [SELECT id,Client_Service_Group__c FROM Account WHERE Name = :accids];
system.debug('size of Accounts list accounts = ' +accounts.size());
map<id,string> accmap=new map<id,string>();

for(Account Acc:accounts){
    accmap.put(acc.id,acc.Client_Service_Group__c);
}


for (Case caseInLoop : Trigger.new) 
system.debug('entered case loop with trigger new collection');
    if(accmap.contains(caseInLoop.Accountid)){
system.debug('map contains the matching account');
system.debug('map Client_Service_Group__c value is : '+accmap.get(caseInLoop.Accountid););
        string group1=accmap.get(caseInLoop.Accountid);
            if(group1=='US -  Danbury' || group1=='US -  Houston'){
	system.debug('entered final if condition');
                caseInLoop.EntitlementID='5501900000003FxAAI';
	}
                
                    else{
		system.debug('entered final else condition');
                          caseInLoop.EntitlementID='5501900000003FsAAI';  
	}
         }   
     
    }

}

 

All Answers

RamuRamu (Salesforce Developers) 
Hi, Below are some of the things you would need to keep in mind to avoid errors/governor limits
- 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
 
trigger UpdateEntitlementsName on Case (before insert) {

set<id> accids=new set<id>();
For(case cs:trigger.new){
accids.add(cs.Accountid);
}
List<Account> accounts = [SELECT id,Client_Service_Group__c FROM Account WHERE Name = :accids];
map<id,string> accmap=new map<id,string>();

for(Account Acc:accounts){
	accmap.put(acc.id,acc.Client_Service_Group__c)
}

for (Case caseInLoop : Trigger.new) {
	if(accmap.contains(caseInLoop.Accountid)){
		string group=accmap.get(caseInLoop.Accountid);
			if(group=='US -  Danbury' || group=='US -  Houston'){
				caseInLoop.EntitlementID=<US CLIENT SUPPORT ID>;
			}
	}

}

 
RamuRamu (Salesforce Developers) 
Forgot to mention, you would need to change the 
<US CLIENT SUPPORT ID> with the id of that record.
Mayank Srivastava (Salesforce fan)Mayank Srivastava (Salesforce fan)
Hey Ramu, thanks for the repsonse!
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
 
trigger UpdateEntitlementsName on Case (before insert) {

set<id> accids=new set<id>();
For(case cs:trigger.new){
accids.add(cs.Accountid);
}
List<Account> accounts = [SELECT id,Client_Service_Group__c FROM Account WHERE Name = :accids];
map<id,string> accmap=new map<id,string>();

for(Account Acc:accounts){
    accmap.put(acc.id,acc.Client_Service_Group__c);
}

for (Case caseInLoop : Trigger.new) {
    if(accmap.contains(caseInLoop.Accountid)){
        string group1=accmap.get(caseInLoop.Accountid);
            if(group1=='US -  Danbury' || group1=='US -  Houston')
                caseInLoop.EntitlementID='5501900000003FxAAI';
                
                    else
                          caseInLoop.EntitlementID='5501900000003FsAAI';  
         }   
     
    }

}



 
RamuRamu (Salesforce Developers) 
Hi Mirage, Please change the word 'Contains' to 'ContainsKey' and keep everything else intact. The changed line should be as below


if(accmap.containskey(caseInLoop.Accountid)){
 
Mayank Srivastava (Salesforce fan)Mayank Srivastava (Salesforce fan)
Ramu, 
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. 
RamuRamu (Salesforce Developers) 
It is written for beforeinsert, if you are testing with update, please add beforeupdate as well. Other than this, I do not see any other reason why it will not fire.
Mayank Srivastava (Salesforce fan)Mayank Srivastava (Salesforce fan)
I created a new Case and expected the field to populate but it comes out blank. 
RamuRamu (Salesforce Developers) 
I suspect there is no value under Client service group. However, i suggest that you enable debug logs for yourself and do the testing by creating new case record. Before testing please copy the existing code into a notepad as a backup and replace it with the below code that consists of debug lines. You will be able to trace where it went wrong. 
 
trigger UpdateEntitlementsName on Case (before insert) {

set<id> accids=new set<id>();
For(case cs:trigger.new){
	accids.add(cs.Accountid);
}
system.debug('size of accids = ' +accids.size());
List<Account> accounts = [SELECT id,Client_Service_Group__c FROM Account WHERE Name = :accids];
system.debug('size of Accounts list accounts = ' +accounts.size());
map<id,string> accmap=new map<id,string>();

for(Account Acc:accounts){
    accmap.put(acc.id,acc.Client_Service_Group__c);
}


for (Case caseInLoop : Trigger.new) 
system.debug('entered case loop with trigger new collection');
    if(accmap.contains(caseInLoop.Accountid)){
system.debug('map contains the matching account');
system.debug('map Client_Service_Group__c value is : '+accmap.get(caseInLoop.Accountid););
        string group1=accmap.get(caseInLoop.Accountid);
            if(group1=='US -  Danbury' || group1=='US -  Houston'){
	system.debug('entered final if condition');
                caseInLoop.EntitlementID='5501900000003FxAAI';
	}
                
                    else{
		system.debug('entered final else condition');
                          caseInLoop.EntitlementID='5501900000003FsAAI';  
	}
         }   
     
    }

}

 
This was selected as the best answer
Mayank Srivastava (Salesforce fan)Mayank Srivastava (Salesforce fan)
This is from the debug log and it seems like the following line has an issue:
List<Account> accounts = [SELECT id,Client_Service_Group__c FROM Account WHERE Name = :accids];

User-added image
Mayank Srivastava (Salesforce fan)Mayank Srivastava (Salesforce fan)
Found the issue:
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];

 
Mayank Srivastava (Salesforce fan)Mayank Srivastava (Salesforce fan)
Ran into an issue while deploying. Any ideas on how I can resolve them? Here are the final code and test class code:
 
trigger UpdateEntitlementsName on Case (before insert) {

set<id> accids=new set<id>();
For(case cs:trigger.new){
    accids.add(cs.Accountid);
}
system.debug('size of accids = ' +accids.size());
List<Account> accounts = [SELECT id,Client_Service_Group__c FROM Account WHERE Id = :accids];
system.debug('size of Accounts list accounts = ' +accounts.size());
map<id,string> accmap=new map<id,string>();

for(Account Acc:accounts){
    accmap.put(Acc.id,acc.Client_Service_Group__c);
}


for (Case caseInLoop : Trigger.new) {
system.debug('entered case loop with trigger new collection');
    if(accmap.containskey(caseInLoop.Accountid)){
system.debug('map contains the matching account');
system.debug('map Client_Service_Group__c value is : '+accmap.get(caseInLoop.Accountid));
        string group1=accmap.get(caseInLoop.Accountid);
        system.debug('string group1 value is : ' +group1);
            if(group1=='US - Danbury' || group1=='US - Houston'){
    system.debug('entered final if condition');
                caseInLoop.EntitlementID='5501900000003FxAAI';
    }
                
                    else{
        system.debug('entered final else condition');
                          caseInLoop.EntitlementID='5501900000003FsAAI';  
    }
         }   
     
    }

}
 
Test Class

@isTest
public class TestUpdateOnEntitlements {
static testMethod void insertnewCase() {

Case caseToCreate = new Case();

caseToCreate.AccountId='00119000001vzPZAAY';
caseToCreate.ContactId ='00319000001Rb23AAC';
caseToCreate.Status ='Open';

insert caseToCreate;

}
}

User-added image