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
dasari123dasari123 

trigger to send a email when field is updated

HI ,
  I have some fields in a object ,My requirment is i have selected four fields ,if any of these four fields value is changed i want to send a email to the record owner with only one updated field value.
Vinit_KumarVinit_Kumar
Have you written something or do you want someone else to write something for you.

I would suggest to write something based on the suggestion below :-

Create a before update Trigger on your object which would check whether the value has been changed in any of your fields,if that is true then send an email using Messaging.SingleEmailMessage class.

If this helps,please mark it as best answer to help others :)
Developer.mikie.Apex.StudentDeveloper.mikie.Apex.Student

I could probably get you started.


If this helps...please mark as either best answer or like it please. This trigger is designed to send an email when a field is changed...You will just have to change the object and field names, I will highlight the important bits. It works by getting triggered when a field is changed and then uses an email handler to send the email.


Email handler class:

public class EmailHandler{


      //ClientPath Emails
      public static void sendTriggerEmail (List<String> contactids){
     
        Messaging.MassEmailMessage mail = new Messaging.MassEmailMessage();
        //set the email properties
        mail.setTargetObjectIds(contactids);
        mail.setSenderDisplayName('Your company customer service'); //change to whatever
        mail.setTemplateId('00X90000001F79Z'); //Id of the Email Template
     
        //send the email
        Messaging.sendEmail (new Messaging.MassEmailMessage[] { mail } );
        }
     
        
}

and the trigger:
 

trigger SendEmailtocontact on Service__c (after Update)
{
    List<String> lcontactEmails = new List<String>();
    Set<Id> sIds = new Set<Id>();
//Added
    Set<ID> accIds = new Set<ID>();
    for(Service__c qItr : Trigger.new)
{
        if(Trigger.isUpdate)
  {
            if(Trigger.oldmap.get(qItr.id).Last_Email_Sent__c!= qItr.Last_Email_Sent__c ) //basically says that if this field is changed it will trigger the trigger
   {
                sIds.add(qItr.id);
    //Added
    accIds.add(qItr.Account__c);
            }
        }
  //Will Never Execute as trigger will fire only if a record is updated
        else if(Trigger.isInsert)
  {
           
                sIds.add(qItr.id);
            
        }
    }
// Modified
    //for(Account accItr : [SELECT id,(SELECT id FROM Contacts) FROM Account WHERE id IN (SELECT Account__c FROM Service__c WHERE Id IN: sIds)])
for(Account accItr : [SELECT id,(SELECT id FROM Contacts) FROM Account WHERE id IN : accIds])
{
        for(Contact con : accItr.contacts)
  {
            if(!String.isBlank(con.id))
   {
                lcontactEmails.add(con.id);
            }
        }
       // No need to put this condition here.
  /*if(!lcontactEmails.isEmpty())
  {
   EmailHandler.sendEmail(lcontactEmails);
  }*/   
    }
//Put this here
if(!lcontactEmails.isEmpty())
{
for(Service__c serv : Trigger.new)
{        
        //Email handler Updated
        if(Serv.Last_Email_Sent__c == 'IPC Support Expiry') //Set critera if statement..Add others to set different emails based on different criteria
        {
        EmailHandler.sendTriggerEmail(lcontactEmails);
        }

}   
}
}
I hopethis helps. Remember to mark as solution when it works. Thank you


 

Andries.NeyensAndries.Neyens
Can this not be done with a simple Workflow rule and an email alert?
(using the  PRIORVALUE function )