Client script to auto populate fields on scoped application

Marques2
Tera Expert

Hello,

I recently created a scoped application that handles an HR request process, Employee Action Request. I created the table and then added a service catalog record producer for the request. We've done this similarly for another request process where we query the cmdb_ci table and populate some information on the catalog item form based on the selected CI. In this example, I wanted to auto-populate information about a specific user based on the user record I had selected from a reference field on the sys_user table. One obstacle is that we need it to work on the Service Portal as well as the 'Native UI' service catalog. As a disclaimer, I used the same script 'skeleton' that I used for the auto-population of the cmdb_ci information.

Problem: The catalog client script I'm using does not auto-populate the information of the user in the 'Native UI', however, it's working as expected in Central Station (our Service Portal).

find_real_file.png

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

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

          return;

    }

    //Save a little time by filling in four fields for the user with what we expect the answers to be

// Search for this user in the sys_user table

var gfd = new GlideRecord('sys_user');

gfd.addQuery('sys_id', newValue);

gfd.query(function(gfd) { // this callback query is used to comply with Central Station (Service Portal)

if(gfd.next()) {

// If it's found, fill in the fields with its data

g_form.setValue('u_requested_for_user_name', gfd.user_name);

g_form.setValue('u_requested_for_manager', gfd.manager);

g_form.setValue('u_requested_for_title', gfd.title);

g_form.setValue('u_requested_for_location', gfd.location);

}

});

 

}

I'll reiterate that this is for a scoped application, attempting to read the sys_user table. However, I don't think that's a problem based on the fact that it's functioning on the Service Portal.

Anyone know maybe why it's not running in the Native UI?

Your help is greatly appreciated,

-Marques

1 ACCEPTED SOLUTION

Shishir Srivast
Mega Sage

Please try in this way using getReference() function http://wiki.servicenow.com/index.php?title=GlideForm_(g_form)#getReference



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


  1.     if (isLoading || newValue == '') {  
  2.           return;  
  3.     }  

var caller = g_form.getReference('u_requested_for', doAlert);


function doAlert(caller) {


  1. g_form.setValue('u_requested_for_user_name', caller.user_name);  
  2. g_form.setValue('u_requested_for_manager', caller.manager);  
  3. g_form.setValue('u_requested_for_title', caller.title);  
  4. g_form.setValue('u_requested_for_location', caller.location);

}


View solution in original post

4 REPLIES 4

Raju Koyagura
Tera Guru

Better create GlideRecord query in script include and call that in your client script using GlideAjax.


Shishir Srivast
Mega Sage

Please try in this way using getReference() function http://wiki.servicenow.com/index.php?title=GlideForm_(g_form)#getReference



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


  1.     if (isLoading || newValue == '') {  
  2.           return;  
  3.     }  

var caller = g_form.getReference('u_requested_for', doAlert);


function doAlert(caller) {


  1. g_form.setValue('u_requested_for_user_name', caller.user_name);  
  2. g_form.setValue('u_requested_for_manager', caller.manager);  
  3. g_form.setValue('u_requested_for_title', caller.title);  
  4. g_form.setValue('u_requested_for_location', caller.location);

}


That worked great! Very clean solution.



Thank you for the help!


- Marques


Harsh Vardhan
Giga Patron

writing glide record at client side is not good practice.


you can try with script include and call it in catalog client script through glide ajax.



Script Include: Please checked client callable then it will be callable in client script.



var test = Class.create();


test.prototype = Object.extendsObject(AbstractAjaxProcessor, {


    getValue: function() {


var value=[];


  var check =this.getParameter('sysparm_user_name');


  var gfd = new GlideRecord('sys_user');  


  gfd.addQuery('sys_id', check);


  gfd.query();


while(gfd.next())


{


var obj={};


obj.name=grp.getDisplayValue('user_name');


obj.manager=grp.getDisplayValue('manager');


obj.location=grp.getDisplayValue('location');


obj.title=grp.getDisplayValue('title');



value.push(obj);


gs.log("hello:"+value);


}


    return JSON.stringify(value);


    },



});




Catalog Client script:




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


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


          return;


    }


var abc= g_form.getValue('u_requested_for');


alert('value of requested user'+abc);


var check = new GlideAjax('global.test');   // if your script include has written in different scope then replace the global with you application name


check.addParam('sysparm_name', 'getValue');


check.addParam('sysparm_user_name', abc);


check.getXML(analyzeResponse);



function analyzeResponse(response)


{


var answer = response.responseXML.documentElement.getAttribute("answer");


alert('this is test :'+answer);


var obj = JSON.parse(answer);


for (var k = 0; k < obj.length; k++) {


g_form.setValue('u_requested_for_user_name',obj[k].name);


g_form.setValue('u_requested_for_manager',obj[k].manager);


g_form.setValue('u_requested_for_title',obj[k].title);


g_form.setValue('u_requested_for_location',obj[k].location);


}


}


}




Let me know if you have any question .