3

Prefilling fields is easy on standard edit pages via an URL parameter, like .../a0B/e?Name=MyNameValue Doing this on a visualforce page does not work without custom controllers, or is there a way?

Problem description:

URL .../apex/MyVFPage?Name=MyNameValue

<apex:inputField value="{!MyCustomObject__c.Name}" id="Name">

Name in URL does not populate field with id="Name".

1
  • If you actually inspect the element on the page of your VF page, that input field doesn't have an id of 'Name'. It has some Id similar to "j_ido:jid1_:name". This is how the VF is converted to html. Is there a reason you don't want to use a standardController and add an extension? Feb 20, 2014 at 14:51

4 Answers 4

6

Sorry I misunderstood you.

here is a the right solution for your problem

<apex:page standardController="Lead">
    <apex:form>
        <apex:inputtext  id="jsName"/>
        <script>
            var jsName = "{!$Component.jsName}";
        </script>

    </apex:form>

    <script>
        window.onload=function()
        {

         document.getElementById(jsName).value = "{!$CurrentPage.parameters.Name}";
        };
    </script>
</apex:page>

And don't forget that you can't use the inputField, because it needs to be bond to an existing sObject Field

2
  • 3
    Make sure you use the JSENCODE around your parameters. Otherwise you are opening security loopholes.
    – dphil
    Feb 20, 2014 at 15:43
  • Thank you very much Melmoussaoui. It works like a charm. Feb 21, 2014 at 8:24
2

You will need at least a standardController specified on your visualforce page as apex:inputfield needs to bind to a SObject. It can be the standard controller for you custom object. As for prefilling the form, you will need to use some JavaScript.

<apex:page standardController="MyCustomObject__c" id="mainPage">
    <apex:form id="mainForm">
        <apex:inputField value="{!MyCustomObject__c.Type__c}" id="typeField"/>
    </apex:form>

    <script>
        window.onload = function() {
            document.getElementById('mainPage:mainForm:typeField').value = "{!$CurrentPage.parameters.type}";
        };
    </script>
</apex:page>
1
  • This is the best Answer. It can handle inputField binding, and the JS is way simple. Not sure why so few votes up votes. Both great answers though, 2 different approaches. This works so so simply. Important point is getElementId needs to be quite accurate, but then your parameter can be whatever you want. I thought I was going to have to write a ton of JS, this was a life saver Apr 28, 2017 at 6:44
0

You can get the parameters using an extension:

public class MyExtension {

    private MyCustomObject__c record;
    public MyExtension(ApexPages.StandardController controller) {
        record = (MyCustomObject__c) controller.getRecord();
        record.Name = ApexPages.currentPage().getParameters().get('Name');
    }
}
0
0

Expanding on @user6941, i am providing back my code. Not sure how to put code in a comment.

The job is a lookup field, the type is a picklist

    <apex:page standardController="object__c" id="p" >
<apex:form id="f">
<apex:pageblock title="Time Entry" id="pb">
    <apex:pageblockButtons >
        <apex:commandButton action="{!save}" value="Save"/>
        <apex:commandButton action="{!cancel}" value="Cancel"/>
    </apex:pageblockButtons>
    <apex:pageBlockSection Title="PO Detail" id="pbs">
        <apex:inputField label="Type" value="{!object__c.Type__c}" id="type"/>
        <apex:inputField label="Category" value="{!object__c.Category__c}" id="category"/>
        <apex:inputField label="Job" value="{!object__c.Job__c}" id="job"/>
    </apex:pageBlockSection>
</apex:pageblock>
</apex:form>
    <script>
        window.onload = function() {
            document.getElementById('p:f:pb:pbs:type').value     = "{!$CurrentPage.parameters.type}";
            document.getElementById('p:f:pb:pbs:category').value = "{!$CurrentPage.parameters.category}";
            document.getElementById('p:f:pb:pbs:job').value      = "{!$CurrentPage.parameters.job}";
            document.getElementById('p:f:pb:pbs:job_lkid').value = "{!$CurrentPage.parameters.jobid}";
        };
    </script>

</apex:page>

Here is my URL:

/apex/TimeEdit?jobid=a000sfid000000t&category=xyz&job=Job+Name&type=Type+Value

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .