+ Start a Discussion
ram_devram_dev 

Need to send email to multiple users (case owner, case creator)whenever case is closed

HI,

 

i need to send email to case owner and case creator whenever case is closed. i wrote after update trigger for this. but its not working. can anyone help me out from this.

 

 

Thanks & Regards

Ram

Best Answer chosen by Admin (Salesforce Developers) 
AroraAnupAroraAnup

Why do you need to write a trigger for this requirement? You could have written a workflow rule that gets triggered as soon as the Case is closed and you can select the owner and creator as the email recepients.

Let me know if that works!

All Answers

ram_devram_dev

Hi,

 

here is the trigger i wrote. it is saved but didn't send emails.

 

trigger csetrigger on Case(after update){  

   Map<Id,Case> lcase = trigger.newmap;  

 Map<Id,Case> loldcase = trigger.oldmap;            

    list<id> uIds = new list<id>();    

List<user> userList=new List<user>();    

map<id,string> Emails=new map<id,string>();   

  //List<string> Emails=new List<string>();     

List<Case> cs=new list<case>();                     

  for(user a : userList)         

    {            

    for(case c :cs){     

    if(a.id== c.createdbyId || a.id== c.ownerId )        

  {            

  uIds.add(a.id);                

Emails.put(a.id,a.email);        

 

system.debug('Tesing:'+Emails);                 

      }   

    }             

         for(Case ct : lcase.values())        

         {          

     if (ct.status == 'closed')         

      {              

              Messaging.SingleEmailMessage mail = new Messaging.SingleEmailMessage();       

             mail.setBccSender(false);          

          mail.setUseSignature(false);              

      mail.setSaveAsActivity(false);            

   //string strEmail = Emails.get(a.id);           

    string [] toaddress= New string[]{a.email};   

                                                                                                                                                                                                mail.setSubject('closed cases');   

        mail.setHtmlBody('Dear user,<br><br>Confidential);  

                  mail.setToAddresses(toaddress);     

              Messaging.sendEmail(new Messaging.SingleEmailMessage[] { mail });          

     }          

     }           

    }             

  }

 

i didn't understand where i did mistake. can anyone help me out from this

 

Thanks & Regards

Rama

AroraAnupAroraAnup

Why do you need to write a trigger for this requirement? You could have written a workflow rule that gets triggered as soon as the Case is closed and you can select the owner and creator as the email recepients.

Let me know if that works!

This was selected as the best answer
AsiereikiAsiereiki

CloudKnight wrote:

Why do you need to write a trigger for this requirement? You could have written a workflow rule that gets triggered as soon as the Case is closed and you can select the owner and creator as the email recepients.

Let me know if that works!


Im agree with CloudKnight. You don't need any code!!! you can link actions (field update, email,...) to the event of closed case.

ram_devram_dev

Hi cloud knight and Asierieki,

 

Thankyou for your reply. workflow will work for only one recepint type. for multiple recepints how can we? can you tell me the briefly.

 

 

Thanks & Regards

Rama

AsiereikiAsiereiki

Hi, you can add up to 10 different email actions

AroraAnupAroraAnup

So when you create a new Email Alert, select CASE as the object. Then in the Recipient Type, select CREATOR under Search and select RECORD CREATOR. Then, you can again choose OWNER from the Search and add it also to the Selected Recipients. This way you should be able to select both!

 

Hope this helps! Do click KUDOS is this helps you.

 

Let me know how it goes

ram_devram_dev

Hi,

 

Thankyou for you reply. Here i've one more doubt that i 've custom field in case object it is lookup field to user. How come we select that field in recepint. I didn't found that field in recepints.

 

 

Thanks & Regards

Ram

AroraAnupAroraAnup

I think if you select the option of User in the Search criteria, you should be able to see this custom field which is the lookup to user.

 

Let me know if this helps!

ram_devram_dev

Hi ,

 

Thank you for your quick replys. I found the field under Related user.  But recepints didn't get any emails when case is closed.

where i did mistake. After workflow rule(case statue = closed) i added emailalert in workflow action.

 

Thanks & Regards

Ram

AroraAnupAroraAnup

I think your workflow criteria is a problem. Instead of saying Case Status = CLOSED, you should use a formula in the criteria saying:

 

 IsClosed = TRUE

 

This way, when the case gets closed by clicking the CLOSE CASE button, your workflow will then get triggered.

 

Hope this helps! Let me know if this works!

AsiereikiAsiereiki

No no!

 

Don't use a workflow.

 

follow this steps:

 

1.-Go to the approval process

2.-Look for the "Final Reject Actions"

3.-Add the action of sending an email.

AroraAnupAroraAnup

Approval Process?? Why do we need an approval process in this case?

ram_devram_dev

Hi cloudknight and asiereike,

 

Thank you for you reply. you both are amazing. it works with Workflow. if i give personal email id's it is working fine but for my organization id's its not working. i dont know what is the problem. i thought that the problem with my org mail id's.

 

 

Thanks & Regards

Ram

ram_devram_dev

Hi,

 

Have one more issue that, when ever case is closed it sent mails successfully but any other case fields got updated that time also we got mail. What do? Any suggestions?

AsiereikiAsiereiki

What is the formula in your workflow? you need to make a formula that only returns true when the case changes from open to close.

ram_devram_dev

Hi,

 

I took EC as Evaluate the rule when a record is created, and every time it’s edited and RC as Case: StatusequalsClosed.

 

I didn't took anything else. Where i need to give this formula? can you suggest me?

ram_devram_dev

can you briefly tell me what formula i need to give in that formula bar?

ram_devram_dev

Hi,

 

Thankyou. I used formula. it worked . ..:)