Hide UI Macro icon using onLoad Client script.

ar1
Kilo Sage

Hi All,

We created one UI Macro to display the approver's delegates and it's working fine.
But now we want to hide the UI Macro if the approver don't have the delegates.

We gone through the old threads and tried few possibilities to hide the macro, But no luck.

Can anyone please help us hide the Marco using onLoad client script.

 

Ui Macro Script:
Name: delegates_approver

 

<?xml version="1.0" encoding="utf-8" ?>
<j:jelly trim="false" xmlns:j="jelly:core" xmlns:g="glide" xmlns:j2="null" xmlns:g2="null">

<g:evaluate var="jvar_guid" expression="gs.generateGUID(this);" />
<j:set var="jvar_n" value="show_incidents_${jvar_guid}:${ref}"/>

<g:reference_decoration id="${jvar_n}" field="${ref}"
onclick="showRelatedList('${ref}'); "
title="${gs.getMessage('Show delegates')}" class="btn btn-ref btn-default hide_hosted_asset_flag" image="images/icons/tasks.gifx" icon="icon-tree-right"/>


<script>
function showRelatedList(reference) {
var w = new GlideDialogWindow('show_list');
w.setTitle('Delegate Users');
w.setPreference('table', 'sys_user_delegate_list');
w.setPreference('sysparm_view', 'default');
//Set the query for the list
var approver = g_form.getValue('approver');

var usr = 'user=' + approver + '^approvals=true' + '^delegate.active=true';

w.setPreference('sysparm_query', usr);
w.render();
}
</script>
</j:jelly>

 

Hi @RaghavSh 

Could you please help us here.

Advance thanks.

1 ACCEPTED SOLUTION

@ar1 the code worked perfectly fine for me pasting it again:

client:

function onLoad() {
//Type appropriate comment here, and begin script below

alert('one');

//var usr = g_form.getReference('approver');


var gr = new GlideAjax('approverDelegationsCheck');
gr.addParam('sysparm_name','getDelegates');
gr.addParam('sysparm_usr',g_form.getValue('approver'));
gr.getXML(callbackfunction);
} // this bracket is the onload function bracket, it should close here , check your code.
function callbackfunction(response)
{
var answer = response.responseXML.documentElement.getAttribute("answer");
alert(answer);
if(answer==0){
alert('inside loop');

gel("test").style.display="none"; //make sure isolate script of your client script is false

}

}

 

SI:

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

getDelegates: function()
{
gs.log("raghav111");
var usr = this.getParameter('sysparm_usr');
var app = new GlideRecord('sys_user_delegate');
app.addQuery('user',usr);
app.addQuery('approvals',true);
app.query();
return app.getRowCount();
// while(app.next())
// {
// var delUsr = app.getValue('delegate');
// //var delUsr = app.getRowCount();
// return delUsr;
// }
},


type: 'approverDelegationsCheck'
});

 

I think you should paste the screenshots of codes. Also I have put logs, check if you are getting this log or any error when you load the approval page.


Raghav
MVP 2023

View solution in original post

34 REPLIES 34

Hi Raghav,
Thanks for the response.

Apologies for the mistakes in Client script, now we corrected but no luck.
After the changes also still only first alert is popping up.

Just for reference i'm posting the code.
Could you please check once.
Many thanks for the continuous support.

CS: Onload

function onLoad() {
//Type appropriate comment here, and begin script below

alert('one');

//var usr = g_form.getReference('approver');


var gr = new GlideAjax('approverDelegationsCheck');
gr.addParam('sysparm_name','getDelegates');
gr.addParam('sysparm_usr',g_form.getValue('approver'));
gr.getXML(callbackfunction);
} // this bracket is the onload function bracket, it should close here , check your code.
function callbackfunction(response)
{
var answer = response.responseXML.documentElement.getAttribute("answer");
alert(answer);
if(answer==0){
alert('inside loop');

gel("test").style.display="none"; //make sure isolate script of your client script is false

}

}

Script Include:
Name: approverDelegationsCheck

Client callable  == 

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

getDelegates: function()
{
var usr = this.getParameter('sysparm_usr');
var app = new GlideRecord('sys_user_delegate');
app.addQuery('user',usr);
app.addQuery('approvals',true);
app.query();
return app.getRowCount();
// while(app.next())
// {
// var delUsr = app.getValue('delegate');
// //var delUsr = app.getRowCount();
// return delUsr;
// }
},


type: 'approverDelegationsCheck'
});


UI Macro:


<?xml version="1.0" encoding="utf-8" ?>
<j:jelly trim="false" xmlns:j="jelly:core" xmlns:g="glide" xmlns:j2="null" xmlns:g2="null">

<div id = "test">

<g:evaluate var="jvar_guid" expression="gs.generateGUID(this);" />
<j:set var="jvar_n" value="show_incidents_${jvar_guid}:${ref}"/>

<g:reference_decoration id="${jvar_n}" field="${ref}"
onclick="showRelatedList('${ref}'); "
title="${gs.getMessage('Show delegates')}" class="btn btn-ref btn-default hide_hosted_asset_flag" image="images/icons/tasks.gifx" icon="icon-tree-right"/>

</div>


<script>
function showRelatedList(reference) {
var w = new GlideDialogWindow('show_list');
w.setTitle('Delegate List');
w.setPreference('table', 'sys_user_delegate_list');
w.setPreference('sysparm_view', 'default');
//Set the query for the list
var approver = g_form.getValue('approver');
//var act = g_form.getValue('approvals');
<!-- //var query = 'user=' + approver + '^approvals=true' + '^delegate.active=true' + '^starts<=javascript&colon;gs.daysAgoEnd(0)^ends>javascript&colon;gs.daysAgoStart(0)';
-->
var user = 'user=' + approver + '^approvals=true' + '^delegate.active=true';

w.setPreference('sysparm_query', user);
//Open the popup
w.render();
}
</script>
</j:jelly>




Advance thanks.

do delegate table have a records  where user field( delegate table) is same as approver field (approval table).


Raghav
MVP 2023

Hi Raghav,
Thanks for the response.
Yes, delegate table have the records, 
and Yes and user field user and approver both are same.

We checked few records.
Did you tried the above codes in your PDI. and are you able to achieve the requirement.
Could you please help us.

Advance thanks.

@ar1 the code worked perfectly fine for me pasting it again:

client:

function onLoad() {
//Type appropriate comment here, and begin script below

alert('one');

//var usr = g_form.getReference('approver');


var gr = new GlideAjax('approverDelegationsCheck');
gr.addParam('sysparm_name','getDelegates');
gr.addParam('sysparm_usr',g_form.getValue('approver'));
gr.getXML(callbackfunction);
} // this bracket is the onload function bracket, it should close here , check your code.
function callbackfunction(response)
{
var answer = response.responseXML.documentElement.getAttribute("answer");
alert(answer);
if(answer==0){
alert('inside loop');

gel("test").style.display="none"; //make sure isolate script of your client script is false

}

}

 

SI:

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

getDelegates: function()
{
gs.log("raghav111");
var usr = this.getParameter('sysparm_usr');
var app = new GlideRecord('sys_user_delegate');
app.addQuery('user',usr);
app.addQuery('approvals',true);
app.query();
return app.getRowCount();
// while(app.next())
// {
// var delUsr = app.getValue('delegate');
// //var delUsr = app.getRowCount();
// return delUsr;
// }
},


type: 'approverDelegationsCheck'
});

 

I think you should paste the screenshots of codes. Also I have put logs, check if you are getting this log or any error when you load the approval page.


Raghav
MVP 2023

Hi Raghava,
Many many thanks for your continuous support.
We copied and paste the code then it's working fine as per the requirement.
One last doubt:
Could you please let us know how to check the delegate users start  and end dates are can't be in past dates and also below scenario's we need to check

  • start before current date/time
  • ends after current date/time

We added below line in UI Macro script level, but some reasons it's not properly checking the start and end dates for delegate users.

//var user = 'user=' + approver + '^approvals=true' + '^delegate.active=true' + '^starts<=javascript&colon;gs.daysAgoEnd(0)^ends>javascript&colon;gs.daysAgoStart(0)';

Advance thanksss.