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
MargiroMargiro 

Limit trigger to a VF page

Hello

I have written a trigger that sends an email when a lead is updated. However I only want the email to be sent when a lead is created using a visualforce page not when a lead is created directly form salesforce. Currently, the trigger is selecting the last created lead not necessarily the lead created using the page. How can I limit the trigger so that it only sends using the vf page and not the interface? Or, do I have to send the email using a different method?

Trigger

 

trigger mhform on Lead (after insert) { Messaging.SingleEmailMessage mail= new Messaging.SingleEmailMessage(); String[] toAddresses = new String[]{'margiro@piab.com'}; Lead lead = [SELECT Id from Lead Order by Lead.createdDate Desc limit 1]; mail.setToAddresses(toAddresses); mail.setTemplateId('00X400000016by4'); mail.setTargetObjectId(lead.Id); mail.setReplyTo('support@acme.com'); mail.setSenderDisplayName('Piab Support'); mail.setSaveAsActivity(false); mail.setBccSender(false); mail.setUseSignature(false); Messaging.sendEmail(new Messaging.SingleEmailMessage[] { mail}); }

 


 

Best Answer chosen by Admin (Salesforce Developers) 
sforce2009sforce2009

you can do it in 2 ways

1. You can write the email code on the visual force page's controller class itself

2. Create a Boolean Custom Filed on the lead object and update it as true when the lead is created from vf page through the controller class and check in the trigger if the value is true, then only process else not.

 

hope this helps 

All Answers

sforce2009sforce2009

you can do it in 2 ways

1. You can write the email code on the visual force page's controller class itself

2. Create a Boolean Custom Filed on the lead object and update it as true when the lead is created from vf page through the controller class and check in the trigger if the value is true, then only process else not.

 

hope this helps 

This was selected as the best answer
MargiroMargiro

Thank you!

Working on placing the code on the controller.

-Matt