The CreatorCon Call for Content is officially open! Get started here.

How to auto populate list collector variable using on Load client script and script include

Alon Grod
Tera Expert

Hi,


I have the List collector variable called 'u_ramad' that is a reference to the sys_user table. I have the field 'u_ramad' of type List (reference to sys_user table as well) in my 'u_ou' table.

I need to write onLoad client script that called a script include which return the values from the list field 'u_ramad' from the u_ou table. These values should be populated automatically in the right bucket of the 'u_ramad' list collector variable.

function onLoad() {

 var user = g_form.getvalue('requested_for');

 var ga = new GlideAjax('fill Ramad'); 
 ga.addParam( sysparm_name, isRamad);
 ga.addParam('sysparm_std', user);

 ga.getXML(getResponse);

 function getResponse(response) {

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

 alert(ans);

 if(ans != '') {

   g_form.setvalue('u_ramad', ans);

   }
}

 

Script include:

var fillRamad = Class.create(); 
fillRamad.prototype object.extendsObject(AbstractAjaxProcessor, {
  isRamad: function() {

     var us  = this.getParameter('sysparm_std');

     var gr= new GlideRecord('sys_user);
     gr.addEncodedQuery('sys_id='+us.toString());
     gr.query();
     if(gr.next()) {
          var hi = gr.u_hierarchy.toString();
          hi =  hi.split('/')[0];

         var sp= new GlideRecord('u_ou'); 
         sp.addEncodedQuery('u_ou=' + hi);
         sp.query();
         if(sp.next()) {

            return sp.u_ramad.toString();
      }

},
 type: 'fillRamad'

});

 

1 ACCEPTED SOLUTION

Hi @Alon Grod 

 

can you try to update Variable attribute to  "glide_list"

 

VishalBirajdar7_0-1694416656440.png

 

 

 

Vishal Birajdar
ServiceNow Developer

I know one thing, and that is that I know nothing.
- Socrates

View solution in original post

8 REPLIES 8

Vishal Birajdar
Giga Sage

Hi @Alon Grod 

 

Can you try below code ...!! Just modified some things in your code

 

Step 1 : onChange script on requested for 

 

 var user = g_form.getValue('requested_for');

    g_form.addInfoMessage('user=' + user);

    var ga = new GlideAjax('fillRamad');
    ga.addParam('sysparm_name', "isRamad");
    ga.addParam('sysparm_std', user);

    ga.getXMLAnswer(getResponse);

    function getResponse(answer) {

        g_form.addInfoMessage(answer);

        if (answer) {

            g_form.setValue('u_ramad', answer);

        }

    }

 

Step 2 : Client callable script include

 
isRamad: function() {

         var result;

         gs.log("fillrmad= inside script include");

        var us = this.getParameter('sysparm_std');

         gs.log("fillrmad= US=" + us);

        var gr = new GlideRecord('sys_user');

        gr.addEncodedQuery('sys_id=' + us);

        gr.query();

        if (gr.next()) {

            var hi = gr.u_hierarchy.toString();

            hiSplit = hi.split('/')[0];

            var sp = new GlideRecord('u_ou');

            sp.addEncodedQuery('u_ou=' + hiSplit);

            sp.query();

            if (sp.next()) {

                gs.log("fillramad=" + sp.u_ramad.toString());  

                result =  sp.u_ramad.toString();

               

            }

        }

        return result;

    },

 

Output : 

Record in the OU table 

VishalBirajdar7_0-1694415365554.png

 

Same users will be displayed on "Ramad" variable

 

VishalBirajdar7_1-1694415425090.png

 

 

 

 

 

 

Vishal Birajdar
ServiceNow Developer

I know one thing, and that is that I know nothing.
- Socrates

@Vishal Birajdar what is the field type of Ramad variable that you created in the catalog item? How did you make it as a List?

@Vishal Birajdar I can only make it as a list collector

Hi @Alon Grod 

 

can you try to update Variable attribute to  "glide_list"

 

VishalBirajdar7_0-1694416656440.png

 

 

 

Vishal Birajdar
ServiceNow Developer

I know one thing, and that is that I know nothing.
- Socrates