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
raj sfdc 8raj sfdc 8 

Trigger is fire for the same picklist feild value again

when ever account type is changed form any value to 'Customer' need to 
send an email to account owner saying your account become customer 
and create a task and assign it to account owner.



Trigger:

trigger accouncustomertype on Account ( after update) {
     
    accountcustomer.email_send (Trigger.old);
    
     List <task> taskToInsert = new List <task> ();

   for (Account a : Trigger.old) {
       if(a.Type=='Customer'){
           
            Account ab =  (Account) Trigger.oldMap.get(a.Id);
             Boolean oldtypeiscustomer = ab.Type.equals('Customer');
             Boolean   oldtype = a.Type.equals('Customer');
    
    if (!oldtypeiscustomer && oldtype ) {
      a.awesome__c = true;        
    } 
           
           
   task t = new task ();
   t.Subject = 'please call me';
   t.OwnerId = a.OwnerId;
   t.WhatId = a.id;
   taskToInsert.add(t);
}
}
insert taskToInsert;
}


Class :
 
 public class accountcustomer {
    
    public static void email_send( List <Account> acclist)
        {
        List<Messaging.SingleEmailMessage > Email_list=new List<Messaging.SingleEmailMessage >();
             for(Account Acc : acclist ){
              if(Acc.Type=='Customer'){
                  
                   //trigger.oldmap.account.Type(Customer)== trigger.newmap.account.Type(Customer)
                
                  
             Account a =  (Account) Trigger.oldMap.get(Acc.Id);
             Boolean oldtypeiscustomer = a.Type.equals('Customer');
             Boolean   oldtype = Acc.Type.equals('Customer');
    
    if (!oldtypeiscustomer && oldtype ) {
      Acc.awesome__c = true;        
    } 
                
                System.debug('email send');
                Messaging.SingleEmailMessage message = new Messaging.SingleEmailMessage();
                   message.toAddresses =new String[] {'rajasekhara.bhavanam@forsysinc.com','rajarede08@gmail.com'};
                message.setSubject('your account changed to customer');    
                String Body_data='';
                Body_data=Acc.Name;
                Body_data=Acc.Type;
                message.sethtmlBody(Body_data);
                Email_list.add(message);
                Messaging.sendEmail(new Messaging.SingleEmailMessage[] {message});
                }
        }
        
        
    }
}


My trigger is firing but well,But i don't want my trigger to fire when am again edit the same picklist value with Type:Custommer.First time it has to fire when the account Type is customer but i don't have to fire when the am editing it with same account type with value customer.
am got stuck with this issue please can anyone help me
Raj VakatiRaj Vakati
try this
 
trigger accouncustomertype on Account ( before update) {
     
    accountcustomer.email_send (Trigger.new);
    
     List <task> taskToInsert = new List <task> ();

   for (Account a : Trigger.new) {
       if(a.Type=='Customer'){
           
            Account ab =  (Account) Trigger.oldMap.get(a.Id);
             Boolean oldtypeiscustomer = ab.Type.equals('Customer');
             Boolean   oldtype = a.Type.equals('Customer');
    
    if (!oldtypeiscustomer && oldtype ) {
      a.awesome__c = true;        
    } 
           
           
   task t = new task ();
   t.Subject = 'please call me';
   t.OwnerId = a.OwnerId;
   t.WhatId = a.id;
   taskToInsert.add(t);
}
}
insert taskToInsert;
}
 
public class accountcustomer {
    
    public static void email_send( List <Account> acclist)
        {
        List<Messaging.SingleEmailMessage > Email_list=new List<Messaging.SingleEmailMessage >();
             for(Account Acc : acclist ){
              if(Acc.Type=='Customer'){
                  
             Account a =  (Account) Trigger.oldMap.get(Acc.Id);
             Boolean oldtypeiscustomer = a.Type.equals('Customer');
             Boolean   oldtype = Acc.Type.equals('Customer');
    
				if (!oldtypeiscustomer && oldtype ) {
				  Acc.awesome__c = true;        
				} 
                
                System.debug('email send');
                Messaging.SingleEmailMessage message = new Messaging.SingleEmailMessage();
                   message.toAddresses =new String[] {'rajasekhara.bhavanam@forsysinc.com','rajarede08@gmail.com'};
                message.setSubject('your account changed to customer');    
                String Body_data='';
                Body_data=Acc.Name;
                Body_data=Acc.Type;
                message.sethtmlBody(Body_data);
                Email_list.add(message);
                Messaging.sendEmail(new Messaging.SingleEmailMessage[] {message});
                }
        }
        
        
    }
}

 
Maharajan CMaharajan C
HI Raj,

Please use the below well formatted trigger and helper class for your scenario:

=======================
Trigger : 

trigger accouncustomertype on Account (before update) {
    if(Trigger.IsBefore)
    {
        if(Trigger.IsUpdate)
        {
            AccountTriggerHelper.sendEmail(Trigger.OldMap, Trigger.New);
        }
    }
}

=======================
Apex Class:

public class AccountTriggerHelper {
    
    public static void sendEmail(Map<Id,Account> oldMap , List<Account> accounts)
    {
        List<Task> tasktoInsert = new List<Task>();
        List<Messaging.SingleEmailMessage> mails = new List<Messaging.SingleEmailMessage>();
        for(Account acc : accounts)
        {
            Account a =  (Account) oldMap.get(acc.Id);
            Boolean oldtypeiscustomer = a.Type.equals('Customer');
            if (!oldtypeiscustomer && acc.Type=='Customer' ) {
                acc.awesome__c = true;        
                
                task t = new task ();
                t.Subject = 'please call me';
                t.OwnerId = acc.OwnerId;
                t.WhatId = acc.id;
                taskToInsert.add(t);
                
                Messaging.SingleEmailMessage message = new Messaging.SingleEmailMessage();
                // list of people who should get the email
                List<String> sendTo = new List<String>();
                sendTo.add(acc.OwnerId);
                message.setToAddresses(sendTo);
                message.setSubject('your account changed to customer'); 
                message.setSenderDisplayName('MahaRajan C');
                message.setReplyTo('maharaja0393@gmail.com');
                String Body_data='';
                Body_data = 'Account Name is ' + acc.Name + ' &&  Type is ';
                Body_data += acc.Type;
                message.sethtmlBody(Body_data);
                mails.add(message);
            } 
        }
        
        // Send all emails in the master list
        if(mails.size()>0) Messaging.sendEmail(mails);
        
        if(tasktoInsert.size() > 0) Insert tasktoInsert;
    }
}

Can you please Let me know if it helps or not!!!

If it helps don't forget to mark this as a best answer!!!


Thanks,
Maharajan.C


 
raj sfdc 8raj sfdc 8
Thanks for response people.
Again my trigger is firing for the same picklist value even if am editinng it with the same value.
Maharajan CMaharajan C
So if my understanding is correct you will send the email and task create against the Account for Account type as customer as only one time.

Even if some changed the value from customer to some other types and then again the account comes to customer at that you want this functions right. If yes use the below condtion. In the awesome__c custom field the default value should be Unchecked.

Change the if condition in the apex class as below:

 if (!oldtypeiscustomer && acc.Type=='Customer'  && acc.awesome__c != true)