How to use client scripts from Native UI to Service operations workspace

VISWANATH KAMAL
Tera Contributor

I need to get caller loaction for the caller in the incident table.We have this related client script in Native  UI after changing the UI type to All still the location is not auto populated.

1 ACCEPTED SOLUTION

Amit Gujarathi
Giga Sage
Giga Sage

Hi @VISWANATH KAMAL ,
I trust you are doing great.

Here's an example solution:

  1. Open the incident table in ServiceNow Native UI.
  2. Navigate to the client script section and locate the related client script.
  3. Edit the script and ensure the UI type is set to "All" so that it functions correctly across different user interfaces.
  4. Add the following code snippet to retrieve the caller's location and populate it automatically:
function onLoad() {
   var callerId = g_form.getValue('caller_id');
   if (callerId) {
      var callerGr = new GlideRecord('sys_user');
      if (callerGr.get(callerId)) {
         var location = callerGr.getValue('location');
         g_form.setValue('location', location);
      }
   }
}

Was this answer helpful?


Please consider marking it correct or helpful.


Your feedback helps us improve!


Thank you!


Regards,


Amit Gujrathi



View solution in original post

6 REPLIES 6

Amit Gujarathi
Giga Sage
Giga Sage

Hi @VISWANATH KAMAL ,
I trust you are doing great.

  1. Navigate to the Incident table in ServiceNow.
  2. Open the related client script for the Native UI.
  3. Update the client script code to include the logic for retrieving and populating the caller's location.
function onChange(control, oldValue, newValue, isLoading) {
   if (isLoading || newValue === '') {
      return;
   }

   var callerSysId = g_form.getValue('caller_id');
   var locationField = g_form.getField('location');

   if (callerSysId) {
      var caller = new GlideRecord('sys_user');
      if (caller.get(callerSysId)) {
         var location = caller.getValue('location');
         if (location) {
            g_form.setValue('location', location);
         } else {
            locationField.clearValue();
         }
      }
   } else {
      locationField.clearValue();
   }
}

Was this answer helpful?


Please consider marking it correct or helpful.


Your feedback helps us improve!


Thank you!


Regards,


Amit Gujrathi



@Amit Gujarathi 

We already have the client script to get the caller location.It is working in Native UI but not in Service operations workspace.

Hi Viswanath Kamal , 
To work Client Script in Specific View , Uncheck the Global Field Checkbox in Client Script Form and Then View Field will be Visible and select the view which is required (for Service Operations Workspace ) Enter sow View

Amit Gujarathi
Giga Sage
Giga Sage

Hi @VISWANATH KAMAL ,
I trust you are doing great.

Here's an example solution:

  1. Open the incident table in ServiceNow Native UI.
  2. Navigate to the client script section and locate the related client script.
  3. Edit the script and ensure the UI type is set to "All" so that it functions correctly across different user interfaces.
  4. Add the following code snippet to retrieve the caller's location and populate it automatically:
function onLoad() {
   var callerId = g_form.getValue('caller_id');
   if (callerId) {
      var callerGr = new GlideRecord('sys_user');
      if (callerGr.get(callerId)) {
         var location = callerGr.getValue('location');
         g_form.setValue('location', location);
      }
   }
}

Was this answer helpful?


Please consider marking it correct or helpful.


Your feedback helps us improve!


Thank you!


Regards,


Amit Gujrathi