Trigger.isInsert() and trigger.isUpdate() in salesforce

Trigger.isInsert() and trigger.isUpdate() in salesforce

Sample Trigger:

 
trigger memberInviteNotify on Member__c ( after insert,after update ) {
    
    List < Messaging.SingleEmailMessage > listMessages = new List < Messaging.SingleEmailMessage >();
    
    for ( Member__c member:trigger.New ) {
    
        String[] toAddresses = new String[] {member.E_Mail_Id__c};
        String messageBody;
        Messaging.SingleEmailMessage mail = new Messaging.SingleEmailMessage();
        mail.setToAddresses(toAddresses);
 
        //Email invitation
        if(trigger.isInsert)
        {
            mail.setSubject( ‘Welcome to InfallibleTechie’ );
            messageBody = ‘Hi ‘ + member.Name + ‘, Welcome to InfallibleTechie’;
            mail.setHtmlBody(messageBody);
        }
       
        //Email notification
        if(trigger.isUpdate) {
       
            mail.setSubject( ‘Updates in your details’ );
            messageBody = ‘Hi ‘ + member.Name + ‘, Changes have been made to your details. Contact administrator if you are not responisble.’;
            mail.setHtmlBody(messageBody);
        }
       
        listMessages.add( mail );
        
    }

    Messaging.sendEmail( listMessages );
 
}

Leave a Reply