• Yuval Vardi 1
  • 0 Points
  • Member since 2014

  • Chatter
  • 0
    Best Answers
  • 0
    Likes Received
  • 0
    Likes Given
  • 0
  • 1

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:
<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:pageBlockSection >
   <apex:pageBlockSectionItem >
    <apex:outputLabel value="Checklist Template" />
    <apex:inputField value="{!Checklist__c.Checklist_Template__c}" required="true" /> 
<!-- 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];

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.

  • October 13, 2008
  • Like
  • 0