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
[Lindsey Kiken][Lindsey Kiken] 

Apex: Inserting a Queue into a Case Team (After Insert)

I am attemping to insert a Queue into a Case Team (after insert) via apex. I have the logice to successfully insert a new Case Team Member, but continue to hit errors when attemting to swap the memberid for a Queue value.

My working member (user) code is as follows:
trigger CaseTeamMember_Owner_Insert on Case (after insert) {

    CaseTeamRole role = [select Name from CaseTeamRole where Name = 'Support Team' LIMIT 1];
    Map<Id, CaseTeamMember> membersToAdd = new Map<Id, CaseTeamMember>();
    List<Case> cases = [Select Id,OwnerId,ContactId,RecordTypeId,RecordType.Name
                  from Case where id IN :Trigger.newMap.keySet()];
    for (Case c : cases) {
    
      if (c.RecordType.Name == 'Support Case') {
           
        membersToAdd.put(c.Id, 
          new CaseTeamMember(                   
            TeamRoleId = role.Id,
            ParentId = c.Id,                        
            MemberId = c.OwnerId
          )
        );
        if (!membersToAdd.isEmpty()) {
          insert membersToAdd.values();
        }        
      }
    } 
}
My non-working member (queue) code is as follows:
trigger CaseTeamMember_Owner_Insert on Case (after insert) {

    CaseTeamRole role = [select Name from CaseTeamRole where Name = 'Support Team' LIMIT 1];
    Group g = [select Name from Group where Type = 'Queue' AND Name = 'Support Unassigned' LIMIT 1];
    Map<Id, CaseTeamMember> membersToAdd = new Map<Id, CaseTeamMember>();
    List<Case> cases = [Select Id,OwnerId,ContactId,RecordTypeId,RecordType.Name
                  from Case where id IN :Trigger.newMap.keySet()];
    for (Case c : cases) {
    
      if (c.RecordType.Name == 'Support Case') {
           
        membersToAdd.put(c.Id, 
          new CaseTeamMember(                   
            TeamRoleId = role.Id,
            ParentId = c.Id,                        
            MemberId = g.Id
          )
        );
        if (!membersToAdd.isEmpty()) {
          insert membersToAdd.values();
        }        
      }
    } 
}
The error that I am receiving when creating a new case with the member (queue) code in place:
 
Error: Invalid Data. 
Review all error messages below to correct your data.
Apex trigger CaseTeamMember_Owner_Insert caused an unexpected exception, contact your administrator: CaseTeamMember_Owner_Insert: execution of AfterInsert caused by: System.DmlException: Insert failed. First exception on row 0; first error: FIELD_INTEGRITY_EXCEPTION, Member ID: id value of incorrect type: 00G19000000ZNltEAG: [MemberId]: ()

Any thoughts?
 
Best Answer chosen by [Lindsey Kiken]
kevin lamkevin lam
Two options I can think of:
  1. Change the owner of the case to the queue.
  2. Loop through the queue members and add each one to the case team (please note each queue member can be either a user or group).

All Answers

kevin lamkevin lam
A case team member can either be a user or a contact, but not a queue, that is why you are getting the id value of incorrect type error.
[Lindsey Kiken][Lindsey Kiken]
Ouch- it looks like you are right! Is there any way to insert a predefined team in its wake?
kevin lamkevin lam
Why do you need to add a queue/team to the case team?
[Lindsey Kiken][Lindsey Kiken]
I need a predefined set of users to be automatically added to the Case Team based on certain ContactId related criteria (ex: queues based on different support regions).
kevin lamkevin lam
Two options I can think of:
  1. Change the owner of the case to the queue.
  2. Loop through the queue members and add each one to the case team (please note each queue member can be either a user or group).
This was selected as the best answer
[Lindsey Kiken][Lindsey Kiken]
Hi Kevin,

Both of those routes will work, albeit neither are truly ideal. Thank you for your help!

Cheers,
Lindsey