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
JoeyDJoeyD 

Controller Extension - populate Look-up Field before insert

Hi all,

 

I have a custom extension I am using with a VF page that replaces the "new" button of the object. The object is a child of Opportunities and, and the records are created from an Opportunity related list.  I have the Opportunity field on the object auto-populating, but I would like an Account look-up field to auto populate as well with the Account related to the Opportunity, as I would like a related list of the custom object on Account records. 

 

Is there a way to do it in the extension, or will I have to use a before insert trigger?

 

Thanks

Best Answer chosen by Admin (Salesforce Developers) 
forecast_is_cloudyforecast_is_cloudy

In your case I would recommend that you set the Account lookup field in the background (via a before insert trigger as you had suggested) so that the Account field always reflects the correct value. i.e. You don't want to allow users to select the Account lookup value to be anything other than Opportunity's parent Account.

For some reason, if you do infact want users to be able to select the Account value on the custom object and you just want to pre-populate that field with the correct value when the VF page first displays, here's how:

 

1) Use the following in your custom extension class (assuming 'My_Custom_Obj__c' is the custom junction object between Account and Opportunity)

 

public with sharing class TestPage2Ext 
{
My_Custom_Obj__c record {get;set;}

public TestPage2Ext(ApexPages.StandardController stdController)
{
record = (My_Custom_Obj__c) stdController.getRecord();
record.account__c = ApexPages.currentPage().getParameters().get('acctId');
record.opportunity__c = ApexPages.currentPage().getParameters().get('oppId');

}

public PageReference save()
{
insert record;
return null;
}
}

2) In your VF page, bind the 'record' variable to the inputFields that accept the value for the Opportunity and Account

 

<apex:inputField value="{!record.Opportunity__c}"/>
<apex:inputField value="{!record.Account__c}"/>

 3) Add a new custom 'List Button' on the 'My_Custom_Obj__c' custom object. Set the following for the URL of that custom button (assuming that 'TestPage2' is your custom VF page:

 

/apex/TestPage2?acctId={!Opportunity.AccountId}&oppId={!Opportunity.Id}

 4) In the Page Layout for Opportunity, edit the related list for the 'My_Custom_Obj__c' custom object. Remove the standard 'New' button and instead add the button you created above to the related list.

 

 

Now when you users click on this custom button from the related list on an Opportunity record, they'll be taken to a custom VF page with the Opportunity and Account fields pre-populated.

Hope this helps...

 

All Answers

forecast_is_cloudyforecast_is_cloudy

In your case I would recommend that you set the Account lookup field in the background (via a before insert trigger as you had suggested) so that the Account field always reflects the correct value. i.e. You don't want to allow users to select the Account lookup value to be anything other than Opportunity's parent Account.

For some reason, if you do infact want users to be able to select the Account value on the custom object and you just want to pre-populate that field with the correct value when the VF page first displays, here's how:

 

1) Use the following in your custom extension class (assuming 'My_Custom_Obj__c' is the custom junction object between Account and Opportunity)

 

public with sharing class TestPage2Ext 
{
My_Custom_Obj__c record {get;set;}

public TestPage2Ext(ApexPages.StandardController stdController)
{
record = (My_Custom_Obj__c) stdController.getRecord();
record.account__c = ApexPages.currentPage().getParameters().get('acctId');
record.opportunity__c = ApexPages.currentPage().getParameters().get('oppId');

}

public PageReference save()
{
insert record;
return null;
}
}

2) In your VF page, bind the 'record' variable to the inputFields that accept the value for the Opportunity and Account

 

<apex:inputField value="{!record.Opportunity__c}"/>
<apex:inputField value="{!record.Account__c}"/>

 3) Add a new custom 'List Button' on the 'My_Custom_Obj__c' custom object. Set the following for the URL of that custom button (assuming that 'TestPage2' is your custom VF page:

 

/apex/TestPage2?acctId={!Opportunity.AccountId}&oppId={!Opportunity.Id}

 4) In the Page Layout for Opportunity, edit the related list for the 'My_Custom_Obj__c' custom object. Remove the standard 'New' button and instead add the button you created above to the related list.

 

 

Now when you users click on this custom button from the related list on an Opportunity record, they'll be taken to a custom VF page with the Opportunity and Account fields pre-populated.

Hope this helps...

 

This was selected as the best answer
JoeyDJoeyD

Thank you so much for the detailed reply. This worked great. I'm actually only displaying the field on the detail page, making it an uneditable field, so I was able to add those two lines to my controller and create that custom button and it works perfectly.

 

Thanks again!