top of page

Experiencing challenges with ServiceNow support?

IKC provides professional, reliable assistance that covers gaps not supported by ServiceNow

and without the high costs of traditional services.

 

Starting from just $1,000

Calling Script Includes from UI Action Conditions in ServiceNow: A Practical Guide

Updated: Mar 30

Are you trying to control the visibility or behavior of UI Actions in ServiceNow based on complex logic? Leveraging Script Includes from the "Condition" field of a UI Action is a powerful technique. This article will guide you through the process, explaining common pitfalls and providing a clear, step-by-step solution.


Understanding the Challenge


ServiceNow UI Actions provide buttons, links, and context menu items that allow users to interact with records. The "Condition" field determines whether a UI Action is displayed for a specific user and record. While simple conditions can be written directly in this field, more complex logic often requires using a Script Include for better organization and reusability.


One common issue ServiceNow developers face is difficulty calling a Script Include properly from the UI Action's condition. You might encounter situations where the Script Include isn't executed, or the UI Action appears for all users regardless of the intended condition. This typically stems from syntax errors or misunderstandings about how the condition field is evaluated.


Common Causes and Troubleshooting


Several factors can prevent your Script Include from executing correctly within the UI Action condition:


  • Incorrect Syntax: The most frequent problem is using incorrect syntax when calling the Script Include. The condition field exects a JavaScript expression that evaluates to true or false.


  • Missing Parentheses: A prevalent error is forgetting the parentheses when instantiating your Script Include class. This prevents the Script Include from being properly initialized and its methods from being accessed.


  • Unnecessary "javascript:" Prefix: The "Condition" field is already evaluated as JavaScript, so adding the "javascript:" prefix is redundant and can lead to errors.


  • Logic Errors in Script Include: The Script Include itself may contain logical errors, preventing it from returning the expected true or false value. Always thoroughly test your Script Include independently to ensure it functions as intended.


Step-by-Step Solution


Here's how to correctly call a Script Include from a UI Action's "Condition" field:


  1. Create your Script Include: Develop a Script Include that contains the logic for determining whether the UI Action should be visible. Make sure the Script Include is active.

var MyConditionChecker = Class.create();
MyConditionChecker.prototype = {
	initialize: function() {
	},
	
	shouldShowAction: function() {
		// Your logic here. For example:
		var user = gs.getUser();
		if (user.isMemberOf('Some Group')) {
			return true;
		} else {
			return false;
		}
	},
	
	type: 'MyConditionChecker'
};

  1. Call the Script Include from the UI Action: In the "Condition" field of your UI Action, use the following syntax:

     new MyConditionChecker().shouldShowAction();

  • new MyConditionChecker(): This instantiates your Script Include class. Do not forget the parentheses!

  • .shouldShowAction(): This calls the specific function within your Script Include that evaluates the condition.


  1. Avoid Client Callable (Usually): In most cases, you do not need to check the "Client Callable" box on your Script Include if you are calling it from the condition field. The condition is evaluated on the server-side. Only check this box if you intend to call the Script Include from client-side code.


Practical Example


Let's say you want a "Close Task" UI Action to be visible only to users who are either the assigned user or a delegate of the assigned user. You could create a Script Include like this:

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

    isDelegateOrAssignee: function() {
        var assignedTo = current.assigned_to;
        var loggedInUser = gs.getUserID();

        if (assignedTo == loggedInUser) {
            return true; // User is the assignee
        }

        var grDelegate = new GlideRecord("sys_user_delegate");
        grDelegate.addQuery("delegate", loggedInUser);
        grDelegate.addQuery("user", assignedTo); 
		//Check if the logged in user is a delegate of the assignee.
        grDelegate.addQuery("assignments", "true");
        grDelegate.addQuery("starts", "<=", gs.daysAgo(0));
        grDelegate.addQuery("ends", ">=", gs.daysAgo(0));
        grDelegate.query();

        return grDelegate.hasNext();
		// Returns true if a delegate record is found
    },

    type: 'TaskDelegateChecker'
};
    

Then, in the UI Action's "Condition" field, you would use:

      new TaskDelegateChecker().isDelegateOrAssignee();
    

Alternative Solutions


While Script Includes are a great way to reuse logic in ServiceNow, you can avoid a Script Include altogether if you can put all your logic in the condition field directly. This could be useful when you don't need to reuse this logic, or the logic itself isn't that complex.


Conclusion


Calling Script Includes from UI Action conditions is a powerful technique for customizing ServiceNow behavior. By understanding the common pitfalls and following the steps outlined above, you can effectively control UI Action visibility and create a more user-friendly and efficient ServiceNow experience. Remember to pay close attention to syntax and always test your logic thoroughly.


Next Steps


  1. Review your existing UI Actions and identify opportunities to use Script Includes for more complex conditions.

  2. Practice writing Script Includes and calling them from UI Action conditions.

  3. Explore other ways to use Script Includes in ServiceNow, such as in Business Rules and Client Scripts.

Experiencing challenges with ServiceNow support?

IKC provides professional, reliable assistance that covers gaps not supported by ServiceNow

and without the high costs of traditional services.

 

Starting from just $1,000

CONTACT

New Zealand HQ

Integrated Knowledge Consulting Office

Level 3, 93 Grafton Road

Auckland

South Korea

Integrated Knowledge Consulting Office

BMY역삼타워 6층

서울특별시 강남구 역삼동 678-10번지

 

info@ikconsulting.com

Thanks for submitting!

  • LinkedIn Social Icon

© Copyright 2025 Integrated Knowledge Consulting. All rights reserved.

bottom of page