Send email to all members in queue


// Step 1: Collect all unique ownerIds from email messages
Set<Id> ownerIds = new Set<Id>();
for (EmailMessage em : emailList) {
    if (em.Parent != null && em.Parent.OwnerId != null) {
        ownerIds.add(em.Parent.OwnerId);
    }
}

// Step 2: Query groups to identify which owners are queues
Map<Id, Group> ownerIdToGroup = new Map<Id, Group>(
    [SELECT Id, Type FROM Group WHERE Id IN :ownerIds AND Type = 'Queue']
);

// Step 3: Query GroupMembers for all identified queues
Set<Id> queueIds = ownerIdToGroup.keySet();
Map<Id, List<String>> queueToEmails = new Map<Id, List<String>>();

if (!queueIds.isEmpty()) {
    List<GroupMember> members = [
        SELECT GroupId, UserOrGroupId, UserOrGroup.Email
        FROM GroupMember
        WHERE GroupId IN :queueIds
    ];

    for (GroupMember gm : members) {
        if (gm.UserOrGroup.Email != null) {
            if (!queueToEmails.containsKey(gm.GroupId)) {
                queueToEmails.put(gm.GroupId, new List<String>());
            }
            queueToEmails.get(gm.GroupId).add(gm.UserOrGroup.Email);
        }
    }
}

// Step 4: Build messages with ccAddresses
for (EmailMessage emsg : emailList) {
    Messaging.SingleEmailMessage message = Messaging.renderStoredEmailTemplate(
        etemp.Id, UserInfo.getUserId(), null
    );

    String emailTemplateBody = message.getHtmlBody();
    emailTemplateBody = emailTemplateBody
        .replace('{CaseNumber}', emsg.Parent.CaseNumber)
        .replace('{LeadName}', emsg.Parent.Ops_Lead__r.Name)
        .replace('{InternalStage}', emsg.Parent.TXBX_Case_Stage__c)
        .replace('{CaseOwner}', emsg.Parent.Owner.Name)
        .replace('{CaseCommentLink}', URL.getSalesforceBaseUrl().toExternalForm() + '/' + emsg.ParentId);

    message.setHtmlBody(emailTemplateBody);
    message.setOrgWideEmailAddressId(owa[0].Id);
    message.setSaveAsActivity(false);

    // To Address logic
    String toEmail = emsg.Parent.Ops_Lead__r.Email != null
        ? emsg.Parent.Ops_Lead__r.Email
        : emsg.Parent.Owner.Email;

    Id toId = emsg.Parent.Ops_Lead__r.Email != null
        ? emsg.Parent.Ops_Lead__r.Id
        : emsg.Parent.Owner.Id;

    message.setToAddresses(new List<String>{toEmail});
    message.setTargetObjectId(toId);

    // CC logic: If queue, get all member emails; if user, use owner email
    List<String> ccEmails = new List<String>();
    if (queueToEmails.containsKey(emsg.Parent.OwnerId)) {
        ccEmails.addAll(queueToEmails.get(emsg.Parent.OwnerId));
    } else if (emsg.Parent.Owner.Email != null) {
        ccEmails.add(emsg.Parent.Owner.Email);
    }

    message.setCcAddresses(ccEmails);
    messages.add(message);

    emsg.Notification_Sent__c = true;
    emailListUpdate.add(emsg);
}
Send email to all members in queue Send email to all members in queue Reviewed by dasfrogpractice on 10:25 Rating: 5

No comments:

Theme images by mariusFM77. Powered by Blogger.
Youtube Channel Image
Dasfrog Subscribe To watch more Salesforce Training
Subscribe