softvowels.com

Update Case Owner based on Account Team Member Role

Table of Contents

Scenario: This scenario expects Case owner assignment depending on certain criteria. Whenever a case is created then it should check related Account Teams and according below criteria that account team member should be the owner of that Case.

  • If Case Origin is Phone then owner will be the account team member having Role “Sales Rep”
  • If Case Origin is Email then owner will be the account team member having Role “Channel Manager”
  • If Case Origin is Web then owner will be the account team member having Role “Account Manager”
/*
Scenario: This scenario expects Case owner assignment depending on certain criteria. Whenever a case is created then it should check related Account Teams and according below criteria that account team member should be the owner of that Case.
If Case Origin is Phone then owner will be the account team member having Role "Sales Rep"
If Case Origin is Email then owner will be the account team member having Role "Channel Manager"
If Case Origin is Web then owner will be the account team member having Role "Account Manager"
 */
trigger caseTrigger on Case (Before Insert) {
    Map<Id, Id> mapCaseIdAccId = new Map<Id, Id>();
    
    Map<Id, List<AccountTeamMember>> mapAccIdTeamRole = new Map<Id, List<AccountTeamMember>>();
    List<AccountTeamMember> lstATM = new List<AccountTeamMember>();
    if(Trigger.isBefore){
        if(Trigger.isInsert){
            for(Case cs:Trigger.New){
                mapCaseIdAccId.put(cs.Id,cs.AccountId);
            }
            
            for(AccountTeamMember atm:[SELECT Id,AccountId,TeamMemberRole, UserId 
                                        FROM AccountTeamMember 
                                        WHERE AccountId IN:mapCaseIdAccId.values()]){
                                            if(mapAccIdTeamRole.get(atm.AccountId)==null){
                                                lstATM = new List<AccountTeamMember>();
                                                lstATM.add(atm);
                                                mapAccIdTeamRole.put(atm.AccountId,lstATM);
                                            }else{
                                                mapAccIdTeamRole.get(atm.AccountId).add(atm);
                                            }
            }
            
            for(Case cs:Trigger.New){
                for(AccountTeamMember atm:mapAccIdTeamRole.get(cs.AccountId)){
                    if(cs.origin=='Phone' && atm.TeamMemberRole=='Sales Rep'){
                        cs.OwnerId = atm.UserId;
                    }
                    if(cs.origin=='Email' && atm.TeamMemberRole=='Channel Manager'){
                        cs.OwnerId = atm.UserId;
                    }
                    if(cs.origin=='Web' && atm.TeamMemberRole=='Account Manager'){
                        cs.OwnerId = atm.UserId;
                    }
                }
            }
        }
    }
}

Recent Post

Batch Apex is usually used to build complex, long-running processes that run on thousands of records(large data volume till 50 million records), at any specific time. Go through to get concrete understanding.
Salesforce has an annotation @future that we can use defining a method making its execution as asynchronous. For concrete understanding read further.
0 0 votes
Article Rating
Subscribe
Notify of
guest
0 Comments
Oldest
Newest Most Voted
Inline Feedbacks
View all comments
0
Would love your thoughts, please comment.x
()
x