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

SanjivMeher
Kilo Patron
Kilo Patron
  1. function onChange(control, oldValue, newValue, isLoading) {  
  2.     if (newValue == '') {  
  3.           return;  
  4.     }  
  5.          
  6. var user_ref = g_form.getReference('user', loadInfo);  
  7.  
  8. function loadInfo(user_ref) {  
  9.      
  10. var gp = new GlideRecord('cmn_location');  
  11.       gp.addQuery('sys_id', user_ref.location);  
  12.         gp.query(function(gp){  
  13.   if(gp.next())   {  
  14.       var locname = gp.name;  
  15.         // This is working up until now  
  16. // NOW I NEED TO SET A VALUE ON A REFERENCE FIELD ON WHERE THE NAME IS THE SAME AS gp.name  
  17.            
  18.               g_form.setValue('your variable name',gp.sys_id); //Use sys_id, since you are setting a reference field to location
  19.           }  
  20.   });    
  21. }}  

Please mark this response as correct or helpful if it assisted you with your question.

Hi Sanjiv



The sys_id's are not the same, only the names. The user's location is different to the new location table's data, so I am setting the new reference if I can find the same value. We are getting the new locations from a different source.



Regards


In that case, you will have to query the other data source as wel.



Please mark this response as correct or helpful if it assisted you with your question.

Harsh Vardhan
Giga Patron

Hi Rudi,



Writing glide record in client script is not a best practice. you can try with script include and call it in client script using glide ajax.


It will slow your form because you are travelling to server to get the record. each time when you will make the changes on form field it will enter to the server.



http://wiki.servicenow.com/index.php?title=GlideAjax