function readOnly(count){ }
Starting November 20, the site will be set to read-only. On December 4, 2023,
forum discussions will move to the Trailblazer Community.
+ Start a Discussion
Daniel BlevinsDaniel Blevins 

Adding contacts as case team members

I am using the following APEX trigger to add Contacts automatically to cases that want to be (theres a check box for it) but it is not adding case team members to case. What am I missing here?

trigger CaseTeamAddition on Case (after insert) {
 
if (Trigger.isAfter){
                If (Trigger.isInsert){
                                CaseTeamRole caseTeamRole = [SELECT Id 
FROM CaseTeamRole 
WHERE Name = 'Case Update Team'
LIMIT 1];
 
                                List<Id> accountIdList = new List<Id>();
                                
                                For (Case currentCase : Trigger.new){
                                                accountIdList.add(currentCase.AccountId);
                                }
                                
                                List<Contact> contactList = [SELECT Id, AccountId
                                                                                    FROM Contact
                                                                                    WHERE AccountId IN : accountIdList
                                                                                   AND Get_Email_Updates__c = true];
                                
                                List<CaseTeamMember> caseMembersToInsert = new List<CaseTeamMember>();
                                For (Case currentCase : Trigger.new){
                                               
                                                For (Contact currentContact : contactList){
                                                                If (currentContact.AccountId == currentCase.AccountId){
                                                                               
                                                                                CaseTeamMember newMember = new CaseTeamMember(ParentId = currentCase.Id,
                                                                                                                                                                                                MemberId = currentContact.Id,
                                                                                                                                                                                                TeamRoleId = caseTeamRole.Id);
                                                                                caseMembersToInsert.add(newMember);
                                                                }
                                                }
                                }
 
                                if (caseMembersToInsert.size() > 0){
                                                insert caseMembersToInsert;
                                }
                                                               
                                }
}                               
    }

Best Answer chosen by Daniel Blevins
Daniel BlevinsDaniel Blevins

Ended up using the following to add profile into the code in this situation which enabled us to get rid of the errors and have it find people correct

       User dbUser = [SELECT Id 
                                                    FROM User
                                                    WHERE Alias = 'dblevins'
                                                    LIMIT 1];
                          
                          List<Case> caseList = [SELECT Id, OwnerId
                                                      FROM Case
                                                      WHERE Id IN :caseIdList
                                                      //AND Type = 'Web'];
                                                      AND Owner.Profile.Name = 'Customer Community User'];
                          
                                for (Case curCase : caseList){
                                    curCase.OwnerId = dbUser.Id;
                                }
                          

All Answers

Debanjan SharmaDebanjan Sharma
Hi Daniel,

The code looks fine to me. There might be field level security issue with the new field Get_Email_Updates__c. Please check this field's permission once. Have you recieved any error in the debug log?

Regards,
Debanjan
Daniel BlevinsDaniel Blevins

Ended up using the following to add profile into the code in this situation which enabled us to get rid of the errors and have it find people correct

       User dbUser = [SELECT Id 
                                                    FROM User
                                                    WHERE Alias = 'dblevins'
                                                    LIMIT 1];
                          
                          List<Case> caseList = [SELECT Id, OwnerId
                                                      FROM Case
                                                      WHERE Id IN :caseIdList
                                                      //AND Type = 'Web'];
                                                      AND Owner.Profile.Name = 'Customer Community User'];
                          
                                for (Case curCase : caseList){
                                    curCase.OwnerId = dbUser.Id;
                                }
                          

This was selected as the best answer