2

I need to send email to queue members of queue on update case record based on some conditions. There are multiple queues and on case there is one field based on that I need to send email... How can I achieve this using trigger .... Please suggest

4
  • 1
    can you share what you have done so far? Commented Jun 28, 2016 at 16:46
  • There is one lookup field(Euipment__c) on case .Whenever i change the X2nd_Level_Support_Needed__c field on Case to 'Yes' then it will take the euipment lookup Object field (Fleet Owner_c) and it will look to the same custom setting with field value Fleet_owner_c .if its find then again there is one field on custom setting which is queue name , I need to send emai to that queue members/Queue Email . Commented Jun 28, 2016 at 17:08
  • so what is not working here. Commented Jun 28, 2016 at 17:10
  • i am not getting how to get queue members and send mail Commented Jun 28, 2016 at 17:13

2 Answers 2

2

There are a couple approaches here:

  • Send one SingleEmailMessage to each User. This is a great option if you can use it, because it does not consume your daily email limit.
  • Collect the Email for each User and then call setToAddresses with that collection.

Either way, you can use this SOQL:

SELECT Email FROM User WHERE Id IN (
    SELECT UserOrGroupId FROM GroupMember WHERE Group.DeveloperName = 'My_Group'
)

I also recommend implementing the Lazy Load pattern here (in addition to using a trigger handler, which you should research on your own to decide what's best for you).

public with sharing class MyClass
{
    public List<User> queueMembers
    {
        get
        {
            if (queueMembers == null)
                queueMembers = [/*query*/];
            return queueMembers;
        }
        private set;
    }

    // OR

    public List<String> queueEmails
    {
        get
        {
            if (queueEmails == null)
            {
                Set<String> uniqueEmails = new Set<String>();
                for (User user : [/*query*/]) uniqueEmails.add(user.Email);
                queueEmails = new List<String>(uniqueEmails);
            }
            return queueEmails;
        }
        private set;
    }
}
2

so in that case you can do something like

SELECT Email FROM User WHERE Id IN (
    SELECT UserOrGroupId FROM GroupMember WHERE Group.DeveloperName IN: mapQNameWithIds.keyset();
) 

Note Thanks @Adrian Larson for the query

and fill Map<string, List<string>> emailMap = new Map<string, List<string>> emailMap() ANd then continue in your loop

List<Messaging.SingleEmailMessage> emailList = new List<Messaging.SingleEmailMessage>();
for(Default_Case_Queue__c casedefaultSetting : Default_Case_Queue__c.getAll().values())  {
     if(casedefaultSetting.Fleet_PLC__c.equals(equip.Fleet_Owner_PLC__c) && mapQNameWithIds.containsKey(casedefaultSetting.queueName) ){
           Messaging.SingleEmailMessage mail = new Messaging.SingleEmailMessage();
           List<String> sendTo = new List<String>();
           sendTo.addAll(emailMap.get(casedefaultSetting.queueName));
           // Assign the addresses for the To and CC lists to the                      mail object.
           mail.setToAddresses(sendTo);


            // Specify the subject line for your email address.
            mail.setSubject('New Case Updated : ' + case.Id);

            // Set to True if you want to BCC yourself on the email.
            mail.setBccSender(false);


          // Specify the text content of the email.
          mail.setPlainTextBody('Your Case: ' + case.Id +' has been updated.');

           mail.setHtmlBody('Your case:<b> ' + case.Id +' </b>has been updated.<p>'+
     'To view your case <a      href=https://***yourInstance***.salesforce.com/'+case.Id+'>click here.</a>');
                   emailList .add(mail );

                    }
                } 
// Send the email you have created.
Messaging.sendEmail(emailList ); //keep this line out of for loop
5
  • How to fill this map {Map<string, List<string>> emailMap = new Map<string, List<string>>(); for(User u:[SELECT Email FROM User WHERE Id IN (SELECT UserOrGroupId FROM GroupMember WHERE Group.DeveloperName IN: mapQNameWithIds.keyset()) ]) { emailMap.put(u.email, u); }} Commented Jun 28, 2016 at 18:31
  • 1
    wht i need to put in map ? Commented Jun 28, 2016 at 18:31
  • In this map one side keep queue name and another side list of id Commented Jun 28, 2016 at 19:42
  • 2
    Did you just copy that soql straight from my answer? :P
    – Adrian Larson
    Commented Jun 28, 2016 at 21:05
  • 1
    @AdrianLarson yes with few changes :). Hope you will pardon me. Commented Jun 29, 2016 at 4:43

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .