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
saracsarac 

Pre-populating field on Visualforce Page from url

Hello,

I have been trying to get a field on a Visualforce page (the Account ID - a custom field which is a standard lookup) to pre-populate from a query parameter in the url. It does *not* work.

The general use case is that we have a related list on the Account Page for a custom object called Checklist. When we use the standard New button to create a new record, the Account ID is pre-populated on the page since the ID and name are passed in as query parameters.

We had to create a small Visualforce page instead of using the standard page, and overrode the New button. Now when you select New, the page loads without the Account ID pre-populated even though the ID and name are still in the url as query parameters. I took some JavaScript code to pull out those values and have been trying for the past day to figure out how to pass them to the standard controller so it recognizes the Account. The point here is that the end user should not have to re-select the Account from a lookup field when they just came from the Account page.

Since you can't simply View Source on a Visualforce page to see what the field name "{!Checklist__c.Checklist_Template__c}" resolves to (it's just escaped yet not interpreted at that point) I was forced to use a program to view the post request. It would appear that the <apex:inputfield> tag when referring to a lookup actually generates 6 input fields for it.

So... I used those 6 fields, populated them exactly as the lookup field would populate them, and still no luck.
Here is the code:
Code:
<apex:page standardcontroller="Checklist__c" tabstyle="Checklist__c">

  <apex:sectionHeader title="Checklist" subtitle="Create Checklist"/>
  <apex:form id="form1">
    <apex:pageBlock >
  <apex:pageBlockButtons location="top" >
   <apex:commandButton action="{!save}" onclick=" gup();" value="Save" />
  </apex:pageBlockButtons>
  <apex:pageBlockSection >
   <apex:pageBlockSectionItem >
    <apex:outputLabel value="Checklist Template" />
    <apex:inputField value="{!Checklist__c.Checklist_Template__c}" required="true" /> 
   </apex:pageBlockSectionItem>
     </apex:pageBlockSection> 
    </apex:pageBlock>
  
<!-- Here are the 6 fields that are generated by the lookup field -->
  <input type="text" id="AccountID_lkid" name="j_id0:form1:j_id2:j_id5:j_id6:AccountID_lkid"  />
  <input type="text" id="AccountID_lkold" name="j_id0:form1:j_id2:j_id5:j_id6:AccountID_lkold"  />
  <input type="text"  name="j_id0:form1:j_id2:j_id5:j_id6:AccountID_lspf" value="0"  />
  <input type="text"  name="j_id0:form1:j_id2:j_id5:j_id6:AccountID_mod" value="0"  />
  <input type="text"  name="j_id0:form1:j_id2:j_id5:j_id6:AccountID_lktp" value="001"  />
  <input type="text" id="AccountID" name="j_id0:form1:j_id2:j_id5:j_id6:AccountID"  />
  
<!-- Here is the code to pull the Account ID and Name out of the url. This definitely works -->
<script type="text/javascript">

   var regexS = "[\\—&]CF00NS0000000M2Je_lkid=([^&#]*)";
   var regex = new RegExp( regexS );
   var results = regex.exec( window.location.href );
   document.getElementById('AccountID_lkid').value = results[1];
 
 var regexS2 = "[\\–&]CF00NS0000000M2Je=([^&#]*)";
   var regex2 = new RegExp( regexS2 );
   var results2 = regex2.exec( window.location.href );
   document.getElementById('AccountID').value = results2[1];
   document.getElementById('AccountID_lkold').value = results2[1];
  </script>
  </apex:form>
</apex:page>

 
Now I realize I can override the Standard controller and just grab the ID out of the request and it would work just fine, but I really want to understand why this does not work with just the standard controller. This is a pretty big loss of functionality when the page used to pre-populate with the Account ID and then when you write a Visualforce page it no longer does.

Thanks,
Sara
Best Answer chosen by Admin (Salesforce Developers) 
kevinedelmannkevinedelmann

For all who end up on this page after me.  Here is what I found works.

 

If you include some parameter calls as part of the get object you will be able to pass information into the input fields.

 

Here is an example of what I did:

 

 public Opportunity getOpportunity() {
      if(opportunity == null) opportunity = new Opportunity();
      opportunity.recordtypeid = ApexPages.currentPage().getParameters().get('RecordType');
      opportunity.AccountID = ApexPages.currentPage().getParameters().get('accid');
      opportunity.Name = ApexPages.currentPage().getParameters().get('oppName');
      opportunity.StageName = ApexPages.currentPage().getParameters().get('Stage');
      return opportunity;
   }

I then am able to pass values using the url. i.e. www.salelsforce.com/apex/PAGENAMEHere?accid=XXXXXX&RecordType=YYYYYYYY

 

I hope this helps everyone.


Kevin

All Answers

JMPlayer22JMPlayer22

I need to be able to do this same thing, except I need to be able to pre-populate the fields the same way I can with a URL button...where I select the field label and find the "label for" by viewing the selection source and then pasting it in to the URL button like so: 

/a0K/e?CF00N80000003vlpp=test

 

The "CF00N80000003vlpp" is the "label for" on the field I need to populate and "test" will successfully populate, BUT this doesn't work for visualforce pages...the label for for the exact same field is for some reason set to:

j_id0:j_id2:j_id5:j_id12:j_id13

 

using the above "label for" just doesn't work...

 

Message Edited by JMPlayer22 on 08-24-2009 11:12 AM
Big EarsBig Ears
I'm having the exact same issue.
kevinedelmannkevinedelmann

Did anyone get an answe as to how to do this?

 

Thanks,


Kevin

Big EarsBig Ears

My solution ended up using a custom controller extension that supported the new Visualforce page:

 

1. Create a new custom related list button that passes in the Account ID from the account page (using a URL along the lines of "/apex/MyVisualforcePage?id={!Account.Id}"

2.The controller extension, as part of initialising the record, assigns the required values to the required fields. The information for the fields can be gotten from a SOQL query of the Account ID.

 

It's not faster than the old s-control way of doing things, but it does feel more stable and versatile (and you don't have to worry about escaping punctuation marks, etc)

 

Let me know if you need more details.

 

With thanks,

Andy

kevinedelmannkevinedelmann

For all who end up on this page after me.  Here is what I found works.

 

If you include some parameter calls as part of the get object you will be able to pass information into the input fields.

 

Here is an example of what I did:

 

 public Opportunity getOpportunity() {
      if(opportunity == null) opportunity = new Opportunity();
      opportunity.recordtypeid = ApexPages.currentPage().getParameters().get('RecordType');
      opportunity.AccountID = ApexPages.currentPage().getParameters().get('accid');
      opportunity.Name = ApexPages.currentPage().getParameters().get('oppName');
      opportunity.StageName = ApexPages.currentPage().getParameters().get('Stage');
      return opportunity;
   }

I then am able to pass values using the url. i.e. www.salelsforce.com/apex/PAGENAMEHere?accid=XXXXXX&RecordType=YYYYYYYY

 

I hope this helps everyone.


Kevin

This was selected as the best answer
ethanoneethanone

That makes sense although I'm a little confused on implementation in my case.  I'm trying to create a new case record from another application (not a SFDC app).  Since I'm making a new record, I can't modify an existing record and I can't make a new one in code because some required fields need user input.

 

Essentially, I'm trying to create a new case record when someone clicks a google map application and capture the latitude and longitude passed in the url.  So given a url that ends like /apex/CustomCaseEditPage?lat=XXX&lng=YYY, how can I pre-populate my form inputFields case.latitude__c and case.longitude__c fields respectively?

 

Thanks for your help.

Kevin042Kevin042

Just create a full URL (na#.salesforce.com/apex/CustomCaseEditPage?lat=XXX&lng=YYYand populate the Lat/Log where you have XXX & YYY and put the instance number where the # is.

NK123NK123

Hi ethan,

 

I have the same problem. In my case I have the following relationship:

 

Account->Opportunity--> Custom Quote --> Custom QuoteLineItem --> Custom QuoteChargeItem which points to (Custom Quote) as Parent key and have a lookup relationship field for (Custom QuoteLineItem).

 

So when I goto the Custom QuoteLineItem screen and click on "new Custom QuoteChargeItem" from related list, only the Custom QuoteLineItem field is filled-in and the Custom Quote field is always blank.

 

I tried passing the Parameters for both these fields like shown below and still fails to fill in the custom quote field(last two parameters) where as custom quotelineitem works which is (first two parameters).

 

/a0Q/e?CF00NT0000001CC2c={!HTMLENCODE( PashaQuoteLineItem__c.Name)}&CF00NT0000001CC2c_lkid={!PashaQuoteLineItem__c.Id}&retURL=%{!PashaQuoteLineItem__c.Id}&CF00NT0000001CByv=xxx&CF00NT0000001CByv_lkid=a0LT0000001YkM8

 

~NK

 

Yuval Vardi 1Yuval Vardi 1
Great Ideas!! encountered with the same issue..I found my self using Visual Workflows...

Embeding flows on visualforce page and add it to layout..
Makes cool similar results..

Just assign the variables and you set to go!