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
AmbigaRamAmbigaRam 

Trigger for email notification when the case is created to the case team,owner and contact email id

Hi,

 

Can anyone help me to write the apex trigger for snding email notifcation to the customer when the new case is created using email template and email notification for assigning the case to the case team and case owner(queues)?

 

 

Please help me.

 

 

Thanks.,

Ambiga

harry.freeharry.free
In this case, I think a workflow would be better.
AmbigaRamAmbigaRam

  Hi,

 

Yes I know the workflow rules would be better. but I am in need of Apex trigger only.

 

Please help me

 

 

Regards,.

Ambiga

Abhi_TripathiAbhi_Tripathi

Here is the trigger and after that its helper class.

 

trigger SendEmailToAccount on Contact (after insert, after update) {

    if(Trigger.isAfter){
        if(Trigger.isInsert ){ 
            
            //helper class for single email but bulk messages
            HelperContactTrigger.sendEmail(trigger.new);
        }
    }
}

 

/**
* Description : Trigger to send email to the contact if accountId is null .
*
* Created By : Abhi Tripathi
*
* Created Date : 07/16/2013
*
* Revision Logs : V1.0
*
**/
public with sharing class HelperContactTrigger {

//static method
public static List<Contact> sendEmail(List<Contact> contacts) {

//query on template object
EmailTemplate et=[Select id from EmailTemplate where name=:'Sales: New Customer Email'];

//list of emails
List<Messaging.SingleEmailMessage> emails = new List<Messaging.SingleEmailMessage>();

//loop
for(Contact con : contacts){

//check for Account
if(con.AccountId == null && con.Email != null){

//initiallize messaging method
Messaging.SingleEmailMessage singleMail = new Messaging.SingleEmailMessage();

//set object Id
singleMail.setTargetObjectId(con.Id);

//set template Id
singleMail.setTemplateId(et.Id);

//flag to false to stop inserting activity history
singleMail.setSaveAsActivity(false);

//add mail
emails.add(singleMail);
}
}

//send mail
Messaging.sendEmail(emails);

return contacts;
}
}

KrishHariKrishHari

If your requirements are to include only standard/custom fields from the case object, you can use the HTML email template (with letterhead) and use it in your send email method that will be called from your trigger. If you need to include dynamically computed data such as 'number of days since the case is pending in New status' then you may want to check my blog post titled 'Dynamically populating custom HTML email template content in force.com with custom dynamic data using Apex'. Here is a code snippet to send email from your trigger. This code snippet can send emails to all the case queue members. The good thing about this code is that it uses the salesforce user id in the 'to' field which means that it is exempted from the daily limits on how many emails can be sent.

 

trigger CaseSendEmailNotification on Case(after insert) {
    List<EmailMessageWrapper> listEmailMessageWrapper = new List<EmailMessageWrapper>();
    List<Id> listUserIds = UtilityClass.getQueueMembers('Case', '<Your case queue name');
    for(Case c : trigger.new) {
        for(Id userId : listUserIds)
            listEmailMessageWrapper.add(new EmailMessageWrapper('<from address', userId, null, null, null); 
    }
    if(listEmailMessageWrapper.size() > 0)
        UtilityClass.sendEmail(listEmailMessageWrapper, '<HTML email template name>');
}

 You can find the link to the source code for the classes that I referred above in my blog post.

 

Regards,

HK.