On Change Client Script - g_form.setValue

rsanon
Tera Contributor

I have an onChange Client Script, that is suppose to populate the assignment group on the incident form, based on some condition.

The logic seems to be working, in that the field is getting populated with the right value, but it is not linking correctly to the group table (it references none available). Which in turn, when trying to save the record, throws an error.

g_form.setValue.jpg

Client Script

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

var ga = new GlideAjax('checkOSP');

      ga.addParam('sysparm_name', 'getOPS');

      ga.addParam('sysparm_ci', g_form.getValue('cmdb_ci'));

  ga.getXMLWait();

  //alert(ga.getAnswer());

  //var answer=ga.getAnswer;

  //alert(answer);

  if (ga.getAnswer()   == "DELL"){

  g_form.setValue('assignment_group',"Dell Group");}

  if (ga.getAnswer()   == "CSI GLOBAL EDUCATION INC"){

  g_form.setValue('assignment_group',"CSI Support");}

  if (ga.getAnswer()   == "ECCA"){

  g_form.setValue('assignment_group',"ECCA - Infotech");

  if (ga.getAnswer()   == ""){

  g_form.setValue('assignment_group',"Server Support");}

}

Script Include

var checkOSP = Class.create();

checkOSP.prototype = Object.extendsObject(AbstractAjaxProcessor, {

    getOPS: function() {

      var a;

      var cmdb_ci_sys_id=this.getParameter('sysparm_ci');

     

      var ci = new GlideRecord('cmdb_ci_hardware');

  if(ci.get(cmdb_ci_sys_id))

            a=ci.u_operations_support_provider;

  else

        a="not found";

  return a;

    }});

1 ACCEPTED SOLUTION

edwin_munoz
Mega Guru

Hello Rachelle,



The problem seems to be that the assignment group is a reference field. You need to set the value of the field to the sys_id of the group, not the name. Substitute Dell Group for example for the sys_id of that group and it should work. Possibly it would be better to get the sys_id out of the script include.



Thank you.



Regards.


View solution in original post

5 REPLIES 5

edwin_munoz
Mega Guru

Hello Rachelle,



The problem seems to be that the assignment group is a reference field. You need to set the value of the field to the sys_id of the group, not the name. Substitute Dell Group for example for the sys_id of that group and it should work. Possibly it would be better to get the sys_id out of the script include.



Thank you.



Regards.


Thanks Edwin!


As said by Jim, using the sys_id and display value is the best way.. It avoids a extra Ajax call to get the display value ....


And for performance purposes, try to set both the value and display value of the reference field - Client Script Best Practices - ServiceNow Wiki:


        g_form.setValue('assignment_group', "sys_id of the group", "Group Name")



That will save a trip to the server to fetch the group name.