+ Start a Discussion
ram dram d 

Need Help in Writing After Update Trigger

Hi All ,I have requirement if any one helps i am very thankful to them please see my requirement below
I have 3 objetcs like Account,Account Convace,Catlog Account is lookup of Account convace and Catlog is Master object for Account Convas so when the user changes the Account name in Account convas then we need to send a email for  all the users in the Account convas with Account old name and new name please any one help in this Requirement 
Raja ReddyRaja Reddy
Hope this helps yyou
trigger SendEmailOnOwnerChange on Lead (before update,After Update) {
    if (trigger.old[0].OwnerId != trigger.new[0].OwnerId ) {
        Messaging.SingleEmailMessage mail = new Messaging.SingleEmailMessage();

        //Select the Email Address of the old owner
        String emailAddrnew = [select Email from User where Id = :trigger.old[0].OwnerId].Email;
        String emailAddrold = [select Email from User where Id = :trigger.new[0].OwnerId].Email;
        
        //Select the new owner and old owner from user
        String newOwnerName = [select Name from User where Id = :trigger.new[0].OwnerId].Name;
        String oldownerName=[select name from User where Id =:trigger.old[0].OwnerId].Name;
      
      
        //Store the email address
        String[] emailAddrnew1 = new String[] {emailAddrnew};
        String[] emailAddrold1 = new string[]{emailAddrold};
        mail.setToAddresses(emailAddrnew1 );
        mail.setCcAddresses(emailAddrold1 );

        mail.setSubject('Owner Changed for Lead : ' + trigger.new[0].Name);

        mail.setPlainTextBody('Owner of lead: ' + oldownerName + ' Changed to ' + newOwnerName);
        mail.setHtmlBody('This is to notify that the Owner of lead: https://ap1.salesforce.com/ <b>' + trigger.new[0].Id+'<b>&nbsp;&nbsp;Changed from</b>&nbsp;&nbsp;'+oldownerName  + '</b>&nbsp;&nbsp; Changed to <b>' + newOwnerName  + '</b>');

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