2

On Case object have two custom fields:

  1. Case Account Team (text)

  2. Case Account Team Role (text)

On User object have two fields

  1. Account Team (Picklist)

  2. Account Team Role (Picklist)

I need a after insert trigger to: select the user based on the two case fields matching the user fields and change the case owner to the selected user. I only want the first user that matches.

  • 1
    Welcome to SFSE, Michael. How far have you gotten with your SOQL and Trigger? Please post what you have so we can help guide you to a complete solution. – Mike Chale Apr 8 '13 at 15:11
  • Mke, Thanks for responding. I have never written a trigger. Have looked at numerous examples but have not put any code together yet. – Michael Milner Apr 8 '13 at 16:22
  • @MichaelMilner Welcome to the site, though its purpose is to solve specific questions and you'll get a much better response if you have a go at the trigger and then ask about any problems you encounter, as opposed to asking for people to write it for you. As a starting point you're going to want a trigger that works on the Case object, before insert and before update. – Matt Lacey Apr 9 '13 at 1:26
1

Given that you want to change the OwnerId on the Case Record being inserted (or updated), you're better off using a before trigger so that you don't have to explicitly update the Case records. This is a simplified approach where all matching users are selected and the OwnerId will be any one of the selected users. It also assumes that Account Team and Account Team Role don't have common picklist values.

Trigger CaseBefore on Case (before insert, before update){

Map<String, List<Case>> acctTeamAndRoleMap = new Map<String, List<Case>>();
Set<String> accTeamRoleSet = new Set<String>{};
Set<String> acctTeamSet = new Set<String>();

For (Case case : trigger.new){ //collect acct team and roles to query by
List<Case> currList = acctTeamAndRoleMap.get(case.Account_Team__c + case.Account_Team_Role__c);
If(currList == null){
currList = new List<Case>();
accountTeamAndRoleMap.put(case.Account_Team__c + case.Account_Team_Role__c, currList);
}
currList.add(case);

acctTeamSet.add(case.Account_Team__c);
acctTeamSet.add(case.Account_Team_Role__c);
}

For(User usr : [select Id, name, Account_Team__c, Account_Team_Role__c from
User where Account_Team__c IN :acctTeamSet AND Account_Team_Role__c IN :acctTeamRoleSet ])
For(Case cas : acctTeamMap.get(usr.Account_Team__c + usr.Account_Team_Role__c))
Cas.OwnerId = usr.Id;
}

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.