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 Button - How to hide/show button based on conditions for Demand form

Brandon26
Tera Contributor

I need to hide the Qualify UI action button on a Demand form if the Stakeholder related list is empty, and display a message that at least one stakeholder needs to be added before it can be qualified. 

Does anyone know how I can accomplish this??

Thanks,

Brandon

 

1 ACCEPTED SOLUTION

Sure so you could create a new script include as below:

find_real_file.png

Script:

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

    checkStakeholders: function(dmnd) {
        var stakeholderRelGr = new GlideRecord('dmn_m2m_demand_stakeholder');
        stakeholderRelGr.addQuery('demand', dmnd);
        stakeholderRelGr.query();
        return stakeholderRelGr.hasNext();
    },

    type: 'demandUtils'
};

Then you could set the Condition field on your Qualify UI action as below to conditionally display based on whether there are any stakeholders for the current Demand record:

new global.demandUtils().checkStakeholders(current.getUniqueValue())

And for the message to display at the top you could create a new Display business rule as below:

find_real_file.png

find_real_file.png

Condition:

!new global.demandUtils().checkStakeholders(current.getUniqueValue())

Script:

(function executeRule(current, previous /*null when async*/) {

	// Add your code here
	gs.addInfoMessage('At least one Stakeholder needs to be added before Demand can be qualified');

})(current, previous);

If this answer is helpful please mark correct and helpful!

Regards,

Christopher Perry

If this answer is helpful please mark correct and helpful!

Regards,
Chris Perry

View solution in original post

8 REPLIES 8

chrisperry
Giga Sage

You will need a script include function which returns true/false depending on if there are any Stakeholders for the current Demand record. You can then call the script include function from the UI action's condition field so that it displays based on whether the script include returns true/false.

For displaying the message that at least one stakeholder needs to be added, you can use a Display business rule which references the same script include function from above.

Hopefully that makes sense, I can provide more details if needed.

If this answer is helpful please mark correct and helpful!

Regards,

Christopher Perry

If this answer is helpful please mark correct and helpful!

Regards,
Chris Perry

I'm new to javascripting. How do you write that kind of script include??

 

Sure so you could create a new script include as below:

find_real_file.png

Script:

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

    checkStakeholders: function(dmnd) {
        var stakeholderRelGr = new GlideRecord('dmn_m2m_demand_stakeholder');
        stakeholderRelGr.addQuery('demand', dmnd);
        stakeholderRelGr.query();
        return stakeholderRelGr.hasNext();
    },

    type: 'demandUtils'
};

Then you could set the Condition field on your Qualify UI action as below to conditionally display based on whether there are any stakeholders for the current Demand record:

new global.demandUtils().checkStakeholders(current.getUniqueValue())

And for the message to display at the top you could create a new Display business rule as below:

find_real_file.png

find_real_file.png

Condition:

!new global.demandUtils().checkStakeholders(current.getUniqueValue())

Script:

(function executeRule(current, previous /*null when async*/) {

	// Add your code here
	gs.addInfoMessage('At least one Stakeholder needs to be added before Demand can be qualified');

})(current, previous);

If this answer is helpful please mark correct and helpful!

Regards,

Christopher Perry

If this answer is helpful please mark correct and helpful!

Regards,
Chris Perry

Thanks for all this!

To take this a step further, how can I configure the Qualify UI action button to NOT display if there are no stakeholders??  Maybe using OnClick??