Custom Field Type: Special – Browser Script

From SmartWiki
Revision as of 15:50, 4 May 2009 by David (talk | contribs)

Jump to: navigation, search

Field Display

This type of field is not displayed.

General Description

  • This field type adds a JavaScript to the page on which it is located.
  • This script will execute whenever the page is saved.
  • This field type is used by programmers to create enhanced functionality.
  • The field is not displayed.
  • In general Browser Scripts must be at the end of the page, so should be included sequentially at the end of your list of Custom Fields.

Field Options

  • Display Order: The order (relative to other fields) in which this field is displayed
  • Tab Name: Displays the field under a given tab
  • Field Name: The name of the field, used internally to reference the user's input
  • Caption: The leading question or prologue before the field
  • Track Changes: Keeps track of changes made to this field

Special Options for Field

JavaScript: Stores the procedure you wish to execute

Services

  • Enable Map Service: displays the map icon next to the field name that will launch the user’s selected map service and display a map of the content of the field
  • Enable People Search Service: displays the search Internet icon next to the field name that will launch a variety of search services and display the contact details from those services
  • Enable Organization Search Service: displays the search Internet icon next to the field name that will launch a variety of search services and display the organisations details from those services

Role - Field Permissions

These fields allow you to control who is allowed and who is denied viewing and/or modifying the field.

Referencing Other Fields

  • Standard Fields can be referenced by name (see this page for a list of standard field variable names)

Level 1: document.form1.startdate.value
Level 2 & 3: document.frmevent.startdate.value


  • To reference Custom Fields use cf_xxxxxx where xxxxxx is the field ID

Level 1: document.form1.cf_12345.value
Level 2 & 3: document.frmevent.cf_54321.value

Instead of using document.form1 at Level 1, and document.frmevent at Levels 2 & 3
you can use the following, which will simply take the first form, so works at any level:

document.forms[0]

Examples (For more examples of scripts see Category:JavaScript Examples)

1. Setting End Date to current date as long as the status is not equal to Closed

if ([status] != 'Closed')
{ 
var  t=new Date();
var year = t.getFullYear();
var mon = t.getMonth() + 1; 
var day = t.getDate();
document.form1.enddate.value=year+'-'+mon+'-'+day;
} 



2. How to calculate sum of Dynamic Custom Field

****The following instructions are for those who have Global System Administrator only. Please contact your system administrator before proceeding. ****

You will need to create in total three custom fields. Two custom fields that will be display before (having a smaller display order than) the Dynamic Custom Field (i.e if the dynamic custom field have display order of 2050 then the two custom fields display order have to be smaller than 2050 like 2045 and 2047). One custom fields will be located within the dynamic custom field.

Step 1: Create the 1st custom field using type "Text box - Single Line" that will be store the value of the sum or total. You need to remember the Field ID of this custom field before proceeding to step 2. The Field ID is located in the last column of the "Field List" view.

Step 2: Create the 2nd custom field using type "Special - Browser Script" and paste the following script into the Javascript text box. Replace the "xxxxxx" with the Field ID of the 1st Custom Field that you just created.

//Set the field that store the total to read only

document.frmedit.cf_xxxxxx.readOnly=true;

//Take the value of the passing in field and add it to total sum.

function addTotal(valuetxt,index) 
{  
   // the index is equal to 1 then reset the sum value to zero

  if (index == 1 ) 
     document.frmedit.cf_xxxxxx.value=0;

  //update the value

   document.frmedit.cf_xxxxxx.value = parseFloat(document.frmedit.cf_xxxxxx.value) +  parseFloat(valuetxt); 
} 


Step 3: You will need to go to the Edit view of the Dynamic Data - Dynamic Custom Field and create a new sub field. Create this 3rd custom field using type "Special - Browser Script" and paste the following script into the Javascript text box. Replace "xxxxx1" with the field id of the Dynamic Data - Dynamic Custom Field. Replace "xxxxx2" and "xxxxx3" with field id of all the fields that you need to sum up.

var expenseCF = new Array('cf_xxxxx1', 'cf_xxxxx2', 'cf_xxxxx3');
var i = 0;
var number= eval("document.frmedit."+ expenseCF[0] + ".value;");
number = parseInt(number);

for (i =1; i <= number; i++)
{
  var valuetxt = eval("document.frmedit."+ expenseCF[i] + ".value;");
  if (valuetxt != undefined)
  {  //Call the function AddTotal so that it would add the sum together
      addTotal(valuetxt,i);
  } 
}

NOTE: If you want the total to display at the bottom of the dynamic field, you need to create a new field and assign the value of the original total to the new custom field that you just create.

See Also

Editing Counting Business Days between Two Dates


For more examples of scripts see Category:JavaScript Examples