0

I have a trigger that does a round robin assignment to queue that assigns cases. How would be able to send a custom email to the user once the case is assigned to them via apex? How can I query which user was assigned the last case in the queue? My code is listed below. Thank you.

trigger ConsultationCaseAssignment on Case(before insert, before update) {
  if (Trigger.isBefore && (Trigger.isInsert || Trigger.isUpdate)) {
    // Get the queue user details
    List<Group> queues = [
      SELECT Id, (SELECT Id, UserOrGroupId FROM GroupMembers ORDER BY ID ASC)
      FROM Group
      WHERE Type = 'Queue' AND DeveloperName = 'Consultation_Requests'
    ];

    // Get the index of the last case assigned user in the queue
    Case_Round_Robin_Assignment__c lrr = Case_Round_Robin_Assignment__c.getOrgDefaults();
    Integer userIndex = (lrr.get('User_Index__c') == null ||
      Integer.valueOf(lrr.get('User_Index__c')) < -1)
      ? -1
      : Integer.valueOf(lrr.get('User_Index__c'));

    if (queues.size() > 0 && queues.get(0).GroupMembers.size() > 0) {
      Id queueId = queues.get(0).Id;
      Integer groupMemberSize = queues.get(0).GroupMembers.size();
      for (Case c : Trigger.new) {
        if (c.OwnerId != queueId) {
          Integer caseUserIndex = (userIndex + 1) >= groupMemberSize
            ? 0
            : userIndex + 1;
          c.OwnerId = queues.get(0)
            .GroupMembers.get(caseUserIndex)
            .UserOrGroupId;
          System.Debug(c.OwnerId);
          c.RecordTypeId = '0128Z000000JUwLQAW';
          userIndex = caseUserIndex;
        }
      }

      // Update the custom settings user index with the last lead assigned user
      lrr.User_Index__c = userIndex;
      update lrr;
    }
  }
}

denzgrant
  • 11
  • 2
  • 3

0 Answers0