How to pass an array of values from Script Include to Catalog Client Script?

User205031
Tera Contributor

Hi All,

We have a requirement. If a user selects an Application from 'app_service' then tags related to that application will populate in a different field. So, if an application has 3 tags, how to populate those 3 tags? If an application has 1 tag, it is populating fine but problem is with more than 1 tag. 

 

Catalog Client Script:

function onChange(control, oldValue, newValue, isLoading) {
if (isLoading || newValue == '') {
return;
}

//Type appropriate comment here, and begin script below

var a = g_form.getValue('app_service');
alert(a);

var ga = new GlideAjax("TagPop");
ga.addParam("sysparm_name", "getTagName");
ga.addParam("sysparm_groupid", a);
ga.getXML(setTagName);

function setTagName(response) {
var answer = response.responseXML.documentElement.getAttribute("answer");
g_form.clearValue('subscription_name');
g_form.clearOptions('subscription_name');


alert(answer);

g_form.setValue('subscription_name', answer);
}
}

 

Script Include:

var TagPop = Class.create();
TagPop.prototype = Object.extendsObject(AbstractAjaxProcessor, {

getTagName: function() {
var efg = '';
var nam = this.getParameter('sysparm_groupid');
//gs.log('h1' + nam);
//var t = nam.getDisplayValue();
//gs.log('h2' + t);
var abc = new GlideRecord('label_entry');
abc.addQuery('title', 'CONTAINS', nam);
//var vendors = [];
abc.query();
if (abc.next()) {
efg = abc.label.getDisplayValue();
//vendors.push(efg.getUniqueValue());
}
return efg;
//return 'sys_idIN'+vendors.toString();
},

type: 'TagPop'

});

 

Please help!

1 ACCEPTED SOLUTION

Updated CLient Script code to be used here as below:

function onChange(control, oldValue, newValue, isLoading, isTemplate) {
    if (isLoading || newValue === '') {
        return;
    }
    if (newValue) {
        var ga = new GlideAjax('getTags');
        ga.addParam('sysparm_name', 'getTagsValue');
        ga.addParam('sysparm_val', newValue);
        ga.getXML(getTags);
    }

    function getTags(response) {
        var answer = response.responseXML.documentElement.getAttribute("answer");
        alert(answer);
		var splitAnswer = answer.split(',');
		for(var i=0;i<splitAnswer.length;i++){
			g_form.setValue('description',g_form.getValue('description') + '\n' + splitAnswer[i]);
		}

    }


    //Type appropriate comment here, and begin script below

}

 

This will ensure that tags appear in different lines as you said, did not read it initially but have modified it. Let me know for an issue here.

Hope this helps. Please mark the answer as correct/helpful based on impact.

Regards,
Shloke

Hope this helps. Please mark the answer as correct/helpful based on impact.

Regards,
Shloke

View solution in original post

15 REPLIES 15

Hi,

Thanks for confirming that my script worked.

to show them in new line just do this

var TagPop = Class.create();
TagPop.prototype = Object.extendsObject(AbstractAjaxProcessor, {

    getTagName: function() {
        var vendors = [];
        var nam = this.getParameter('sysparm_groupid');
        var abc = new GlideRecord('label_entry');
        abc.addQuery('title', 'CONTAINS', nam);
        abc.query();
        while(abc.next()) {
            vendors.push(abc.label.getDisplayValue());
        }
        return vendors.join('\n');
    },

    type: 'TagPop'

});

Did you mistakenly marked other response as correct?

I believe I answered your question and mentioned to use string or multi-line text variable.

There can be only 1 response marked as correct.

Kindly mark the answer correct based on response given quickly and accurately.

Regards
Ankur

Regards,
Ankur
✨ Certified Technical Architect  ||  ✨ 9x ServiceNow MVP  ||  ✨ ServiceNow Community Leader

Patryk5
Tera Contributor

Try:

var TagPop = Class.create();
TagPop.prototype = Object.extendsObject(AbstractAjaxProcessor, {

getTagName: function() {
var efg = [];
var nam = this.getParameter('sysparm_groupid');

var abc = new GlideRecord('label_entry');
abc.addQuery('title', 'CONTAINS', nam);

abc.query();
if (abc.next()) {
efg = push(abc.label.getDisplayValue());
}
return JSON.stringify(efg)

;
},

type: 'TagPop'

});

 

 

@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@

 

Then on Client side you can use:

 

...

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

answer = answer.split(',');

 

 

 

 

 

EXAMPLE:

var t =  ['a','b'];
t = JSON.stringify(t);
gs.print(t.split(','));
 
 
// OUTPUT:
*** Script: ["a","b"]

Hi Patryk,

I tried this but its not working. Showing error of Javacript error in browser console.

 

Thanks!

Patryk5
Tera Contributor

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

answer = answer.split(',');

 

there was space before dot.

 

try now

Patryk5
Tera Contributor

Can you send me an error?