You need to sign in to do that
Don't have an account?

Trigger is not working as expected.
Hi All,
I need to send email messeges to my contacts on closing the cases also need to create Task and Email Messages record. I have done some thing like this.
Also I need to do some other modifications. we have 5 customer email addreses where we receive cases, I wants to send different email templates according different customers email addresses like if we receive case on customer email address1, template1 should be sent and accordingly.
Please suggest how can i update the case records and achieve this functionality.
Thank You
I need to send email messeges to my contacts on closing the cases also need to create Task and Email Messages record. I have done some thing like this.
trigger sendemailonclosecase on Case (before update, after update ) { Set<Id> Caseidset = new Set<Id>(); List<Case> updCase = new List<Case>(); List<Case> Caselist = new List<Case>(); Set<id> conidset = new set<Id>(); List<Messaging.SingleEmailMessage> msgList= new List<Messaging.SingleEmailMessage>(); String [] emails = new List<String>(); Map<Id, String> Emailmap = new Map<Id, String>(); Map<Id, String> Accountmap = new Map<id, String>(); List<Task> Tasklist = new List<Task>(); List<Emailmessage> ListEmail = new List<EmailMessage>(); if(trigger.isAfter){ if(trigger.isUpdate){ System.Debug('>>>>>'); EmailTemplate et = [Select Id from Emailtemplate where Name = 'closurecaseemailtemplatece1']; for (case c : trigger.new){ if(c.status == 'Closed' && c.Survey_Sent__c == false){ conidset.add(c.contactid); } } List<Contact> contactList = [Select Id, Email, AccountId, Account.name from Contact where Id In: conidset]; for(Contact ct : contactList){ Emailmap.put(ct.Id, ct.Email); Accountmap.put(ct.AccountId, ct.Account.Name); } System.Debug('>>>' + Emailmap); System.Debug('>>>' + conidset); System.Debug('>>>' + Accountmap); for(Case cCase : Trigger.new){ System.Debug('>>> for cas'); if(cCase.Status == 'Closed'){ System.Debug('>>> if' + cCase.status); Messaging.SingleEmailMessage msg = new Messaging.SingleEmailMessage(); msg.setTemplateId(et.id); msg.setTargetObjectId(cCase.ContactId); msg.setTemplateId(et.id); msg.setWhatId(cCase.id); msg.setSaveAsActivity(false); if(!Emailmap.isEmpty()){ emails.add(Emailmap.get(cCase.contactId)); } System.Debug('Sting' + emails); if(Emails.size() > 0){ msg.setToAddresses(emails); } msglist.add(msg); // Updating Case hear------------- //cCase.Survey_Sent__c = true; Caseidset.add(cCase.Id); // Creating Task hear ------------ Tasklist.add(new Task(ownerId = cCase.OwnerId, Priority = 'Normal', Subject = 'Case ' + cCase.Casenumber + 'has been closed', whoId = cCase.ContactId, WhatId = cCase.Id, Account_Name__c = Accountmap.get(cCase.AccountId))); System.Debug('>>> ' + Tasklist); ListEmail.add(new Emailmessage(FromName = 'FXAll Client Service', ToAddress = Emailmap.get(cCase.ContactId), Subject = 'Case ' + cCase.Casenumber + 'has been closed', TextBody = 'Thank you for contacting AM3CS your Case ' + cCase.Casenumber + 'has been closed', ParentId = cCase.Id, ActivityId = Tasklist[0].Id)); System.Debug('>>> ' + ListEmail); } } Messaging.sendEmail(msgList); Insert Tasklist; Insert ListEmail; } } /*updCase = [Select Survey_Sent__c from case where Id IN : caseidset]; for(case c : updCase){ c.survey_sent__c = true; Caselist.add(c); } Update Caselist;*/ }Hear I am getting Activity id null and if my email has been sent I want to update my case with survey_sent__c = true. I am not able to update case records may be i am doing something wrong i have commented the code for this.
Also I need to do some other modifications. we have 5 customer email addreses where we receive cases, I wants to send different email templates according different customers email addresses like if we receive case on customer email address1, template1 should be sent and accordingly.
Please suggest how can i update the case records and achieve this functionality.
Thank You