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
MattMet86MattMet86 

New Record Screen - class or trigger when user clicks "New"

Is there a way to run a trigger or class when a user clicks the New record button? 

For example, I want a bit of code to run when the user clicks the New Account button. This way I can show them an alert or other message before they put an details in for the new record and click Save which fires an existing trigger. 

I need osmething like "onload Insert"...
Vijay NagarathinamVijay Nagarathinam
Hi,

The apex trigger is firee, when you perform any dml operation on the specific object.

Thanks,
Vijay
 
MattMet86MattMet86
I am looking to run code on "New" button click, not "Save". *Matt Metzinger* *Salesforce Admin / Developer* *CRM Team* *(913) 652-2087*
Deepak GulianDeepak Gulian
On save it is possible, but on Standard New Button you cant fire a trigger or Class.
Tolga SunarTolga Sunar
You can overwrite the functionality of standard "New" button and call a Visualforce Page. This Visualforce Page can directly call an Apex method or display an alert upon click, then redirect to the record creation page or any page you wish.

Triggers will execute when a button click issues any DML operation.
MattMet86MattMet86
Can you provide some example code of how to go from the visual page to the standard record create page? *Matt Metzinger* *Salesforce Admin / Developer* *CRM Team* *(913) 652-2087*
Tolga SunarTolga Sunar
Visualforce Page that you are going to call from your overridden standard New Record button should look like this:
<apex:page>
    <script>
        alert('Hello World!');
        if((typeof sforce != 'undefined') && sforce && (!!sforce.one)) {
            sforce.one.createRecord('Account'); 
        }
        else {
            window.location.href = '/001/e';
        }
    </script>
</apex:page>
This one will display an alert upon click, and redirect to new record page as the user clicks "OK" on the pop-up window. If SF1 or LEX platform is detected, sforce.one navigation is executed. Otherwise the window redirection is executed. Also note that I have used Account object there, you should change the wrapped statements inside navigation commands.

If you would like to execute an Apex code as well, you gotta add an action attribute to the apex:page tag, and call a method from there via controller.
 
MattMet86MattMet86
How do you get a custom object record creation page instead? 
window.location.href = '/001/e';  takes me to Account. 
I need to go to the record create screen for Employee_Session__c
 
Tolga SunarTolga Sunar
Just open an Employee_Session__c record and fetch the first three characters of its Id in the URL and replace with 001.