enter image description here

how to set the Default value of any field on standard record via trigger? For example: when you click New on Lead component, I want user field to be set to 'current USER' - I want to do this via trigger

enter image description here

  • Triggers do not involve any UI manipulation. That is to say, you can't influence the appearance of the Edit window with a trigger because a trigger has no UI, it is code based logic that happens at the database level. – greenstork Mar 19 '14 at 19:11

I'm guessing the requirement is :

When a new record is created I want to set a value into field xxx__c, but the value that is set will be dependent on other factors that need to be calculated at run-time.

So here we can not use a field level default, as the value will be different at different times (perhaps due to the user or profile or other data), we also can not use a trigger as we want to show the value on the screen and perhaps give the user the opportunity to change it, as this is just a starting default.

The pattern I would employ for this would be:

  1. Create a VF page, this is a "null" page that is used to call the apex controller and then redirect to the standard page.
  2. Create an OverrideController to handle all the custom logic to calculate the value of XXX__c. This then uses url encoding to pass the value of xxx_c via the url and redirects to the standard page
  3. Override the New Button on the object to use the VF page, so that all "new" records are created through this means.

In the example below, we store the default for xxx__c at a per user level in a custom field on the user record, but you can implement any apex logic obviously.

For this pattern to work we do need the fields on the standard page to be editable.

The VF page:

<apex:page standardcontroller="lead" extensions="LeadOverrideController" action="{!PageLoadEvent}">
<!--  This page is used as override to the new lead button, -->
<!-- calling the pageLoadEvent on page load to generate user defaults and redirect to the standard lead create page -->

The controller:

public with sharing class cdcrm_LeadOverrideController { 
    public LeadOverrideController(ApexPages.StandardController controller) {}  

    // ### method return url string will params as per user defaults
    public pagereference PageLoadEvent(){ 
        //get the user information that we need, this is where we store the per user default for xxx__c
        User u = [Select id, name, default_XXX__c from User where id =: UserInfo.getUserId() LIMIT 1];

        // base url, nooverride to stop the overridew loop
        string URLStr = '/00Q/e?nooverride=1';     
        // add the existing return URL back on                                                  
        URLStr = URLStr + '&retURL=' + ApexPages.currentPage().getParameters().get('retURL');       

        //custom fields (url encode value for field xxx__c)
        URLStr = URLStr + '&00Nd0000006gc4S=' + default_XXX__c;

        pageReference pageRef = new pagereference(URLStr);   
        return pageRef;

I found that implementing a trigger to solve this issue, did not solve the question of setting a default value. Because even on a trigger set to before insert, the trigger fires on Save, not on create, which is what you want on setting a default value.

I implemented this functionality using a Visualforce page that did an URL jack to a custom Lookup/User field as follows.

<apex:page standardController="Submitted_Item__c" >
      window.top.location.href = '/a0O/e?nooverride=1&CF00N400000025ScP={!$User.FirstName}+{!$User.LastName}';

Submitted_Item__c is a custom object where I choose to override the New button with this page

a0O is the internal code for the Submitted Item object

I include a nooverride=1 on advice from other posts to avoid redirect looping

CF00N400000025ScP = CF + 00N400000025ScP where CF is the custom field designation

and I use {!$User.FirstName}+{!$User.LastName} to add the two global variables as my default name for this lookup fields entry (it's expecting a full name value)

Things to remember in setting this up, please make sure: 1) Set the key Profiles you're working with to use the VF page you create and 2) Over-ride the New button on your custom object.

* To avoid needing VF pages like this in the future, it would be great if Salesforce allowed workflow field updating on Lookup values using formulas and/or global variables


Using an "after insert" trigger to update the record being saved is not a best practice. Instead, use a "before insert" trigger.

trigger SetInitialData on MyStandardObj(before insert){
    List<MyStandardObj> updatingList = new List<MyStandardObj>();
    for(MyStandardObj myObj : trigger.new){
        myObj.Description = 'XYZ';

This saves DML and prevents possible recursion errors.

However, if what you want is to have a field in the UI pre-filled when the user clicks "New," just set the default value on the field to that value.

In general, focus on what you want to do, and then choose the right tool. You cannot pre-set a value when clicking New via a trigger - that's just not the right tool for the job. What is more important? Defaulting the value in the UI or writing a trigger? I suspect the former, so don't even use a trigger at all.

New answer, given new information There are a few ways to handle this. The easiest is to use a Chatter Action, and autofilling that field to the current user.

Other solutions will require Visualforce and setting the default value in the controller extension - and then overriding the "New" scren with your new page. It would be easier than you think because you could use an apex:detail tag to just show the regular screen. But then you would need Apex and test coverage.

The last option might seem easy, but a URL hack is unsupported and is generally not a best-practice if other methods are available. But if you were to do it, you would make a custom button and would pass the current user's Id into that field.

  • A comment on the trigger: the question doesn't really ask this, but it would be common for the trigger to set the value only if it is not null so that a value input by a user takes priority. (And updatingList needs removing.) – Keith C Mar 17 '14 at 9:29
  • Actually i want to populate default look-up filed value when i create a new record – Faisal Mar 17 '14 at 13:29

I liked how @dacology approached the problem and interpreted it. Here is how I am interpreting and trying to make it simple to digest:

If I create a new Object with standard Page functionality with a lookup field to a user object. Is there a simple way to default the User Lookup field to the current logged in USER, and still allow the user to do a lookup and change the value to any other user.

Considering above, field default and trigger doesn't seems to be working, as he wants to populate the value when you click NEW button and land on the page. In theory, it should be simple, but within the parameters of Salesforce.com platform, seems like it is not as simple as I think.

Wondering, if there is a simple way or what @dacology described is the best option.


If you want to set initial(default) value to a field, you can do it by having a after insert trigger.

trigger SetInitialData on MyStandardObj(after insert){
    List<MyStandardObj> updatingList = new List<MyStandardObj>();
    for(MyStandardObj myObj : trigger.new){
        myObj.Description = 'XYZ';


         update updatingList;
  • Actually i want to populate default look-up filed value when i create a new record – Faisal Mar 17 '14 at 13:27
  • For editing the record itself in Trigger.new, an "after insert" trigger is inappropriate. Use "before insert" in general. (Of note, in this specific example Faisal asked, a trigger won't help at all - I'm just addressing the "after insert" suggestion.) – DavidSchach Mar 26 '14 at 0:45

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.