Difference between revisions of "Form Scripts"

From AgileApps Support Wiki
imported>Aeric
imported>Aeric
Line 33: Line 33:
{{:Referencing Fields in JavaScript}}
{{:Referencing Fields in JavaScript}}


;Example - Update the field value of a text field:
===="On Load" Examples====
 
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>
|}
 
====Writing Form Scripts====
 
====On Load Examples====
Convert the current date into day name, month name, and day number format and puts the resulting value in the <tt>account_order</tt> field.
Convert the current date into day name, month name, and day number format and puts the resulting value in the <tt>account_order</tt> field.
:{|
:{|
Line 106: Line 81:
|}
|}


====On Save Examples====
===="On Save" Examples====


Make sure there is a contact when creating a record in a "Leads" object.
Make sure there is a contact when creating a record in a "Leads" object.

Revision as of 01:30, 27 June 2013

About Form Scripts

Form Scripting lets you specify JavaScript code to execute when a Form is loaded or saved.

JavaScript code can be invoked at these form-level events:

  • On Load
  • This event happens when the form loads
  • By default, the On Load scripts are triggered on any record action (View, Add, Update)
Optionally, invoke the script on a specific action (View or Add or Update)
Learn more: Trigger on a Specific Action
  • On Save
  • This event happens when a user clicks the [Submit] button on a form
  • At this event, optionally perform custom front-end validations before sending the data to the server
  • Return false to cancel the save
  • Resusable Script Functions
  • Available to be called from the On Load or On Save event form scripts
  • Also available to be called from the On Change and On Focus event field scripts
Learn more:

Working with Form Scripts

Editing Form Scripts

Follow these steps to add or change scripting in a form:

  1. Click [Form Script]
  2. Click [Edit]
  3. Enter or change the JavaScript in On Load Script, On Save Script, and/or Reusable Script Functions
  4. Click [Save]

Referencing Fields in Form Scripts

Referencing Fields in JavaScript

"On Load" Examples

Convert the current date into day name, month name, and day number format and puts the resulting value in the account_order field.

var form = document.mainForm; 

var v1 = new Date(); 
var v2 = new Array('Sunday', 'Monday', 'Tuesday', 'Wednesday', 'Thurs', 'Fri', 'Sat', 'Sun'); 
var v3 = new Array('Jan', 'Feb', 'Mar', 'Apr', 'May', 'Jun', 'Jul', 'Aug', 'Sep', 'Oct', 'Nov', 'Dec'); 
form.account_order.value = v2[v1.getDay()] + " " + v3[v1.getMonth()] + " " + v1.getDate(); 
Trigger on a Specific Action

To invoke a script On Load + Action (View, Update, Add or Clone a record):

Trigger on record View:

var action = document.mainForm.a.value;
if ( action == "view") {
// java script to be triggered on the view of the record only
<!--...-->
}

Trigger on record Update:

var action = document.mainForm.a.value;
if ( action == "update") {
// java script to be triggered on the update of the record only
<!--...-->
}

Trigger on record Add or Clone:

var action = document.mainForm.a.value; 
if ( action == "add") {
//java script to be triggered on the add or clone of the record only
<!--...-->
}

"On Save" Examples

Make sure there is a contact when creating a record in a "Leads" object.

var form = document.mainForm;
if (form.reference_type[form.reference_type.selectedIndex].value == 'Lead')
{
    if (form.contact_id.value == "")
    {
      alert("Please enter Contact Name");
      return false; // cancel the save operation
    }
}

If a user checked the "Done" radio button in a radio button group named technical_spec_completed, set the spec_date field with the current date formatted as month number, day number, and year.

var form = document.mainForm;
function formatDate(value)
{
    return value.getMonth()+1 + "/" + value.getDate() + "/" + value.getFullYear();
} 
for (var i=0; i<form.technical_spec_completed.length; i++) {
    if(form.technical_spec_completed[i].checked &&
        form.technical_spec_completed[i].value == 'Done' && form.spec_date.value == '')
    {
        t = new Date();
        form.spec_date.value = formatDate(t);
    }
}

The email address field on a form is not mandatory, but you want to encourage users to enter it. If the user clicks OK in the confirm dialog, it cancels the save so they can then enter the email address. If they click cancel, the save proceeds.

var form = document.mainForm;

if(form.email.value=="")
    if (confirm("While the Email field is optional, we recommend that you
enter a value")) 
    {
        //User clicked OK: do not proceed with save
       return false;
    else
    {
        //User clicked cancel: let save happen
        return true;
    }
}

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);
}

Learn More