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
Renier F NavasRenier F Navas 

Auto populate a lookup field in custom object with current user

Hello - I have tried few solutions ( trigger, process builder, lookup helper) based on what I have found online but no luck so far :/, so any help is appreciated. 

We have a custom object to enter  Incidents: c4g_Incident_Report__c
This object has a lookup field "Reporting Staff" that looks up in the "user" object: c4g_Reporting_Staff__c

Looking for a solution that will auto-populate, before insert, the lookup "Reporting Staff" field with the name of the current user creating the entry. 

User-added image
User-added image
Abhishek BansalAbhishek Bansal

Hi Renier,

If you are trying to achieve this with the standard "New" page of salesforce then it is not possible to auto populate the value in a field.
This can be achieved if you create your own Custom VF page and then override this custom VF page on the New button of your object.

Please let me know if you need more information on this.

Abhishek Bansal

Renier F NavasRenier F Navas
Abhishek thanks for your reply. I am new to salesforce and it's hard to know what can be done or not when you are trying to figure out a solution. 

Abhishek BansalAbhishek Bansal
Hi Renier,

There is only one way by which you can acheive this requirement. You have to follow the below mentioned steps in order to get what you want :

1. Create a new custom VF page with standard controller as your currenct object i.e. "c4g_Incident_Report__c".
2. In the extension of this VF page populate the value in the field "c4g_Reporting_Staff__c".
3. On your VF page show input fields as per your requirement.
4. Now override this VF page on the "New" button of your "c4g_Incident_Report__c" object.

Yur VF page and controller class will look something like below :

VF page :
<apex:page standardController="c4g_Incident_Report__c" extensions="pageController">
	<apex:form >
        <apex:pageMessages ></apex:pageMessages>
        <apex:pageBlock title="Cost Projection">
            <apex:pageBlockButtons >
                <apex:commandButton action="{!save}" value="Save"/>
                <apex:commandButton action="{!cancel}" value="Cancel"/>
            <apex:pageBlockSection title="Information" columns="2" collapsible="false">
                <apex:inputField value="{!c4g_Incident_Report__c.c4g_Reporting_Staff__c}"/>

Controller Class :
public with sharing class pageController {
    public c4g_Incident_Report__c incidentReport{get;set;}
	public pageController(ApexPages.StandardController stdController) {
		incidentReport = new c4g_Incident_Report__c();
		incidentReport = (c4g_Incident_Report__c)stdController.getRecord();
		//incidentReport.c4g_Reporting_Staff__c = //Populate value as per your requirement

In order to override the standard New button with your VF page please foolow the below steps :
1. Click on Setup
2. In search box at Left side enter "Objects".
3. Click on Objects.
4. Click on "c4g_Incident_Report__c"
5. Scroll down to Custom Buttons and Links section
6. Click on "Edit" link in front of New Button
7. In the "Override With" section Select "Visualforce Page" radio button
8. In the drop down of Visualforce page select the page that you have created now.

Please let me know if you still need any help or information on this.

Abhishek Bansal
Dinesh Suggala 8Dinesh Suggala 8
Hi Renier,

Are you creating "New" incidents from the related list(c4g_Incident_Report__c) "New" button .
IF it is yes then you can remove Standard "New" button and replace with custom button "New" and do URL hacking.Create a custom button as described below:

Thanks & Regards,
Renier NavasRenier Navas
Hey guys thanks! I will try this and will get back to you..