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
Akshay MhetreAkshay Mhetre 

Send Email to Case allocate by__c user

There are 3 Users.. User1,User2,User3.
All 4 Users have same profile.

Scenario:-

On case object there is 1 Picklist field --> Stage__c
There are 3 stages of cases --> Initiation,Verification,Completion (Picklists value of Stage__c)
Case is moving from User1(Initiator) to User2(Verifier) to User3(Completion).
 Once case reaches to Users3 (Means at Completion Stage),Email Should send only to User1(to Initiator) (who sent the case to User2).
           
For above requirment I created Process Builder,It works fine..But Email goes only to previous case owner. I am expecting it should go only to whom who assigned case to User2.

Please Help with apex triggers or class.

Best Answer chosen by Akshay Mhetre
CharuDuttCharuDutt
Hii Akshay Mhetre
Try Below Trigger
trigger caseTrigger on Case (after update) {
    set<Id>OwnerSet = new set<Id>();
    List<Messaging.SingleEmailMessage> mails = new List<Messaging.SingleEmailMessage>();
    list<string> sendTo = new list<String>();
    for(Case c : trigger.new){
        if(c.OwnerId != trigger.oldmap.get(c.Id).OwnerId ){
            OwnerSet.add(c.OwnerId);
        }
    }
    list<User> lstusr = [Select Id,Email from User Where Id In : OwnerSet];
    for(User u : lstusr){
        sendTo.add(u.Email);
    }
    if(!sendTo.isEmpty()){
            Messaging.SingleEmailMessage mail = new Messaging.SingleEmailMessage();
            mail.setSubject('Test Subject');
            String body = 'Test Body';
            mail.setToAddresses(sendTo);
            mail.setHtmlBody(body);
            mails.add(mail);
            try{
                Messaging.SendEmail(mails);
            }
            catch(Exception e){
                System.debug('-----Exception------' +e);        
            }
        }
}
Please Mark It As Best Answer If It Helps
Thank You!

All Answers

CharuDuttCharuDutt
Hii Akshay Mhetre
Try Below Trigger
trigger caseTrigger on Case (after update) {
    set<Id>OwnerSet = new set<Id>();
    List<Messaging.SingleEmailMessage> mails = new List<Messaging.SingleEmailMessage>();
    list<string> sendTo = new list<String>();
    for(Case c : trigger.new){
        if(c.OwnerId != trigger.oldmap.get(c.Id).OwnerId ){
            OwnerSet.add(c.OwnerId);
        }
    }
    list<User> lstusr = [Select Id,Email from User Where Id In : OwnerSet];
    for(User u : lstusr){
        sendTo.add(u.Email);
    }
    if(!sendTo.isEmpty()){
            Messaging.SingleEmailMessage mail = new Messaging.SingleEmailMessage();
            mail.setSubject('Test Subject');
            String body = 'Test Body';
            mail.setToAddresses(sendTo);
            mail.setHtmlBody(body);
            mails.add(mail);
            try{
                Messaging.SendEmail(mails);
            }
            catch(Exception e){
                System.debug('-----Exception------' +e);        
            }
        }
}
Please Mark It As Best Answer If It Helps
Thank You!
This was selected as the best answer
CharuDuttCharuDutt
Hii Akshay Mhetre
Please Close Your Query By Marking It As Best Answer If It Helps So It Also Helps Others In Future
Thank You!