Configuration Item Field Issues

jared_wentzel
Kilo Contributor

I have a script that is working correctly to auto populate the 'Configuration Item' field (which is a reference field to the CMDB) with a value based on the customer selected. The problem is when I go to save the record, I get an error saying a match not found, but there is a matching machine name. I'm guessing it's just because it is populated with the script because if I remove the last letter of the machine name and then re-add it back and tab out of the field it's happy and lets me save. Is there anything I need to add to my script so it, I guess maybe, refreshes the reference field to make it happy and lets me save?

This is the Client Script I'm using:

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

{

      if (g_form.getValue('caller_id') != "")

              {

              var userNetID = g_form.getValue('caller_id.user_name');

              g_form.setValue('u_last', getLastMachine(userNetID));

              }

      else

      {

              g_form.setValue('u_last', "");

      }

     

      if (g_form.getValue('caller_id') != "" && g_form.getValue('u_last') != "" && g_form.getValue('u_last') != "undefined")

      {

              g_form.setValue('cmdb_ci',g_form.getValue('u_last'));

      }

     

      else if (g_form.getValue('caller_id') != "" && g_form.getValue('u_default_ci') != "" && g_form.getValue('u_default_ci') != "undefined")

      {

              g_form.setValue('cmdb_ci',g_form.getValue('u_default_ci'));

      }

     

      else

      {

              g_form.setValue('cmdb_ci','');

      }

     

}

     

function getLastMachine(userNetID)

{

      var gr = new GlideRecord('u_cilastloggedinto');

      gr.addQuery('u_userid'.toLowerCase(), userNetID.toLowerCase());

      gr.query();

      if(gr.next())

              {

              return gr.u_machine;

              }

}

1 ACCEPTED SOLUTION

Brad Tilton
ServiceNow Employee
ServiceNow Employee

I can't quite tell from your script, but typically when I see this situation it's because you're setting the value of the reference field with the name of the CI rather than the sys_id. The value a reference field holds is the sysid of the referenced record, and it just displays the name to you to look friendly. When you remove the letter from the name and re-add it ServiceNow is doing a lookup and putting the correct sysid. If that is the case you would need to change your script to grab the sys_id of the record you're looking for.


View solution in original post

5 REPLIES 5

jared_wentzel
Kilo Contributor

Thanks Brad!! It took a while and I ran into a few additional issues but it's all working now! Here is my code if anyone needs to do something similar..



Client Script:



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


{


if (g_form.getValue('caller_id') != "")


{


var userNetID = g_form.getValue('caller_id.user_name');


g_form.setValue('u_last', getLastMachine(userNetID));


}


else


{


g_form.setValue('u_last', "");


}



if (g_form.getValue('caller_id') != "" && g_form.getValue('u_last') != "" && g_form.getValue('u_last') != "undefined" && g_form.getValue('state') == "1")


{


lastComputerName = g_form.getValue('u_last');


lastComputerSysId = lastCISysID(lastComputerName);


g_form.setValue('cmdb_ci', lastComputerSysId);


}



else if (g_form.getValue('caller_id') != "" && g_form.getValue('u_default_ci') != "" && g_form.getValue('u_default_ci') != "undefined" && g_form.getValue('state') == "1")


{


defaultComputerName = g_form.getValue('u_default_ci');


defaultComputerSysId = defaultCISysID(defaultComputerName);


g_form.setValue('cmdb_ci', defaultComputerSysId);


}



else if (g_form.getValue('state') == "1")


{


g_form.setValue('cmdb_ci','');


}


}



function lastCISysID(u_last)


{


var ls = new GlideRecord('cmdb_ci_computer');


  1. ls.addQuery('name', u_last);
  2. ls.query();

if(ls.next())


{


return ls.sys_id;


}


}



function defaultCISysID(u_default)


{


var ds = new GlideRecord('cmdb_ci_computer');


  1. ds.addQuery('name', u_default);
  2. ds.query();

if(ds.next())


{


return ds.sys_id;


}


}



function getLastMachine(userNetID)


{


var gr = new GlideRecord('u_cilastloggedinto');


      gr.addQuery('u_userid'.toLowerCase(), userNetID.toLowerCase());


      gr.query();


if(gr.next())


{


return gr.u_machine;


}


}