Join the #BuildWithBuildAgent Challenge! Get recognized, earn exclusive swag, and inspire the ServiceNow Community with what you can build using Build Agent.  Join the Challenge.

Pass list collector values from server to Glide Ajax

imran sk
Tera Contributor

I have two fields

 

Primary owner - reference 

 

Secondary owner - list collector 

 

Now I need to fetch all the values from both the fields (field 1: Primary owner - reference, field 2: Secondary owner - list collector ) and pass it to Glide Ajax and then I need to compare the values with requested for field . If requested for is any one of from the values of script include I need to add alert on the form in portal 

 

 

 

data : function () {

 

var name = this.getParameter(sysparm_item);

 

var gr = new Glide Record('xyz_table');

 

gr.addquery('sys_id', name);

gr.query();

If (gr.next()){

Var aa = gr.primary_owner + gr.secondary_owner ;

Return aa;

 

 

Ajax

 

Var requesteduser = g_form.getValue('requested_for');

 

Var ga = new Glide Ajax('SI');

 

ga.addParam('sysparm_name', 'data');

 

ga.addParam('sysparm_id', g_form.getUniqueID());

 

ga.getXML(callback);

 

 

 

Function callback(response){

 

Var ans= response.responseXML.getattributr.doxumentid("answer");

 

Now here I need to compare each value with requesteduser and if it matches I need to add alert 

 

 

 

Need your suggestion experts..thanks in advance 🙏 

 

An

2 REPLIES 2

Brad Bowman
Kilo Patron
Kilo Patron

You would benefit a lot from reading through this guide on GlideAjax

https://www.servicenow.com/community/developer-articles/glideajax-example-cheat-sheet-updated/ta-p/2... 

In your client script addParam lines, you need one for each variable you are passing to the SI - the reference variable, and the list collector.  If requested_for is the name of the reference variable, that's fine but you aren't using requesteduser anywhere if this is the case it should be the sysparm_id, not what you have there.  You should also name your Script Include and function better - make it unique, and not the possibility of a reserved/system word like 'data'.

 

Reference and List Collector variables (fields are on tables, variables on Catalog Items) store sys_ids as their values, so if all you need to do is compare that to requested_for (also a sys_id), you can do that without a trip to the server / Glide Ajax. 

In this video I'll show you how to make your server side JavaScript easier to maintain. We've talked about Script Includes as a way to improve unit testing, abstracting and reusing logic, but there's another use for them to help remove hard-coded values, decrease maintenance, and improve ...

Mani A
Tera Guru

@imran sk 

 

Ok modify ur scripts..i really didn't understand what exactly the use of table in SI

But I was suggesting based on ur code only

 

 

Client script :

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

    

    var ga = new GlideAjax('SI');

    ga.addParam('sysparm_name', 'data');

    ga.addParam('sysparm_id', g_form.getUniqueID());

    ga.getXML(callback);

}

 

function callback(response) {

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

 

    var x = JSON.parse(answer);   

    if(x.includes(g_form.getValue('requested_for')  || x.toString().indexOf(g_form.getValue('requested_for')>-1) {

        alert('The requested user matches one of the owners.');

    }

}

 

Script include:

 

    data: function() {

        var sysId = this.getParameter('sysparm_id');

        var y= [];

 

        var gr = new GlideRecord('xyz_table');

        gr.addQuery('sys_id', sysId);

        gr.query();

        

        if (gr.next()) {

            

            if (gr.primary_owner) {

                y.push(gr.primary_owner.toString());

            }

           

            var secOwners = gr.secondary_owner.toString().split(',');

            y=y.concat(secOwners);

        }

 

        return JSON.stringify(y);

    },