Call an include script from a macro

andreaLuongo
Tera Expert

Hello everyone,

I am trying to build a button on the side of a field.
the button once pressed for now should call an include script via GlideAjax, but it doesn't work and doesn't give error if I try to re-call it from background script it tells me that the includes script is not defined.

 

If in the background script I call the include script in this way it works:

andreaLuongo_1-1712236682095.png

But from the macro it doesn't work and gives me as error 'ReferenceError: test1234 is not defined'

 

macro:

 

function user(){
	var element = document.getElementById("pointAL");
	element.removeAttribute("id");
		
	var group = g_form.getValue('assignment_group');
	var user = g_user.userID;

	var f = new test1234;
	var answer = f.user_has_group(user,group);

	if(answer){
		var add = '<a id="${jvar_n}" onclick="modalOpen()" title="Add your group" class="icon ref-button icon-add btn btn-default btn-ref"></a>';
		var container = document.getElementById("container");
		container.innerHTML = add;
	}else{
		var remuve = '<a id="${jvar_n}" onclick="modalOpen()" title="Remuve your group" class="icon ref-button icon-subtract btn btn-default btn-ref"></a>';
		var container = document.getElementById("container");
		container.innerHTML = remuve;
	}     
}

 

 

script include:

 

var test1234 = Class.create();
test1234.prototype = {
    user_has_group: function(user,group) {
		var gm = new GlideRecord('sys_user_grmember');
		gm.addQuery('group.sys_id='+group+'^user.sys_id='+user);
		gm.query();

		if(gm.next()){
			return true;
		}else{
			return false;
		}
    },

    type: 'test1234'
};

 

 
1 ACCEPTED SOLUTION

Subhashis Ratna
Tera Guru

Hi @andreaLuongo 

Rectify this syntax : 

var f = new test1234();
var answer = f.user_has_group(user, group);

I can see that the syntax to call the script include is incorrect in UI Macro. Here, I have updated the syntax to `var f = new test1234();`.


Additionally, since the `test1234()` script is client-callable, but it doesn't extend any out-of-the-box (OOTB) objects in your script include, the you can utilize the GlideAjax class in your UI Macro.

SubhashisRatna_1-1712377102556.png

 



Below I'm attaching a screenshot for better reference and to Call Script include in Ui Macro , 

SubhashisRatna_0-1712375738685.png

 

Here i  have updated your code from UI Macro Side , could you please try and let me know 

UI Macro Code : 

 

function user() {
    var element = document.getElementById("pointAL");
    element.removeAttribute("id");

    var group = g_form.getValue('assignment_group');
    var user = g_user.userID;

    var ga = new GlideAjax('global.test1234');
    ga.addParam('sysparm_name', 'user_has_group');
    ga.addParam('user', user);
    ga.addParam('group', group);
    ga.getXMLAnswer(function(response) {
        if (response == 'true') {
            var add = '<a id="${jvar_n}" onclick="modalOpen()" title="Add your group" class="icon ref-button icon-add btn btn-default btn-ref"></a>';
            var container = document.getElementById("container");
            container.innerHTML = add;
        } else {
            var remuve = '<a id="${jvar_n}" onclick="modalOpen()" title="Remove your group" class="icon ref-button icon-subtract btn btn-default btn-ref"></a>';
            var container = document.getElementById("container");
            container.innerHTML = remuve;
        }
    });
}

 


 Script Code :

 

var test1234 = Class.create();
test1234.prototype = Object.extendsObject(AbstractAjaxProcessor, {
    user_has_group: function(user, group) {
        var gm = new GlideRecord('sys_user_grmember');
        gm.addQuery('group.sys_id', group);
        gm.addQuery('user.sys_id', user);
        gm.query();

        return gm.hasNext();
    },
    type: 'test1234'
});

 


If this solution resolves your query, kindly mark it as the accepted solution and give it a thumbs up.

Thanks,
Subhashis Ratna

View solution in original post

2 REPLIES 2

andreaLuongo
Tera Expert

andreaLuongo_0-1712237235126.png

 

Subhashis Ratna
Tera Guru

Hi @andreaLuongo 

Rectify this syntax : 

var f = new test1234();
var answer = f.user_has_group(user, group);

I can see that the syntax to call the script include is incorrect in UI Macro. Here, I have updated the syntax to `var f = new test1234();`.


Additionally, since the `test1234()` script is client-callable, but it doesn't extend any out-of-the-box (OOTB) objects in your script include, the you can utilize the GlideAjax class in your UI Macro.

SubhashisRatna_1-1712377102556.png

 



Below I'm attaching a screenshot for better reference and to Call Script include in Ui Macro , 

SubhashisRatna_0-1712375738685.png

 

Here i  have updated your code from UI Macro Side , could you please try and let me know 

UI Macro Code : 

 

function user() {
    var element = document.getElementById("pointAL");
    element.removeAttribute("id");

    var group = g_form.getValue('assignment_group');
    var user = g_user.userID;

    var ga = new GlideAjax('global.test1234');
    ga.addParam('sysparm_name', 'user_has_group');
    ga.addParam('user', user);
    ga.addParam('group', group);
    ga.getXMLAnswer(function(response) {
        if (response == 'true') {
            var add = '<a id="${jvar_n}" onclick="modalOpen()" title="Add your group" class="icon ref-button icon-add btn btn-default btn-ref"></a>';
            var container = document.getElementById("container");
            container.innerHTML = add;
        } else {
            var remuve = '<a id="${jvar_n}" onclick="modalOpen()" title="Remove your group" class="icon ref-button icon-subtract btn btn-default btn-ref"></a>';
            var container = document.getElementById("container");
            container.innerHTML = remuve;
        }
    });
}

 


 Script Code :

 

var test1234 = Class.create();
test1234.prototype = Object.extendsObject(AbstractAjaxProcessor, {
    user_has_group: function(user, group) {
        var gm = new GlideRecord('sys_user_grmember');
        gm.addQuery('group.sys_id', group);
        gm.addQuery('user.sys_id', user);
        gm.query();

        return gm.hasNext();
    },
    type: 'test1234'
});

 


If this solution resolves your query, kindly mark it as the accepted solution and give it a thumbs up.

Thanks,
Subhashis Ratna