create on change catalog client script if this codes conditons match then a popup will…
- User
create on change catalog client script if this codes conditons match then a popup will show with message var relGlide = new GlideRecord("cmdb_rel_ci); var queryString = "type=d93304fb30854943^child.ip_address=" +current.variables.ip_address; relGlide.addEncodedQuery(queryString); relGlide.query(); workflow.scratchpad.vm_flag = false; while (relGlide.next()){ //if any CI is operational if(relGlide.parent.operational_status ==1){ workflow.scratchpad.vm_flag = true; } }
- Assistant
To implement a client script that shows a popup if conditions within the provided server-side code snippet match, you will need to adapt the server-side logic into something that can interact with the client-side environment. Since client scripts execute in the user's browser and do not directly interact with the server-side database, the process involves sending a request to the server to perform the database checks and then handling the response to display the popup.
ServiceNow offers various ways to achieve this, including using GlideAjax to call a Script Include that performs server-side computations and returns a result to the client script. Below is a step-by-step solution to implement your requirements:
Step 1: Script Include
First, create a Script Include that will perform the server-side logic.
Name:
CheckCIStatusClient Callable:Truevar CheckCIStatus = Class.create(); CheckCIStatus.prototype = Object.extendsObject(AbstractAjaxProcessor, { checkOperationalStatus: function() { var ip_address = this.getParameter('ip_address'); // Get IP address from client script var relGlide = new GlideRecord('cmdb_rel_ci'); var queryString = "type=d93304fb30854943^child.ip_address=" + ip_address; relGlide.addEncodedQuery(queryString); relGlide.query(); while (relGlide.next()) { //if any CI is operational if (relGlide.parent.operational_status == 1) { return "true"; // Return "true" if any CI is operational } } return "false"; // Return "false" if no CI is operational } });Step 2: Client Script
Next, create an onChange Catalog Client Script that calls the Script Include when a specific condition is met, such as a modification in the relevant field.
Type:
onChangeField Name: The field that triggers this script (e.g., 'variables.ip_address' if that is the variable name)function onChange(control, oldValue, newValue, isLoading, isTemplate) { if (isLoading || newValue === '') { return; } // Call the Script Include var ga = new GlideAjax('CheckCIStatus'); ga.addParam('sysparm_name', 'checkOperationalStatus'); ga.addParam('ip_address', newValue); // pass the new value of the IP address field ga.getXML(checkResponse); // Handle the response function checkResponse(response) { var answer = response.responseXML.documentElement.getAttribute("answer"); if (answer === "true") { // Show popup if the condition is met alert("A related CI is operational."); } } }In this solution, the client script detects changes to the specified field, and upon a change, it sends an asynchronous request to the server with the new IP address. The server processes this request using the defined Script Include, checking the operational status of related CIs. Once a response is received, the client script shows a popup alert if the condition (any related CI being operational) is met.
This method ensures a separation of client and server logic while leveraging ServiceNow's capabilities to interact between the client and server sides efficiently.