Difference between revisions of "Field Scripts"

From AgileApps Support Wiki
imported>Aeric
imported>Aeric
Line 80: Line 80:
This code accesses the record targeted by a Lookup, retrieves a field value, and places it into the current form. Here, the current record is an OrderItem. When the user selects the Order the item is part of, this code retrieves the Order number and stores it in the current record.
This code accesses the record targeted by a Lookup, retrieves a field value, and places it into the current form. Here, the current record is an OrderItem. When the user selects the Order the item is part of, this code retrieves the Order number and stores it in the current record.
:<syntaxhighlight lang="javascript" enclose="div">
:<syntaxhighlight lang="javascript" enclose="div">
    var order = _sdForm.order.order_num;   // Get Order# from the Order record
  var order = _sdForm.order.order_num;               // Get value from Order record
    setTextFieldValue(_sdForm, "order_number", order); // set order_number in current record
  setTextFieldValue(_sdForm, "order_number", order); // Set it in current record
</syntaxhighlight>
</syntaxhighlight>



Revision as of 22:36, 21 May 2014

About Field Scripts

Field scripts let you execute JavaScript when a field in a Form gets focus or its value changes.

JavaScript code can be run at these field-level events:

  • On Change - This event happens when a user enters a new value in a field. At this event you can change other fields based on the value a user just entered or change the formatting of the value (such as for a telephone number).
  • On Focus - This event happens when a user moves the focus to a field. At this event you can remind the user what to enter or temporarily change the appearance of the field (set as setting the background color).

See Field Scripting Variables for information on variable syntax.

Compare to Form Scripts and Post Selection JavaScript

You can add On Change or On Focus event scripts to any field except the following:

  • Auto Number
  • Custom Control
  • File field
  • Formula
  • Image field
  • Lookup
  • Multi object Lookup
  • Rich Text Area

Working with Field Scripts

Editing Field Scripts

Follow these steps to add or change event scripting for a field:

  1. Click the script icon (Script icon.gif) next to a field in the layout
  2. Enter or change the JavaScript in Field On Change Script and/or Field On Focus Script
  3. Click the Save button

Writing JavaScript

Using JavaScript, you can

You can also use the implicit variable obj to refer to the field where the most recent event occurred.

These examples show using the implicit variable obj with these properties:

alert("Selected field's name: " + obj.name);
alert("Selected field's value: " + obj.value);

On Change Examples

Check Approval Status

When a user changes an order, if a discount is given, make sure that approval_required is checked.

if (_sdForm.discount.value != '0') {
   setCheckboxState(_sdForm, "approval_required", true);
}
Capitalize a Name

When a user enters a first name, capitalize it automatically:

<pre>
var upperName = _sdForm.first_name.value.toUpperCase();
setTextFieldValue(_sdForm, "first_name", upperName);
   // The line could also be written as:
   // obj.value = obj.value.toUpperCase();
Display an Alert with Multiple Values

Display an alert dialog showing the values selected from multiple checkboxes.

var values = getMultiCheckBoxValue(_sdForm, "assets", 0); 
    // Returns a list. For example: ["Laptop", "Cellphone"]
alert( "Multi Check Box Values are: " + values);
Copy a Field in the Current Record

When a user changes the job_knowledge_notes field, copy the new value to the dependability_notes field.

var note = _sdForm.job_knowledge_notes.value;
setTextFieldValue(_sdForm, "dependability_notes", note);
Copy a Field from Another Record

This code accesses the record targeted by a Lookup, retrieves a field value, and places it into the current form. Here, the current record is an OrderItem. When the user selects the Order the item is part of, this code retrieves the Order number and stores it in the current record.

   var order = _sdForm.order.order_num;               // Get value from Order record
   setTextFieldValue(_sdForm, "order_number", order); // Set it in current record
Conditional Change to a Value

Change the value of a text area based on a variable.

if(obj.value == "Record1") {
    setTextFieldValue(_sdForm, "description", "First Record") 
}
if(obj.value == "Record2") {
    setTextFieldValue(_sdForm, "description", "Second Record") 
}
else {
    setTextFieldValue(_sdForm, "description", "Not first or second record") 
}

Change the value of a checkbox field to true when the request amount is large enough.

if(_sdForm[0].request_amount.value > 10000) {
    setCheckboxState(_sdForm, "approval_required", true);
}

On Focus Example

When a user moves the focus to the account name field, remind the user to enter the name capitalized.

alert("Please enter the account name in all caps");

Grid Field Scripting

You can run JavaScript code for On Change and On Focus events in grid fields.

Follow these steps to add event scripting to a grid field:

  1. Click the script icon (Script icon.gif) in the column header of a field in a grid.
  2. Enter the JavaScript in On Change Script and/or On Focus Script.
  3. Click the Save button.

You can refer to these implicit variables in event scripts for grid fields:

  • grid_section: current grid name used as a prefix for field names
  • grid_section_row_id: current row identifier

For example, assume that you have a grid with these field names: full_name, first_name, and last_name. You can create references to the fundamental fields with the statements below:

var firstname = eval('_sdForm.'+grid_section+'first_name');
var lastname = eval('_sdForm.'+grid_section+'last_name');

You can then update the full_name field automatically in an On Change event script for the first name or last name field with this code:

var fullname = firstname.value + ' ' + lastname.value;
setTextFieldValue(_sdForm, "full_name", fullname);

Learn More