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
MrWasabiMrWasabi 

Make Trigger Fire Only on Leads

I have a task Trigger that validates and works great on Leads. The problem is that the trigger fires on also for tasks on Accounts and Opporutnities. I've very new to Apex, so I'm sure I am overlooking something simple. 

 

How do I make this trigger fire only when inserting a task on a lead?

 

trigger UpdateLeadStatusOnTask on Task (after insert) {

//Retrieve task data after a new task has been saved
Task theTask = trigger.new[0];

	//Create the lead (Lead Status and the IsConverted fields) for the Lead which the task was logged against
        Lead theLead= [Select Status,IsConverted from Lead where id=:theTask.WhoId];

    //Check to see if Marketo is the owner of the task, we only want tasks not owned by Marketo
    if(theTask.ownerid<>'00540000001GDw2'){
    
        
            //Only use leads that have a status of open
            if(theLead.Status == 'Open'){
            
            //Change the status to contacted
            theLead.Status = 'Contacted';
            
        //apply the changes to the lead
        update theLead;
        }
    } 
}

 

Best Answer chosen by Admin (Salesforce Developers) 
RamitRamit

Hi,

 

You need to check that whoId starts with ('00Q').

 

Please see the changes in the code: 

 

trigger UpdateLeadStatusOnTask on Task (after insert) 
{

	//Retrieve task data after a new task has been saved
	Task theTask = trigger.new[0];
	if(theTask.whoID <> null)
	{
		if(String.valueOf(theTask.WhoId).startswith('00Q'))
		{
			//Create the lead (Lead Status and the IsConverted fields) for the Lead which the task    was logged against
			Lead theLead= [Select Status,IsConverted from Lead where id=:theTask.WhoId];

			//Check to see if Marketo is the owner of the task, we only want tasks not owned by Marketo
			if(theTask.ownerid<>'00540000001GDw2')
			{
			
				
				//Only use leads that have a status of open
				if(theLead.Status == 'Open')
				{
					//Change the status to contacted
					theLead.Status = 'Contacted';				
					//apply the changes to the lead
					update theLead;
				}
			} 
		}	
	}	
}

 

 

 

All Answers

RamitRamit

Hi,

 

You need to check that whoId starts with ('00Q').

 

Please see the changes in the code: 

 

trigger UpdateLeadStatusOnTask on Task (after insert) 
{

	//Retrieve task data after a new task has been saved
	Task theTask = trigger.new[0];
	if(theTask.whoID <> null)
	{
		if(String.valueOf(theTask.WhoId).startswith('00Q'))
		{
			//Create the lead (Lead Status and the IsConverted fields) for the Lead which the task    was logged against
			Lead theLead= [Select Status,IsConverted from Lead where id=:theTask.WhoId];

			//Check to see if Marketo is the owner of the task, we only want tasks not owned by Marketo
			if(theTask.ownerid<>'00540000001GDw2')
			{
			
				
				//Only use leads that have a status of open
				if(theLead.Status == 'Open')
				{
					//Change the status to contacted
					theLead.Status = 'Contacted';				
					//apply the changes to the lead
					update theLead;
				}
			} 
		}	
	}	
}

 

 

 

This was selected as the best answer
MrWasabiMrWasabi

Ramit, this is excellent! Thank you!

RamitRamit
You are welcome! Thanks & Regards, Ramit Singh ------------------------------------------ Salesforce.com Certified-Administrator | Developer | Sales Cloud Consultant Flex Developer - Actionscript