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

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.


That is helpful thank you! Unfortunately I am grabbing values that much but they exist on different tables so the sysID I were to grab wouldn't match. I might be at a dead end


If you have the name and that is matching, there's nothing that should prevent from querying for the sysid. You might need to use some glideajax so you can use some server side scripting to query the cmdb, but you should be able to retrieve the sysid.


So I would just have to create another function, and do a GlideRecord query with the results to convert them to from the name to the CMDBs sysid for the name value? Something like that?