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
mikkamikka 

Sending Email Notification using Triggers using existing Email template but no workflow alerts...

Hi guys, I am currently into sending Email Notification to users who have either new cases or cases whose due date has been lapsed. So far, this is what I've gone: 1. Created a class that implements Schedulable (for Apex Scheduler) 2. Created a class / method that queries list of users who will be notified by the email. 3. Associate the class into Scheduled Jobs and this will run 8 AM daily. My problem is that how can I create a trigger that will fire up and send email notifications every time the scheduled job has finished running? Also the users want to use their existing email template for this. I've got problem using workflow alerts since it limits me to recipients who should also receive this email notification. For example: To: -> the case owner

Cc: -> case owner's supervisor and not the entire supervisor role

 

Is this possible? Your help will be highly appreciated. Thank you very much.

MJ09MJ09

What exactly do you want your scheduled job to do? You've described it as doing a query, but you haven't said what it does with the results.

mikkamikka

Hi Mj09,

 

It would basically send email notifications to agents whose cases where either Open or reopen. So far, it is now sending email notifications but when I check the debug log, it displays the following message: 

Number of Email Invocations: 8 out of 10 ******* CLOSE TO LIMIT

Does it mean that when I got for example 20 users needed to receive email notifications, an error will fire up because of this limitation? Thanks for your reply.

 

 

 

 


MJ09MJ09

You're getting close!

 

The limit you're running into is a limit on the number of times you can call sendEmail(), not the number of email messages you can send. It sounds like you're sending each SingleEmailMessage individually. Instead, build a list of messages, and add each of the messages to the list. When you're doing building the list, send all of the messages at once.

mikkamikka

Thank you very much Mj09,

The warning message in the debug log was now gone. It now displays one invocation only since messaging.sendemail method was called only once. The problem I am facing right now is that the emails that were sent returns more than what it is expected. I've got 8 users to send but the emails generated 24. It triples the records =( I am just wondering since I used request.size(); method in my loop to control the no of emails to be sent and checked the value it returns which is 8. I have no idea why it sends 3x.

 

Thank you very much for your help!!!!

MJ09MJ09

Glad to hear that callling sendMail() once helped!

 

You'd have to post the code for us to figure that one out. Make a copy of the code and pare it down to the smallest size possible, so we don't have to wade through a bunch of stuff that isn't relevant to the problem. In the process of preparing the code to post here, you may even discover the problem yourself!

mikkamikka

Thank you very much,Mj09.  I've inserted the method in which emails were generated. A schedulable class will call this method to query first the Cases and then trigger the sending of notifications.

 

  public void sendTheEmail(Case[]request)
    {
     List<Messaging.Singleemailmessage> messages = new List<Messaging.Singleemailmessage>();
      
        for (integer i = 0; i < request.size();i++){
        Messaging.Singleemailmessage theEmails = new Messaging.Singleemailmessage();
        
        String[] toAddresses = new String[] {'mjele@sunlife.com'}; 
        String[] ccAddresses = new String[] {'mjele@sunlife.com'};

        theEmails.setToAddresses(toAddresses);
        theEmails.setBccAddresses(ccAddresses);
        theEmails.setCcAddresses(ccAddresses);
        theEmails.setSenderDisplayName('Sales Force');
       
         
        theEmails.setSubject('CRM: SR Assignment Notification');
        
         
        theEmails.setHtmlBody(request[i].LastModifiedById + ' has assigned a new Service Request to you.<p> '+
        'Your case:<b> ' + request[i].CaseNumber +' </b>has been created<p>'+
     'To view case <a href=https://cs1.salesforce.com/'+ request[i].Id +'>Click Here</a>');
        messages.add(theEmails);
        }
        
        //add the elements separately
        List<Messaging.Email> allMails = new List<Messaging.Email>();
            for (Integer j = 0; j<messages.size(); j++) {
            	allMails.add(messages.get(j));
            } 

//send the email Messaging.Sendemailresult[] results = Messaging.sendEmail(allMails); }

 

 

 

 

MJ09MJ09

Mikka,

 

You're setting the To, CC, and BCC addresses all to the same address. Is that address getting three copies of the same message?

 

MJ.

mjele@sunlife.com'
Rasmus MenckeRasmus Mencke

If you try to send 3 emails to the same address, we will only send one email, since the content is the same. Two of the emails are ignored. 

mikkamikka

Hi guys,

 

Many many thanks for your responses. It works!!! I now received the exact no of mails I should be receiving. Now my real problem is that how can I associate my SR Owner field in the UserName of User object? I really needed it very badly since each of the SR owner's has their corresponding emails which can only be found in the User object. The user name and SR owner has the same values but i can't just compare it using the = sign.

Please find below the options that I've used:

 

[1]

 SR = [Select c.Status, c.SR_Number__c, c.Id, c.Due_Date__c, c.Created_Date__c,
              c.CaseNumber,Date_Compare__c,LastModifiedById,c.SR_Owner__c,

              u.Name, u.Email
              From Case c, User u
              Where (c.Status = :Constants.SERVICE_REQUEST_STATUS_OPEN
              or c.Status = :Constants.SERVICE_REQUEST_STATUS_REOPENED)
              and Date_Compare__c = 'true'

              and c.SR_Owner__c = Name
                ];

 

[2]

String usrName=UserInfo.getUserName();
Select c.Status, c.SR_Number__c, c.Id, c.Due_Date__c, c.Created_Date__c,
              c.CaseNumber,Date_Compare__c,LastModifiedById,c.SR_Owner__c
              From Case c where c.SR_Owner__c=:usrName;

 

But this gets only the current user. I needed to associate all the owners to their corresponding usernames.

I really appreciated your help. Again, many many thanks!!!!

 

Thanks and kind regards,

Mikka

MJ09MJ09

Username and User Id are two different things. The Username is a string containing, well, a user's username. User Id is a lookup or master/detail child relationship to a parent User record. You probably want to assign User Id, not the Username, to your custom field. I don't know what your SR_Owner__c field refers to, but it should probably be a User Id -- Owner fields usually do refer to User Ids. In your #2 code sample, you use:

 

 

String usrName=UserInfo.getUserName();

 

Perhaps calling getUserId() help.

 

mikkamikka

Hi Mj,

 

The SR_Owner__c is a string data type holding the following default value: $User.FirstName &" "& $User.LastName

That's the reason why I wanted to use the UserName field of User since they have same type and value.

 

Thanks

mikkamikka

 

Hi guys, I finally got it work. I associated the OwnerId of the Case to the UserId of User so that I can finally get their emails.

Thank you for all your replies. =)