SetValue of Reference field catalog client script

Rudi2
Giga Guru

Hi Guys, just a question

I am a bit stuck and need to finish this script:

What I want to do is to return a name and use that name to set another reference field with the same name

I just need help with the set of the reference field

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

    if (newValue == '') {

          return;

    }

     

var user_ref = g_form.getReference('user', loadInfo);

function loadInfo(user_ref) {

 

var gp = new GlideRecord('cmn_location');

      gp.addQuery('sys_id', user_ref.location);

        gp.query(function(gp){

  if(gp.next())   {

      var locname = gp.name;

        // This is working up until now

// NOW I NEED TO SET A VALUE ON A REFERENCE FIELD ON WHERE THE NAME IS THE SAME AS gp.name

       

         

          }

  });

}}

Once this is done, what would be the best way to do this for another 2 reference fields?

Regards

1 ACCEPTED SOLUTION

Arindam Ghosh
Mega Guru

Hi Rudi,



For this scenario you need to use glide ajax, as you need to do DB operation through client script. So Create a Catalog Client Script and pass the User sys ID to Script Include where we do DB operation and again the location Sys ID sends back to Client script where we set the location. Please use the below script:



Catalog Client Script:



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


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


          return;


    }



g_form.clearValue('location');


var user = g_form.getReference('user').sys_id;



var ajax = new GlideAjax('SetLocationUtil');


ajax.addParam('sysparm_name', 'setLocation');


ajax.addParam('sysparm_user', user);



ajax.getXML(function () {


      g_form.setValue('location', ajax.getAnswer());


});


   


}



Script Include:



Script include Name:   SetLocationUtil


and don't forget to check the Client callable checkbox.


find_real_file.png



Here is the Script for Script include



var SetLocationUtil = Class.create();


SetLocationUtil.prototype = Object.extendsObject(AbstractAjaxProcessor, {



setLocation: function(){



var user = this.getParameter('sysparm_user');


var gr = new GlideRecord("sys_user");


gr.addQuery('sys_id',user);


gr.query();


if (gr.next()) {


return gr.location.sys_id.toString();


}


else return '';


},



type: 'SetLocationUtil'


});



Thanks,


Arindam


View solution in original post

5 REPLIES 5

Arindam Ghosh
Mega Guru

Hi Rudi,



For this scenario you need to use glide ajax, as you need to do DB operation through client script. So Create a Catalog Client Script and pass the User sys ID to Script Include where we do DB operation and again the location Sys ID sends back to Client script where we set the location. Please use the below script:



Catalog Client Script:



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


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


          return;


    }



g_form.clearValue('location');


var user = g_form.getReference('user').sys_id;



var ajax = new GlideAjax('SetLocationUtil');


ajax.addParam('sysparm_name', 'setLocation');


ajax.addParam('sysparm_user', user);



ajax.getXML(function () {


      g_form.setValue('location', ajax.getAnswer());


});


   


}



Script Include:



Script include Name:   SetLocationUtil


and don't forget to check the Client callable checkbox.


find_real_file.png



Here is the Script for Script include



var SetLocationUtil = Class.create();


SetLocationUtil.prototype = Object.extendsObject(AbstractAjaxProcessor, {



setLocation: function(){



var user = this.getParameter('sysparm_user');


var gr = new GlideRecord("sys_user");


gr.addQuery('sys_id',user);


gr.query();


if (gr.next()) {


return gr.location.sys_id.toString();


}


else return '';


},



type: 'SetLocationUtil'


});



Thanks,


Arindam