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

Add object with prepopulated fields


I want to add a new object from a commandbutton on a VF page, where some fields are already filled in as the name/id of the employee.

I use the following:


<apex:commandButton value="Leaverequest" 
action="{!urlFor($Action.Leave__c.New, null, 
[,,retURL='/apex/'+$CurrentPage.Name])}" /> 



the id CF00NA0000003nF3I is the system id for my custom field which I want to pre-populate. This works fine, but if I deploy this in another salesforce org, the field-ID is different so it will no longer work. Just putting the fieldname in doesn't work, although that sometimes works for standard fields.

Any ideas how to get the field ID dynamically (eg in the controller)? I cannot find a getdescribe kind of solution for this.



I think you need to take a different approach here and use a custom controller or controller extension to get and set the information you need.


Here's a good starting point Buiiding Custom Controllers


Thanks for your reply. I could indeed make the new page a VF page, but I prefer to use a standard layout, so that the page can be modified and enhanced with the screen painter and not only with visualforce coding. the only specific thing I want is to fill some default values.


If you use <apex:detail title="false" relatedList="false" /> in the VF ppage this will respect your Page Layout design and User Profiles then use <apex:relatedList list="myObject__r"/> for the related lists.


To include all related lists setup on your layout use <apex:detail title="false" relatedList="true" />


Then you can use a controller to do what you described. Also Spring '11 has a new feature called Field Sets which I'm looking forward to using.


Oh one other option if you don't want to use VF pages. Try using some javascript on the button, Create - Objects - 'Your Object' Custom Buttons. Search the help for custom buttons. Haven't used this myself

!GETRECORDIDS  (seems to be the key)