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
EricReichertEricReichert 

InputField default value

I have the following VF page.

Code:
<apex:page id="AddServiceCallTask" standardController="Service_Call_Task__c" extensions="extServiceCallTask">
    <apex:form >
      <apex:pageBlock title="New Service Call Task" mode="edit">
            <apex:pageBlockButtons >
                <apex:commandButton value="Save" action="{!save}"/>
            </apex:pageBlockButtons>
    
            <apex:pageBlockSection title="Service Call Task Information">
                <apex:inputField ID="ServiceCallNumber" value="{!Service_Call_Task__c.Service_Call__c}">{!strSCN}</apex:inputField>
            </apex:pageBlockSection>
      </apex:pageBlock>
    </apex:form>
</apex:page>

 And the following extension

Code:
public class extServiceCallTask {

    public string strSCN {get;set;}

    public extServiceCallTask(ApexPages.StandardController controller) {
        PageReference pageRef = ApexPages.currentPage();
        Map<String, String> pageParameters = pageRef.getParameters();
        strSCN = pageParameters.get('SCN');
    }
}

The extension outputs the parameter ( {!strSCN} ) underneath the inputfield (under the part where a value would be typed)  instead of setting it as the default value for the field.  The inputField references a lookup field.

There are several places where I need to use this functionality.  What am I doing wrong?






jwetzlerjwetzler
You can't nest the value you want inside of an inputField.  What you want your inputField to show has to be the value attribute.
Code:
public class extServiceCallTask {

    public extServiceCallTask(ApexPages.StandardController controller) {
        PageReference pageRef = ApexPages.currentPage();
        Map<String, String> pageParameters = pageRef.getParameters();
        strSCN = pageParameters.get('SCN');

Service_Call_Task__c sct = (Service_Call_Task__c) controller.getRecord();
sct.Service_Call__c = strSCN;
 } }

 
Code:
<apex:pageBlockSection title="Service Call Task Information">
  <apex:inputField ID="ServiceCallNumber" value="{!Service_Call_Task__c.Service_Call__c}"/>
</apex:pageBlockSection>

 You want to set the field on the record and then use inputField to display the field.


EricReichertEricReichert
Thanks for getting back to me so quickly.  I'm having an awful time finding answers and the VF Developers Guide only gets a person so far.

Okay.  This makes sense...I think.  This leads to another question I've been trying to find the answer to.

The new record is created in Apex and allows me to fill the lookup field on the page with a default value.  The user fills in the rest of the fields (there are more fields I haven't added to the page) for the new record.  In the controller extension how do I grab the other values from the filled in form so that I can assign those values to the record and then commit it to the database?  Is all of that handled automatically when the save button is clicked?


EricReichertEricReichert
I'm getting the following error when I attempt to use your recommendation.

System.StringException: Invalid id: SCN-0000014

Class.extServiceCallTask.<init>: line 23, column 31
External entry point
jwetzlerjwetzler
To answer your first question, since you are setting the field directly on the record, you do not need to do anything special on Save.  The standard controller takes care of all of that for you.

What is at line 23 in your controller?

Sounds like you are passing in an incorrect id.  If I had to guess you're querying for something and you gave it SCN-0000014 as an ID instead of the 15 char record id it's expecting.
EricReichertEricReichert
I got it working thanks to your last question.  I was passing a field value in the link parameters instead of the record ID.

This next question is a little off topic.

How did you learn the platform?  I'm a very experienced, "traditional" programmer but I'm having a heck of a time using this platform.  Finding answers to what seem like trivial questions, like this one, has been maddening. 

I've read the guides and watched some of the presentations but it doesn't seem like enough.

Any suggestions?

Thanks again for the help.  Now maybe I can move on to actually building my application.
jwetzlerjwetzler
I learned the platform by helping to write it :)  I have an advantage, if I have a question I can walk down the hall and ask, or dig through the source code.

What I always suggest is to start at the very beginning of the dev guide and do the examples, really understand why they work.  Make small mods to them to see how you can improve them or make them a little more complex.  Look through the component reference and all of the examples there.  It will give very basic examples of how our components work and how they interact.  Our demos get posted, so it's helpful to work through the source for those, too.

It is not unusual to see hardcore traditional programmers have a little trouble getting started.  We've tried to simplify a lot of the things that would normally be complex and/or javascript-heavy, like partial page updates and interaction with the data model.  A lot of times there's an easy solution to a problem that sounds like it will be difficult.  And a lot of people who are used to Scontrols or even plain old web development want to immediately drop into Javascript when they don't have to.

The boards are good but are admittedly a little hard to search through (I have answered this particular question before, within the past month).

Since Visualforce is still taking off, we are using the boards as a way to determine what people are getting stuck on most often.  We have made a lot of changes to the doc based on the threads that we see come up.  If you have any suggestions for us feel free to post them here, or if you have an idea you can use the ideas site.
EricReichertEricReichert
Obviously, I'm not the most observant person because I didn't even see the signature at the bottom.  I was just reading the profile to the left of your posts.

Thanks again.  Next time I won't hesitate to post a question.
avlerikavlerik

Jill, you seem to be one of the most knowledgable persons here, so please don't laugh if my question sounds stupid. I don't do much coding and try to handle as much as I can directly in VF w/o creating extra controllers or extensions.

 

My problem: as I see it, the "value" attribute on inputField is used for data binding. But how do I get it to accept a default value for a new record? I was thinking of something like:

 

<apex:inputField value="{!Opportunity.OwnerId}" default="{!$User.Id}" /> 

or

<apex:inputField value="{!Opportunity.OwnerId:$User.Id}" /> 

 

Of course, both are made-up and I don't have the foggiest idea how I can get the user's name into the Owner field, because, unlike elsewhere in SFDC, $User doesn't seem to be valid in VF and because I cannot figure out how to use "value" to bind to a field AND provide a default value.

 

Any help, especially if it includes a workable example, is very much appreciated.

 

Thx,

Erik

jm@idcjm@idc
I had a similar problem and wasn't sure how to create the controller extension so I used inputfields on the VF form for user required data and then set the other values with a workflow.  I didn't need the users to see the other fields or modify them so it worked out well.