+ Start a Discussion
Dilip KulkarniDilip Kulkarni 

Apex trigger for case alert

Hi Experts,
I want to create a trigger for following case:

Whenever a case is opened and closed for an account of particular region ( e.x. WE- West), the respective inside rep ( it's a sales rep) for that account should get email alert notification. What will be the trigger for this scenario?
 
GauravGargGauravGarg
Hi Dilip,

You can write workflow rules or create process Builder for these scenario's. 

Going to apex any particular reason?

Thanks,
Gaurav
Skype: gaurav62990
Dilip KulkarniDilip Kulkarni
Hi Gaurav,
Workflow rule is there, but it sends email alert to every recipient. Here I want to send mail to only concerned rep of that account ( and not others).
In process builder email alerts are not there.

Thanks,
Dilip
GauravGargGauravGarg
Hi Dilip,

ohh in that case, we can have trigger 
Trigger caseTrigger on Case(after insert){
   // Generate handler class
   caseTriggerHandler.afterInsert(newList());
} 

public class void CaseTriggerHandler{

     public static void afterInsert(List<case> newList){
       //write logic to throw email out of salesforce. 
  }
}

You can now extend this as per your requirement. 

Thanks,
Gaurav
Skype: gaurav62990 
Harshit Garg 6Harshit Garg 6
Hi Dilip,

Please follow the link below.

Link - https://krishhari.wordpress.com/2013/09/03/dynamically-populating-custom-html-email-template-content-in-force-com-with-custom-dynamic-data-using-apex/

Thanks,
Harshit garg
Dilip KulkarniDilip Kulkarni
Hi Gaurav,
Thank you very much for your response.
As I am new to apex coding, I will much appreciate if you give complete trigger.
Please let me know if any information is required.

Thanks
GauravGargGauravGarg
Hi Dilip,

please give me the scenario's for sending out emails with field values and conditions. 

Thanks,
Gaurav
Skype: gaurav62990
Dilip KulkarniDilip Kulkarni
Hi Gaurav,
Here we have an account in region for example WE. For this account the field 'inside sales' is there.(which is sales executive). When case is opened and closed for this account 'inside sales' (of this account) should receive email notification. The mail should go to only that particular 'inside sales'. (not the others)

If anything required,please let me know.
GauravGargGauravGarg
Hi Dilip,

Please find sample trigger class, do modify as per your actual needs:
 
Trigger caseTrigger on Case(after insert){
   // Generate handler class
   caseTriggerHandler.afterInsert(trigger.new);
} 

public class void CaseTriggerHandler{
	
    public static void afterInsert(List<case> newList){
    	list<id> conId = new list<Id>();
    	for(case c: newList){
    		if(c.contactid != null)
    			conId.add(c.contactId);
    	}

    	//query email address from account object.
    	Map<id,String> insideSalesEmailMap = [select id, account.inside_sales__c from contact where id IN :conId];
    	
    	// preparing for Email send out. 
    	List<SingleEmailMessage> sme = new List<SingleEmailMessage>();
    	for(case c: newList){
	    	Messaging.SingleEmailMessage email = new Messaging.SingleEmailMessage();
	    	email.setTemplateId('put template id');
	    	email.setToAddresses(insideSalesEmailMap.get(c.contactId));
	    	email.targetObjectId(c.id);
	    	email.setSaveAsActivity(false);
	    	sme.add(email);
	    }

	    //send Email
	    if(sme != null && sme.size() > 0)
	    	Messaging.sendEmail(sme);
  }
}

Thanks,
Gaurav
Skype: gaurav62990
Dilip KulkarniDilip Kulkarni
Hi Gaurav,
Thanks for your kind help. Will it work for both open and closed cases, or it's different trigger when case is closed?
As mentioned in SOQL query object 'contact' is mentioned, while we are mentioning query email address from account object.Is it right?
Could you please clarify what is variable 'c.contactId' for?
GauravGargGauravGarg
Hi Dilip,

Is your case Object directly linked with Account object?

In normal scenario, this goes like below:

Account is parent to Contact.
Contact is parent to Case. 

Hence to populate account data, we need to go with Contact --> Account. 

Thanks,
Gaurav
Dilip KulkarniDilip Kulkarni
Hi Gaurav,
Yes, case is having lookup relationship with account name and it's in related list of account.
GauravGargGauravGarg
Then we can directly make query on Account like below:
 
Trigger caseTrigger on Case(after insert){
   // Generate handler class
   caseTriggerHandler.afterInsert(trigger.new);
} 

public class void CaseTriggerHandler{
	
    public static void afterInsert(List<case> newList){
    	list<id> accId = new list<Id>();
    	for(case c: newList){
    		if(c.account__c != null)
    			accId.add(c.account__c );
    	}

    	//query email address from account object.
    	Map<id,String> insideSalesEmailMap = [select id, inside_sales__c from account where id IN :accId];
    	
    	// preparing for Email send out. 
    	List<SingleEmailMessage> sme = new List<SingleEmailMessage>();
    	for(case c: newList){
	    	Messaging.SingleEmailMessage email = new Messaging.SingleEmailMessage();
	    	email.setTemplateId('put template id');
	    	email.setToAddresses(insideSalesEmailMap.get(c.account__c));
	    	email.targetObjectId(c.id);
	    	email.setSaveAsActivity(false);
	    	sme.add(email);
	    }

	    //send Email
	    if(sme != null && sme.size() > 0)
	    	Messaging.sendEmail(sme);
  }
}

Hope this helps....

For more you can contact me on skype: gaurav62990

Thanks,
Gaurav
Dilip KulkarniDilip Kulkarni
Hi Gaurav,
Thank you. I will run the trigger and let you know likewise.
Dilip KulkarniDilip Kulkarni
Hi Gaurav,
I am having error in class 'Invalid type: SingleEmailMessage'