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.

UI Action visibility Restriction based on Script include result

Mrman
Tera Guru

Hi All,

I have created a UI action in Scoped Application to show on the Case form when the 1st Task created is moved to Closed state . 

This UI action should only be visible when Step 1 task is closed .  I am making a call to Script Include to query the table and return 'true' Or 'false' .

The Condition used to restrict the visibility of UI action is not working . Could you let me know if I am missing something in my Script .

Below is the UI Action 

find_real_file.png

 

Below is the Script Include :

find_real_file.png

var SteptaskUtils = Class.create();
SteptaskUtils.prototype = {
    initialize: function checktaskclosure(current) {
        var grt = new GlideRecord('sn_hr_core_task');
        grt.addQuery('parent', current.sys_id);
        grt.addQuery('short_description', 'Step1 Task');
        grt.query();
		gs.info('the returned records are :' + grt.getRowCount());
		var gh = grt.state;
        if (gh == '3') {

            return true;
        }
		else{
			
			return false;
		}


    },


    type: 'SteptaskUtils'

};
1 ACCEPTED SOLUTION

hi, could you please test this one

in condition, we will pass object current

new sn_hr_core.SteptaskUtils().checktaskclosure(current);

 

script include as below

var SteptaskUtils = Class.create();
SteptaskUtils.prototype = {
initialize: function(){},

checktaskclosure: function(current) {
gs.info('Into the Script Include:');
var grt = new GlideRecord('sn_hr_core_task');
grt.addQuery('parent', current.sys_id);
grt.addQuery('short_description', 'Step1 Task');
grt.query();
gs.info('the returned records are :' + grt.getRowCount());
if(grt.next()){

var gh = grt.state;
gs.info('the state value is :' + gh);
if (gh == '3') {

return true;
}
else{
gs.info('Into Else:');
return false;
}

}
},


type: 'SteptaskUtils'

};

View solution in original post

22 REPLIES 22

Hi @nataliya_b ,

I pasted your code and getting the below error in line 33, could you please guide me .

find_real_file.png

sorry, missing bracket at the end

this shall work

 

var SteptaskUtils = Class.create();
SteptaskUtils.prototype = {


initialize: function() {},

checktaskclosure: function (sysid) {
gs.info('Into the Script Include:');
var grt = new GlideRecord('sn_hr_core_task');
grt.addQuery('parent', sysid);
grt.addQuery('short_description', 'Step1 Task');
grt.query();
gs.info('the returned records are :' + grt.getRowCount());
if(grt.next()){

var gh = grt.state;
gs.info('the state value is :' + gh);
if (gh == '3') {

return true;
}
else{
gs.info('Into Else:');
return false;
}

}
},


type: 'SteptaskUtils'
};

Hi @nataliya_b ,

I have placed the code but still the 'Add Step2' Ui action is always appearing even before Step 1 task is created . 

The condition is not working to show it only when Step1 is in 'Closed Complete' state.

Also, now it is not calling Script include as I dont see the log messages in logs.

Please guide me

Script Include :

var SteptaskUtils = Class.create();
SteptaskUtils.prototype = {


iinitialize: function() {
	
},

checktaskclosure: function (sysid) {
gs.info('Into the Script Include:');
var grt = new GlideRecord('sn_hr_core_task');
grt.addQuery('parent', sysid);
grt.addQuery('short_description', 'Step1 Task');
grt.query();
gs.info('the returned records are :' + grt.getRowCount());
if(grt.next()){

var gh = grt.state;
gs.info('the state value is :' + gh);
if (gh == '3') {
gs.info('Into if Ture:');
return true;
}
else{
gs.info('Into Else:');
return false;
}

}
},


type: 'SteptaskUtils'
};

hi, could you please test this one

in condition, we will pass object current

new sn_hr_core.SteptaskUtils().checktaskclosure(current);

 

script include as below

var SteptaskUtils = Class.create();
SteptaskUtils.prototype = {
initialize: function(){},

checktaskclosure: function(current) {
gs.info('Into the Script Include:');
var grt = new GlideRecord('sn_hr_core_task');
grt.addQuery('parent', current.sys_id);
grt.addQuery('short_description', 'Step1 Task');
grt.query();
gs.info('the returned records are :' + grt.getRowCount());
if(grt.next()){

var gh = grt.state;
gs.info('the state value is :' + gh);
if (gh == '3') {

return true;
}
else{
gs.info('Into Else:');
return false;
}

}
},


type: 'SteptaskUtils'

};

Hi @nataliya_b ,

Thanks very much , this time it worked . 

I am trying to include another UI action to create Step 3 task .

I have include another function like below , please see Script include . 

This is not working for Step3 task , am I doing it correctly ?

var SteptaskUtils = Class.create();
SteptaskUtils.prototype = {
    initialize: function() {},

    checktaskclosure: function(current) {
        gs.info('Into the Script Include:');
        var grt = new GlideRecord('sn_hr_core_task');
        grt.addQuery('parent', current.sys_id);
        grt.addQuery('short_description', 'Step1 Task');
        grt.query();
        gs.info('the returned records are :' + grt.getRowCount());
        if (grt.next()) {

            var gh = grt.state;
            gs.info('the state value is :' + gh);
            if (gh == '3') {
                gs.info('into if true:');
                return true;
            } else {
                gs.info('Into Else:');
                return false;
            }

        }
    },

checktask2closure: function(current) {
        gs.info('Into the Script Include task2:');
        var grt2 = new GlideRecord('sn_hr_core_task');
        grt2.addQuery('parent', current.sys_id);
        grt2.addQuery('short_description', 'Step2 Task');
        grt2.query();
        gs.info('the returned records are task2:' + grt2.getRowCount());
        if (grt2.next()) {

            var gh2 = grt2.state;
            gs.info('the state value is :' + gh);
            if (gh2 == '3') {
gs.info('into if true task2:');
                return true;
            } else {
                gs.info('Into Else task 2:');
                return false;
            }

        }
    },
    type: 'SteptaskUtils'

};