Script to populate a field based on another fields selection

jmbrake
Kilo Expert

Can anyone assist with a script to fill a Yes/No field based on another fields selection.

Background Information:

This is an Order Guide item.   I am creating a catalog client script called:   Set Agreement Value Based on Dept

The Order form requires fields to be filled in, like name and department.   Initially the form comes up with no entries.

When I select 'Info Technology' from the reference field from the Department table , I want the field on the Order Guide Form for Confidentiality Agreement to automatically change from No to Yes.

Here is what I have tried, unsuccessfully.

Function onChange() {

  var dept = g_form.getValue('department');

  if(dept == 'info technology'){

  g_form.setValue('agreement','Yes');

  }

}

Form screen shots below:   First is the Department selection fields; second is the Yes/No question I want auto populated based on the department chosen.

find_real_file.png

find_real_file.png

1 ACCEPTED SOLUTION

function onChange(control, oldValue, newValue, isLoading) {


  if (isLoading || newValue == '') {


  return;


  }



var dept = g_form.getReference('department').name;


if(dept == 'info technology'){


      g_form.setValue('agreement','Yes');


  }


}




Try above script.



Regards,


Hardik Vora


View solution in original post

8 REPLIES 8

HV1
Mega Guru

In the order guide, create a new Catalog Client Script as below:



Name: Set Agreement Value Based on Dept


Applies to:   A Catalog Item


Catalog Item: <your order guide name>


Type: onChange


Variable Name: department



Script:function onChange(control, oldValue, newValue, isLoading) {


var dept = g_form.getValue('department');


  if(dept == 'info technology'){


  g_form.setValue('agreement','Yes');


  }


}



* assuming the variable names mentioned above are correct per your environment.



Regards,


Hardik Vora


This partially worked.   It would not accept the words 'info technology', but if I enter in the sysid of that department is works.   The department is being pulled from the Department table, so my variable in the catalog is calling a Referenced field.



Any clue why it works with sysid and not name?


Yeah correct. Since it a reference field the get Value method returns sys_Id.  


Try g_form.getReference ('department').name;


This should give you the selected department name.



Regards


HardikVora



Receive error


find_real_file.png




function onChange(control, oldValue, newValue, isLoading) {


  if (isLoading || newValue == '') {


  return;


  }




  var dept = g_form.getValue('department');




// if(dept == '34bda3027c1c7400e22a4052725d4fbe'){


  if(dept == 'info technology'){


  g_form.getReference('department').name;


  g_form.setValue('agreement','Yes');


  }


}