[SalesForce] Auto-populate fields on custom visualforce page with information from standard page

I'm having a bit of an issue where I can't figure out how to auto-populate some fields on a custom VF page that I made. The page is linked from a button that is found on an Orders object and should ideally auto-populate its fields using some of the fields on the Orders record that its linked from. I've already tried using URL hacking on the custom button, but I haven't had any luck. Here's my VF page:

<apex:page standardController="Payee__c">
  <apex:form >
    <apex:pageBlock title="Add Additional Payee Information" mode="save">
      <apex:pageBlockButtons >
        <apex:commandButton action="{!save}" value="Save"/>
        <apex:commandButton action="{!cancel}" value="Cancel"/>
      </apex:pageBlockButtons>
      <apex:pageBlockSection title="Additional Payee" columns="1">
        <apex:inputField value="{!Payee__c.Order__c}"/>
        <apex:inputField value="{!Payee__c.Payee_Type__c}"/>
        <apex:inputField value="{!Payee__c.Agency__c}"/>
        <apex:inputField value="{!Payee__c.Agent__c}"/>
        <apex:inputField value="{!Payee__c.Additional_Agency_Rate__c}"/>
        <apex:inputField value="{!Payee__c.Rate_Based_On__c}"/>
      </apex:pageBlockSection>
    </apex:pageBlock>
  </apex:form>
</apex:page>

So the "Order", "Agency", and "Agent" fields are all lookups. The "Payee Type" and "Rate Based On" fields are picklists. And the "Additional Agency Rate" is a standard text input field. Again, I've already tried using URL hacking, just trying to populate the "Order" field to try to test it out but I haven't had any luck with that. I'm going to try to write an extension for the VF page, so I'll let you know how that goes. If anyone knows how to handle this issue, it would be greatly appreciated! Thanks!

Best Answer

Alright, I finished the extension and it works. Thanks to Jason Hardy for suggesting the extension! Here it is:

    public class CreateAdditionalPayeeExtension {
    Payee__c payee {get;set;}

    //constructor
    public CreateAdditionalPayeeExtension(ApexPages.StandardController stdController){
        payee = (Payee__c)stdController.getRecord();
        payee.Order__c = ApexPages.currentPage().getParameters().get('ordId');
    }

    public PageReference save(){
        insert payee;
        return null;
    }
}

(You can ignore the save function in the extension; I'm just trying to now make a save button that overrides the standard one on my VF page so that it can redirect to a specific page.) So after I made this, I used it to auto-fill the "Order" lookup field on the VF page (I decided to leave the rest blank). Here's the VF page:

    <apex:page standardController="Payee__c" extensions="CreateAdditionalPayeeExtension">
  <apex:form >
    <apex:pageBlock title="Add Additional Payee Information" mode="save">
      <apex:pageBlockButtons >
        <apex:commandButton action="{!save}" value="Save"/>
        <apex:commandButton action="{!cancel}" value="Cancel"/>
      </apex:pageBlockButtons>
      <apex:pageBlockSection title="Additional Payee" columns="1">
        <apex:inputField value="{!Payee__c.Order__c}"/>
        <apex:inputField value="{!Payee__c.Payee_Type__c}"/>
        <apex:inputField value="{!Payee__c.Agency__c}"/>
        <apex:inputField value="{!Payee__c.Agent__c}"/>
        <apex:inputField value="{!Payee__c.Additional_Agency_Rate__c}"/>
        <apex:inputField value="{!Payee__c.Rate_Based_On__c}"/>
      </apex:pageBlockSection>
    </apex:pageBlock>
  </apex:form>
</apex:page>

So, as you can see, nothing changed on the VF page from before besides the extension at the top, BUT along with the button URL that is on the Order object that links to the page (/apex/Create_Additional_Payee?ordId={!Order.Id}), the extension works to override the

            <apex:inputField value="{!Payee__c.Order__c}"/>

line and when the button is clicked on the Order object, the lookup field is successfully pre-populated, but still able to be changed to something else if the user so desires.

Sorry for the long and convoluted explanation, but that's pretty much the order in which I figured it out so I don't know a better way to explain it. I hope this helps anyone that has the same problem!

Related Topic