Difference between revisions of "Referencing Form Fields in JavaScript"
imported>Aeric |
imported>Aeric |
||
Line 42: | Line 42: | ||
<pre>setTextFieldValue(_sdForm, āfirst_nameā, āAdamā); | <pre>setTextFieldValue(_sdForm, āfirst_nameā, āAdamā); | ||
setPickListValue(_sdForm, āfirst_nameā, āAdamā); | setPickListValue(_sdForm, āfirst_nameā, āAdamā); | ||
setMultiPickListValue(_sdForm, āfirst_nameā, [ānewā,āclosedā]); // array of | setMultiPickListValue(_sdForm, āfirst_nameā, [ānewā,āclosedā]); Ā | ||
Ā Ā // value is an array of strings | |||
setRadioButtonValue(_sdForm, āfirst_nameā, āAdamā); | setRadioButtonValue(_sdForm, āfirst_nameā, āAdamā); | ||
setCheckboxState((_sdForm, āfirst_nameā, true);Ā // checked | setCheckboxState((_sdForm, āfirst_nameā, true);Ā // checked | ||
setCheckboxState((_sdForm, āfirst_nameā, false); // unchecked | setCheckboxState((_sdForm, āfirst_nameā, false); // unchecked | ||
setLookupValue(_sdForm, "project_number", "123456", "My Project"); | |||
Ā Ā // 123456 is the record's internal record ID | |||
Ā Ā // "My Project" is the value of the record locator field(s) for that record | |||
</pre> | |||
|} | |||
:''Learn more:'' [[Record Locator]] | |||
;Example - Update the field value of a text field: | |||
To update the field value of a text field <tt>first_name</tt>, use the following syntax: | |||
:<tt>setTextFieldValue(_sdForm,āfirst_nameā,āAdamā)</tt> | |||
;Example - Update the field value of a Lookup field: | |||
To update the field value of a [[Lookup]] field, two parameters are required: | |||
:;RecordID:<tt>lookup.value</tt> | |||
::This is the [[Record Id]] of the record in the object | |||
:;Field value:<tt>lookup_name.value</tt> | |||
::This is the value of the record in the object | |||
To update the field value of a lookup field (<tt>project_number</tt>), use the following syntax: | |||
:{| | |||
<pre> | |||
form.project_number.value = "123456"; | |||
// 123456 is the internal record identifier of the record present in lookup object | |||
form.project_number_name.value = "My Project"; | |||
// My project is the value present in the record locator field(s) of the lookup object | |||
</pre> | </pre> | ||
|} | |} |
Revision as of 01:36, 27 June 2013
- Accessing fields
In the HTML document object model (DOM), the current form is named _sdForm.
This line of code gets a reference to it and puts it in the form variable:
var form = document._sdForm;
To find the name of a field in the platform:
- Go to > Customization > Objects > {object} > Fields
- Find the label of the field you're interested in
- Get its name from the Field Name column
That name can then be used in the JavaScript code.
Fields of type TextField, TextArea, Date, DateTime, Time, and URL can be accessed using _sdForm[0]. For example, this line of code gets a reference to the first_name field, and puts it in the variable elem:
var elem = _sdForm[0].first_name;
Fields have two properties that you can access in JavaScript:
- name: the name of the field
- value: the value of the field
So this line retrieves the value of the first_name field:
var fn = elem.value;
As does this longer version:
var fn = _sdForm[0].first_name.value;
- Updating fields
The method used to update a field depends on the field type. All of the methods have the general form:
set...Value(_sdForm, field, value);
Here are examples of the different methods:
setTextFieldValue(_sdForm, āfirst_nameā, āAdamā); setPickListValue(_sdForm, āfirst_nameā, āAdamā); setMultiPickListValue(_sdForm, āfirst_nameā, [ānewā,āclosedā]); // value is an array of strings setRadioButtonValue(_sdForm, āfirst_nameā, āAdamā); setCheckboxState((_sdForm, āfirst_nameā, true); // checked setCheckboxState((_sdForm, āfirst_nameā, false); // unchecked setLookupValue(_sdForm, "project_number", "123456", "My Project"); // 123456 is the record's internal record ID // "My Project" is the value of the record locator field(s) for that record
- Learn more: Record Locator
- Example - Update the field value of a text field
To update the field value of a text field first_name, use the following syntax:
- setTextFieldValue(_sdForm,āfirst_nameā,āAdamā)
- Example - Update the field value of a Lookup field
To update the field value of a Lookup field, two parameters are required:
- RecordID
- lookup.value
- This is the Record Id of the record in the object
- Field value
- lookup_name.value
- This is the value of the record in the object
To update the field value of a lookup field (project_number), use the following syntax:
form.project_number.value = "123456"; // 123456 is the internal record identifier of the record present in lookup object form.project_number_name.value = "My Project"; // My project is the value present in the record locator field(s) of the lookup object