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
Thakkar ParthThakkar Parth 

Change Lead status using Trigger

Can anyone please help me out to get a tigger . I would like to change Status of a Lead  and notification should be sent from the owner of that lead .

I know notification can be sent using Workflow . But i dont know how to write Trigger.Help appreciated.

Thanks,
Marc
Virendra ChouhanVirendra Chouhan
Hi Marc,

So you want to work this with the help of trigger.
ok, just a question When (i.e. after  Insert, update ) and how to notify the owner (i.e Email)?


Regards
Virendra
Thakkar ParthThakkar Parth
Yes with the help of Trigger . It should be WHEN the Lead Status field is updated .Yes, with an Email, it should be notified. Thanks !
Virendra ChouhanVirendra Chouhan
OK Marc

Means every time when the status field is updated. it'll notiy the owner with an email.
AshwaniAshwani
When you trigger execute you can change owner field in trigger. Then write a workflow rule on Lead and setup email alert to owner.email.
Grazitti TeamGrazitti Team
Grazitti Team
Hi Marc,

Create Atrigger on Lead:

trigger NAME_OF_TRIGGER on lead( EVENT_ON_WHICH_YOU_WANT_TO_CHANGE_STATUS){
list<lead> ld = new list<lead>();
for(Lead l : Trigger.new){
l.status = 'NEW_STATUS';
ld.add(l);
}
update ld;
}



If your event is after update then you have to maintain a static boolean variable with default value false and in trigger set that value to true. and before update in trigger check that boolean value.
trigger NAME_OF_TRIGGER on lead( EVENT_ON_WHICH_YOU_WANT_TO_CHANGE_STATUS){
list<lead> ld = new list<lead>();
if(abc.isupdated == false){
for(Lead l : Trigger.new){
l.status = 'NEW_STATUS';
ld.add(l);
}
abc.isupdated == true;
}
update ld;
}

public class abc{
public static boolean isupdated = flase;
}


let us know if you have any question .
please don't Forget to Mark this as your best answer if it works fine for you

Regards,
Grazitti Team
Web: www.grazitti.com

Thakkar ParthThakkar Parth
Hi All,

Thanks for the very quick response .I've written something like this but its giving me an error.

Error : Error: Compile Error: Variable does not exist: OwnerId at line 19 column 24

trigger Lead on Lead (after update)
{
    Map <Id, String> leadsOwner = new Map <Id, String> ();

    // Go through every lead in the trigger
    for (Lead lead : trigger.new)
    {
        // Check if the status has been changed
        if (lead.Status != trigger.oldMap.get(lead.Id).Status)
        {
            // get the owner ID's that have been affected
            leadsOwner.put(lead.ownerId, null);
        }
    }

    // Map the owner ID to it's email address
    for (User owner : [SELECT Id, Email FROM User WHERE Id = :leadsOwner.keySet()])
    {
        leadsOwner.put(Id, Email);
    }

    List <Messaging.SingleEMailMessage> emails = new List <Messaging.SingleEMailMessage> ();

    // Go again through every lead in the trigger
    for (Lead lead : trigger.new)
    {   
        // Only work with leads that have owners mapped to their email addresses (only those ones have their status changed)
        if (leadsOwner.get(lead.OwnerId) != null)
        {
            // Create an email message and add it to the list
            Messaging.SingleEmailMessage mail = new Messaging.SingleEmailMessage(); 
            List <String> toAddresses = new List <String> {leadsOwner.get(lead.OwnerId)};
            mail.setToAddresses(toAddresses); 
            mail.setSubject('Automated Email : Lead Status Updated'); 
            String body = 'The status has been changed on the lead record with ID ' + lead.Id; 
            mail.setPlainTextBody(body); 
            emails.add(email);
        }
    }
  }

Can someone please help me to solve my error  . I think this trigger should work. Thanks again
Thakkar ParthThakkar Parth
Any help with the error please ? I'm stucked now .
Virendra ChouhanVirendra Chouhan
Hi Marc,

Just a littel bit change your code
like in line 19 write
leadOwner.put(owner.id, owner.email);
And in line 37 write

emails.add(email);

And also add  
Messaging.sendEmail(emails);
After emails.add(email); } }

So Your full code is like 

trigger UPdateLeadStatus on Lead (after update) {
    Map <Id, String> leadsOwner = new Map <Id, String> ();

    // Go through every lead in the trigger
    for (Lead lead : trigger.new)
    {
        // Check if the status has been changed
        if (lead.Status != trigger.oldMap.get(lead.Id).Status)
        {
            // get the owner ID's that have been affected
            leadsOwner.put(lead.ownerId, null);
        }
    }

    // Map the owner ID to it's email address
    for (User owner : [SELECT Id, Email FROM User WHERE Id = :leadsOwner.keySet()])
    {
        leadsOwner.put(owner.Id, owner.Email);
    }

    List <Messaging.SingleEMailMessage> emails = new List <Messaging.SingleEMailMessage> ();

    // Go again through every lead in the trigger
    for (Lead lead : trigger.new)
    {   
        // Only work with leads that have owners mapped to their email addresses (only those ones have their status changed)
        if (leadsOwner.get(lead.OwnerId) != null)
        {
            // Create an email message and add it to the list
            Messaging.SingleEmailMessage mail = new Messaging.SingleEmailMessage(); 
            List <String> toAddresses = new List <String> {leadsOwner.get(lead.OwnerId)};
            mail.setToAddresses(toAddresses); 
            mail.setSubject('Automated Email : Lead Status Updated'); 
            String body = 'The status has been changed on the lead record with ID ' + lead.Id; 
            mail.setPlainTextBody(body); 
            emails.add(mail);
        }
    }
    Messaging.sendEmail(emails);
}

Regards
Virendra