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
Andrew4Andrew4 

Pre-populating input fields on Visualforce page

Hi all,

I've been stuck on this for a while, so I'm finally asking for help - issue is similar to this one:
https://developer.salesforce.com/forums?id=906F000000094yFIAQ

I have a Visualforce page that uses the standard Event controller.  I overrode the "New Event" button to direct to this Visualforce page.  I'm having trouble pre-populating certain fields on that page.

For example, {!Event.whoID} is passed in the button URL and pre-populates the appropriate field by default.  I want to pass the User ID too and populate the Assigned To field, but I can't get it to work.  The default New Event page (which uses the same standard Event controller) populates the Assigned To field automatically, and without any additions in the URL... what am I missing?  Does my VF page need a controller extension?  If so, can anyone give pointers on what that needs to look like?

I assume I need to A) include whatever IDs I want populated in a custom button URL, and simultaneously B) build a controller extension that tells my VF page to pull the values out of the URL and load them in my input fields.  But all my attempts to implement this setup so far have failed.

Any pointers will be greatly, greatly appreciated.  Thanks for your help!



VF page snippet:
<apex:page standardController="Event" showHeader="true" sidebar="true">
    <apex:sectionHeader title="Log A Call"/>
    <apex:form id="pageForm">
        <apex:pageBlock title="Call Edit" mode="edit">
            <apex:pageBlockButtons location="both">
                <apex:commandButton value="Save" action="{!save}" id="saveButton"/>
                <apex:commandButton value="Cancel" action="{!cancel}" />
            </apex:pageBlockButtons>
            <br />
            <apex:pageBlockSection title="Owner" columns="2" collapsible="false">
                <apex:inputField value="{!Event.ownerID}" label="Who took the call?" style="width:200px;height:15px;" />
                <apex:inputField value="{!Event.StartDateTime}" label="Start" />
                <apex:inputField value="{!Event.Subject}" label="Subject" />
                <apex:inputField value="{!Event.EndDateTime}" label="End" />
                <apex:inputField label="Person" value="{!Event.whoID}" style="width:300px;height:20px;" />
                </apex:pageBlockSection>
                </apex:pageBlock>
    </apex:form>
</apex:page>

Current button URL (owner ID to be added):
https://c.cs8.visual.force.com/apex/NewLogCall?who_id=XXXX&retURL=%XXXX&sfdc.override=1


Assuming a controller extension that looks something like:
public class callExtension {
    public callExtension(ApexPages.StandardController stdController){
    public Event getEvent(){
        if(event == null) event = new Event();
        event.ownerid = ApexPages.currentPage().getParameters().get('ownerID');
        return event;
        }
    }
}


Thanks again!


Andrew

 
Best Answer chosen by Andrew4
Andrew4Andrew4
This is finally working - for any subsequent viewers, this is the final setup:

The custom button URL is as follows (OnClick JavaScript):
window.location = '/apex/NewLogCall?who_id={!Contact.Id}&ownerid={!$User.Id}&sfdc.override=1';


The controller extension is as follows:
public class callExtension2 {
public Event event{get; set;}
    public callExtension2(ApexPages.StandardController stdController){
       if(event == null) event = new Event();
       event.ownerid = ApexPages.currentPage().getParameters().get('ownerid');
       event.whoid = ApexPages.currentPage().getParameters().get('who_id');
    }
}


For subsequent fields, simply add a new line in the controller extension, and add the value of the field into the button URL.  Essentially the same solution as in the other thread on this topic, but this is code you can paste directly and get working.  Thanks for everyone's help!

All Answers

ManojjenaManojjena
Hi  Andrew,

Try with  below code .Who id relationship with event is either lead or Contact .I have given one example with contact .
If your botton is in related list of contact then it will work .

 
public class callExtension {
public Event evt{get;set;}
    public callExtension(ApexPages.StandardController stdController){
      if(evt == null) 
        evt = new Event();
       Id parentId=ApexPages.currentPage().getParameters().get('Id');
         if(parentId != null){
            Contact con=[SELECT Id,Ownerid FROM Contact Where Id=:ParentId LIMIT 1];
            evt.WhoId=con.Id;
            evt.ownerId=con.ownerId;
        }
    }
   
}

<apex:page standardController="Event" showHeader="true" sidebar="true" extensions="callExtension">
    <apex:sectionHeader title="Log A Call"/>
    <apex:form id="pageForm">
        <apex:pageBlock title="Call Edit" mode="edit">
            <apex:pageBlockButtons location="both">
                <apex:commandButton value="Save" action="{!save}" id="saveButton"/>
                <apex:commandButton value="Cancel" action="{!cancel}" />
            </apex:pageBlockButtons>
            <br />
            <apex:pageBlockSection title="Owner" columns="2" collapsible="false">
                <apex:inputField value="{!evt.ownerID}" label="Who took the call?" style="width:200px;height:15px;" />
                <apex:inputField value="{!evt.StartDateTime}" label="Start" />
                <apex:inputField value="{!evt.Subject}" label="Subject" />
                <apex:inputField value="{!evt.EndDateTime}" label="End" />
                <apex:inputField label="Person" value="{!evt.whoID}" style="width:300px;height:20px;" />
                </apex:pageBlockSection>
                </apex:pageBlock>
    </apex:form>
</apex:page>

Let me know if it helps .

Thanks 
Manoj
Andrew4Andrew4
Hi Manoj,

Thanks so much for your help.  I wrestled with your setup for a bit but I couldn't get it working - everything compiled but I simply couldn't get the values to populate.

I'm going to try out some of the techniques in this link and report what happens:
http://raydehler.com/cloud/clod/salesforce-url-hacking-to-prepopulate-fields-on-a-standard-page-layout.html

Thanks again.  I'll keep you posted.
ManojjenaManojjena
Hi Andrew ,

Could you please tell  me one thing .
Where is your button ,in contact / lead relatedlist ? 
If it is in contact related list then add one debug log to check the parentid varibale value .

If it is populating then above code should work .

 
Andrew4Andrew4
Hi Manoj,

Yes, the button is in the related lists, under Open Activities.  I'm a bit new to this so I'll need to determine how to use the debug logs to confirm the parent id, but I'll follow up with you when I do.

Thanks again for your feedback!  Much appreciated.
Andrew4Andrew4
This is finally working - for any subsequent viewers, this is the final setup:

The custom button URL is as follows (OnClick JavaScript):
window.location = '/apex/NewLogCall?who_id={!Contact.Id}&ownerid={!$User.Id}&sfdc.override=1';


The controller extension is as follows:
public class callExtension2 {
public Event event{get; set;}
    public callExtension2(ApexPages.StandardController stdController){
       if(event == null) event = new Event();
       event.ownerid = ApexPages.currentPage().getParameters().get('ownerid');
       event.whoid = ApexPages.currentPage().getParameters().get('who_id');
    }
}


For subsequent fields, simply add a new line in the controller extension, and add the value of the field into the button URL.  Essentially the same solution as in the other thread on this topic, but this is code you can paste directly and get working.  Thanks for everyone's help!
This was selected as the best answer
Charni Wiggins 1Charni Wiggins 1

I have been following this article to do a very similar thing but for a task! I also cannot get the input fields to populate - although I am trying to set the parent of a parent field value on the task (child) record. Also, we are using person accounts, so acc and con share fields - super confusing to me. So I am not sure what I should be setting in the whoid... I am also an Apex newbie so apologies for the mistakes! 

Extension:

public class riskupdateExtension {

    public Task tsk{get;set;}
    public riskupdateExtension(ApexPages.StandardController stdController) {
	
		if(tsk == null)
		tsk = new Task();

	Id parentId =ApexPages.currentPage().getParameters().get('Id');

        if(parentId != null) {
			
				Risk_Register__c rsk=[SELECT Risk_Register__c.Id, Risk_Register__c.Learner__r.Id FROM Risk_Register__c WHERE Id=:ParentId LIMIT 2];

				tsk.WhoId=rsk.Learner__r.Id;
				            
		}
	}
}

Button: 

window.location='/apex/Risk_Update?who_id={!Contact.Id}&ownerid={!$User.Id}&retURL={!Risk_Register__c.Id}&sfdc.override=1';


VF Page:​

<apex:page standardController="Task" extensions="riskupdateExtension">
  <style type="text/css">
    .inputclass {
        width: 445px;
    }
  </style>
  <apex:form id="LearnerRiskUpdate">
      <apex:pageblock title="Learner Risk Update" mode="edit">
        <apex:pageBlockSection title="Information" columns="2">
          <apex:inputField value="{!Task.whoid}"/>
          <apex:inputField value="{!Task.whatid}"/>
       </apex:pageBlockSection>
        <apex:pageBlockSection title="Learner Risk Details" columns="1">
          <apex:inputField styleClass="inputclass" value="{!Task.Learner_Update__c}"/>
          <apex:inputField styleClass="inputclass" value="{!Task.Learner_Update__c}"/>
        </apex:pageBlockSection>
        <apex:pageBlockButtons >
            <apex:commandButton value="Save Update" action="{!save}"/>
            <apex:commandButton value="Cancel" action="{!cancel}"/>
        </apex:pageBlockButtons>
      </apex:pageblock>
  </apex:form>
</apex:page>


When clicking the button I noticed that the url did not contain the whoid at all, where I thought it should? 

Any help would be super appreciated!! :)