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
Sanchivan SivadasanSanchivan Sivadasan 

Override New Button and Pre-populate fields

Hi there,

I have an object called Options. This object has a field called Region and this field needs to default to the user's region. The User Object has a picklist value called region. We want to default this value to the user's region and then give them ability to change it if they want to if needed. We have lightning enabled. I want to make this work for classic and lightning if possible. I am trying to override the New Button for the Option Object and pre-populate the fields that I need to pre-populate and then redirect the user to the object which we need to redirect them to. How do I go about doing this? 

This is what I have done so far and have not worked:

I created a Visualforce page called OptionNewBtnOverride
 
<apex:page standardController="Option__c" extensions="OptionExtension" action="{!pageDirect}">
    <script>
    	window.onLoad = function() {
            var defaultValues = {
                               'Region__c' : {!region}
                             };
        	sforce.one.createRecord('Option__c', null, defaultValues);   
        }
    </script>
</apex:page>
I also have an extension class called OptionExtension as follows:
public class OptionExtension {
    public String region {get; set;}
    public OptionExtension(ApexPages.StandardController controller) {        
    }
    
    public void pageDirect() {
        string region = [Select Region__c From User Where Id = :UserInfo.getUserId()][0].Region__c;
    }
}

For the Option Object's New Button override, I have selected, visualforce for classic and for lightning and mobile I have selected use salesforce classic override. Can someone tell me what I am doing wrong here? Thanks
Best Answer chosen by Sanchivan Sivadasan
Sanchivan SivadasanSanchivan Sivadasan
I figured out what the issue was. The issue was I was not setting the region properly. I created a new variable called region within the constructor rather than setting the public variable. I modified the code quite a bit and you can check if you are in classic and if you are do what we normally do. I am putting the working code here so it will be helpful for someone else in the future.

Here is how my vf looks:
 
<apex:page standardController="Option__c" extensions="OptionExtension" action="{!pageDirect}">
    <apex:outputPanel rendered="{!!isClassic}">
        <script>
            var region = "{!region}";
        </script>
        <apex:outputPanel rendered="{!!isClassic}">
            <script src="/soap/ajax/15.0/connection.js"></script>
            <script>
                var defaultFieldValues = {
                    "Region__c":region
                };
                
                sforce.one.createRecord('Option__c', null, defaultFieldValues);               
            </script>
        </apex:outputPanel>
    </apex:outputPanel>
</apex:page>

Here is the extension class:
public class OptionExtension {
    public String region {get; set;}
    private String returnURL;
    
    public OptionExtension(ApexPages.StandardController controller) {
        returnURL = ApexPages.currentPage().getParameters().get('retURL');
		region = [Select Region__c From User Where Id = :UserInfo.getUserId()][0].Region__c;
        
        if(String.isEmpty(region))
            region = 'Eastern';
    }
    
    public Boolean getIsClassic(){
        return (UserInfo.getUiThemeDisplayed() =='Theme3' && ApexPages.currentPage().getParameters().get('beLightning') == null);
    }
    
    public PageReference pageDirect() {
        Boolean isClassic = getIsClassic();
        
    	PageReference pageRef = null;

    	if(isClassic || test.isRunningTest()) {
    		Schema.DescribeSObjectResult describeInfo = Option__c.sObjectType.getDescribe();
			String objectIdentifier = describeInfo.getKeyPrefix();

            pageRef = new PageReference('/' + objectIdentifier + '/e');
			// I am getting the field Ids from custom setting so this will work on sandboxes and in Production and no code change is required.
            Option_Fields_Ids__c customSetting = Option_Fields_Ids__c.getInstance();   

            String regionalIdentifier = customSetting.RegionalIdentifier_c__c;
            pageRef.getParameters().put(regionalIdentifier, region);
            pageRef.getParameters().put('cancelURL', returnURL);
			// nooverride is required, otherwise you will have an infinite loop
            pageRef.getParameters().put('nooverride', '1');

        	pageRef.setRedirect(true);
    	}
        
    	return pageRef;
    }
}



 

All Answers

Ravi Dutt SharmaRavi Dutt Sharma
You should be using URL hacking instead of the approach you are trying.
Check this post: http://raydehler.com/cloud/clod/salesforce-url-hacking-to-prepopulate-fields-on-a-standard-page-layout.html
Sanchivan SivadasanSanchivan Sivadasan
I don't believe you can use URL hacking in lightning.
Sanchivan SivadasanSanchivan Sivadasan
I figured out what the issue was. The issue was I was not setting the region properly. I created a new variable called region within the constructor rather than setting the public variable. I modified the code quite a bit and you can check if you are in classic and if you are do what we normally do. I am putting the working code here so it will be helpful for someone else in the future.

Here is how my vf looks:
 
<apex:page standardController="Option__c" extensions="OptionExtension" action="{!pageDirect}">
    <apex:outputPanel rendered="{!!isClassic}">
        <script>
            var region = "{!region}";
        </script>
        <apex:outputPanel rendered="{!!isClassic}">
            <script src="/soap/ajax/15.0/connection.js"></script>
            <script>
                var defaultFieldValues = {
                    "Region__c":region
                };
                
                sforce.one.createRecord('Option__c', null, defaultFieldValues);               
            </script>
        </apex:outputPanel>
    </apex:outputPanel>
</apex:page>

Here is the extension class:
public class OptionExtension {
    public String region {get; set;}
    private String returnURL;
    
    public OptionExtension(ApexPages.StandardController controller) {
        returnURL = ApexPages.currentPage().getParameters().get('retURL');
		region = [Select Region__c From User Where Id = :UserInfo.getUserId()][0].Region__c;
        
        if(String.isEmpty(region))
            region = 'Eastern';
    }
    
    public Boolean getIsClassic(){
        return (UserInfo.getUiThemeDisplayed() =='Theme3' && ApexPages.currentPage().getParameters().get('beLightning') == null);
    }
    
    public PageReference pageDirect() {
        Boolean isClassic = getIsClassic();
        
    	PageReference pageRef = null;

    	if(isClassic || test.isRunningTest()) {
    		Schema.DescribeSObjectResult describeInfo = Option__c.sObjectType.getDescribe();
			String objectIdentifier = describeInfo.getKeyPrefix();

            pageRef = new PageReference('/' + objectIdentifier + '/e');
			// I am getting the field Ids from custom setting so this will work on sandboxes and in Production and no code change is required.
            Option_Fields_Ids__c customSetting = Option_Fields_Ids__c.getInstance();   

            String regionalIdentifier = customSetting.RegionalIdentifier_c__c;
            pageRef.getParameters().put(regionalIdentifier, region);
            pageRef.getParameters().put('cancelURL', returnURL);
			// nooverride is required, otherwise you will have an infinite loop
            pageRef.getParameters().put('nooverride', '1');

        	pageRef.setRedirect(true);
    	}
        
    	return pageRef;
    }
}



 
This was selected as the best answer