OnChange script examples

Example 1

You can add an OnChange Script, which changes the Team assigned to an Opportunity depending on the type of opportunity. You must attach the OnChange Script to the field that you want to change.

  1. Click <My Profile> | Administration | Customization | Primary Entities | Opportunity.
  2. Click the Screens tab.
  3. Click the Edit icon beside Opportunity Detail Screen.
  4. Highlight Opportunity: Type within the Screen Contents panel.
  5. Type the following script in OnChangeScript. The first part of the script checks to see if the channelid field is present on the screen.

    if(typeof(oppo_channelid)!='undefined')
    {
    if(oppo_type.value=='Mix')
    {
    oppo_channelid.value='4'
    }
    else if(oppo_type.value=='Consulting')
    {
    oppo_channelid.value='3'
    }
    else
    {
    oppo_channelid.value='2'
    }
    }
  6. Click Update and then click Save.
  7. Add a new opportunity and select Consulting from Type. A team value, in this case Customer Service, automatically fills the Team field. If you change the opportunity type to Mix, a different team value, in this case Marketing automatically fills the Team field.

Example 2

You can add an OnChange Script, which changes the company status to Inactive when the company type is changed to Partner. You can use "this.value" in place of the actual field name when the script is being attached to the actual field that is changing.

  1. Click the Company Entry Screen.
  2. Highlight Company: Type and enter the following in OnChangeScript. When you create or edit a company type and change the company type to Partner, the Status field automatically defaults to Inactive.

    if(this.value == 'Partner')
    {
    comp_status.value = 'Inactive'
    }

Example 3

You can add an OnChange Script, which disables the Company Revenue field when the Company Type is set to Partner.

  1. Click the Company Entry Screen.
  2. Highlight Company: Type.
  3. Enter the following script in OnChange:

    if (this.value == 'Partner')
    {
    comp_revenue.disabled='true';
    }
  4. Click Update and then click Save.
  5. Open a company and change the type to Partner. The Company Revenue field is disabled.

Example 4

You can add an OnChange Script, which hides the company revenue field for companies with more than 500 employees. This example also shows how you can use the visibility property of the HTML DOM.

  1. Click <My Profile> | Administration | Customization | Primary Entities | Company | Screens.
  2. Click the Company Entry Screen.
  3. Highlight Company: Employees.
  4. Enter the following script in OnChangeScript.

    if(this.value =='501+')
    {
    comp_revenue.style.visibility = 'hidden';
    }
    else comp_revenue.style.visibility = 'visible'
  5. Click Update and then click Save. When you create a new company and select 501+ from the Employees list, the Revenue field is automatically hidden. The above script hides the Revenue (comp_revenue) field when the user selects 501+. It will not be run if the field is already set to 501+.
  6. To make this customization complete, you can add a Create script to the comp_revenue as follows, and the field then remains hidden:

    if (Values('comp_revenue')=='501+')
    {
    Hidden=true;
    }