Take the 2-minute tour ×
Salesforce Stack Exchange is a question and answer site for Salesforce administrators, implementation experts, developers and anybody in-between. It's 100% free, no registration required.

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".

share|improve this question
    
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? –  sfdc_ninja Feb 20 at 14:51
add comment

3 Answers

up vote 3 down vote accepted

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

share|improve this answer
2  
Make sure you use the JSENCODE around your parameters. Otherwise you are opening security loopholes. –  dphil Feb 20 at 15:43
    
Thank you very much Melmoussaoui. It works like a charm. –  snstulemeijer Feb 21 at 8:24
add comment

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>
share|improve this answer
add comment

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');
    }
}
share|improve this answer
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.