You need to sign in to do that
Don't have an account?
JoeyD
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
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)
2) In your VF page, bind the 'record' variable to the inputFields that accept the value for the Opportunity and Account
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:
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
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)
2) In your VF page, bind the 'record' variable to the inputFields that accept the value for the Opportunity and Account
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:
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...
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!