in servicenow, i have a script include named as updatestateofrisk and we are gliderecord…
- User
in servicenow, i have a script include named as updatestateofrisk and we are gliderecord the risk table and then gr.get(sys_id): , gr.state = draft; gr.update();.... my requirement is i need to make one record state to draft, via url by calling the script include, please provide me the URL, how to call this script include
- Assistant
To call a Script Include in ServiceNow via a URL, you can use a Scripted REST API. A Scripted REST API allows you to expose server-side functionality to outside applications that can be accessed via standard HTTP methods (GET, POST, PUT, DELETE). Here's a general guide on how you can achieve that:
Step 1: Create a Scripted REST API
- Navigate to:
System Web Services > Scripted REST APIs - Click on:
Newto create a new Scripted REST API. - Fill in the details:
- Name: Set a name for your API, e.g.,
RiskStateAPI. - API ID: Set an ID, e.g.,
risk_state_api. - Click on
Submit.
- Name: Set a name for your API, e.g.,
Step 2: Create a Resource for the API
-
After creating the API, navigate to the newly created API and click
Newunder the Resources related list. -
Fill in the details for the resource:
- Name: e.g.,
UpdateRiskState - HTTP Method: Choose
POST(since you're making a change to the data). - Path: e.g.,
update_state
- Name: e.g.,
-
In the
Scriptfield, implement the logic to call your Script Include. An example script might look like:(function process(/*RESTAPIRequest*/ request, /*RESTAPIResponse*/ response) { var requestBody = request.body.data; var sysId = requestBody.sys_id; // Assume sys_id is passed in the request body if (!sysId) { response.setStatus(400); response.setBody({ error: "sys_id is required." }); return; } try { var updateStateOfRisk = new UpdateStateOfRisk(); // Replace with your Script Include name updateStateOfRisk.updateState(sysId, 'draft'); // Assume updateState is a method in your Script Include that updates the state response.setStatus(200); response.setBody({ success: true, message: "State updated to Draft." }); } catch (e) { response.setStatus(500); response.setBody({ success: false, error: e.message }); } })(request, response);
Step 3: Modify the Script Include
Ensure your
UpdateStateOfRiskScript Include can be called with a method likeupdateState. For example:var UpdateStateOfRisk = Class.create(); UpdateStateOfRisk.prototype = { initialize: function() { }, updateState: function(sysId, newState) { var gr = new GlideRecord('u_risk'); // Replace with your actual table name if (gr.get(sysId)) { gr.setValue('state', newState); // Assume 'state' is the field to be updated gr.update(); } else { throw new Error("Record not found"); } }, type: 'UpdateStateOfRisk' };Step 4: Call the Scripted REST API via URL
Once you've set up the Scripted REST API, you can call it using an HTTP client of your choice. Here's a sample request using
curl:curl --request POST \ --url 'https://your-instance.service-now.com/api/x_your_scope/risk_state_api/update_state' \ --header 'Content-Type: application/json' \ --header 'Authorization: Basic your_encoded_auth' \ --data '{ "sys_id": "your_record_sys_id_here" }'Replace
https://your-instance.service-now.comwith your ServiceNow instance URL. Replacex_your_scopewith the scope of your Scripted REST API (it usually corresponds to the application scope where you created it).You will need to handle authentication; usually, you can use Basic Auth, OAuth, or another method supported by ServiceNow.
Note
- Ensure the necessary roles and permissions are assigned to the user making the request.
- Test thoroughly, especially in a development or test environment, before deploying changes to production.
This process assumes you have administration rights and the capability to create and deploy REST APIs within your ServiceNow environment.
- Navigate to:
- User
how to run this script include via browser console
- Assistant
Running a Script Include directly from the browser console is not straightforward because Script Includes are server-side scripts. This means they are executed on the server rather than the client (browser). However, you can interact with server-side scripts through client-side scripts by leveraging ServiceNow's GlideAjax class.
Using GlideAjax allows you to call Script Includes from client scripts, which can be executed in the browser console. Here's how you can do it:
Step 1: Ensure Your Script Include is Client-Callable
First, you need to ensure that your Script Include is callable from the client. This means it should extend
AbstractAjaxProcessor.Here's a simple adaptation of your Script Include:
var UpdateStateOfRisk = Class.create(); UpdateStateOfRisk.prototype = Object.create(AbstractAjaxProcessor.prototype); UpdateStateOfRisk.prototype = { initialize: function() { AbstractAjaxProcessor.prototype.initialize.call(this); }, updateState: function() { var sysId = this.getParameter('sys_id'); var newState = this.getParameter('new_state') || 'draft'; // Default to 'draft' if (sysId) { var gr = new GlideRecord('u_risk'); // Replace with your actual table name if (gr.get(sysId)) { gr.setValue('state', newState); gr.update(); return "State updated to " + newState; } else { return "Record not found"; } } return "sys_id is required"; }, type: 'UpdateStateOfRisk' };Step 2: Use GlideAjax to Call Your Script Include from the Browser Console
-
Open your ServiceNow instance in a web browser and log in.
-
Open the browser's developer console:
- Right-click on the page and select
Inspect(orInspect Element). - Go to the
Consoletab.
- Right-click on the page and select
-
Execute the following JavaScript in the console:
var ga = new GlideAjax('UpdateStateOfRisk'); // Ensure this name matches your Script Include ga.addParam('sysparm_name', 'updateState'); // 'updateState' is the function name in the Script Include ga.addParam('sys_id', 'your_record_sys_id_here'); // Replace with the actual Sys ID of the record to be updated ga.addParam('new_state', 'draft'); // This is optional since the default is 'draft' ga.getXMLAnswer(function(response) { console.log(response); // This will log the response from the Script Include });
Important Considerations
- Permissions: Ensure you have the right permissions to update records in your environment.
- Testing: Test this approach in a development environment first to avoid unintentional data updates in production.
- Security: Be mindful of calling server-side code from the client side, particularly in production, to avoid exposing logic that could be manipulated.
This setup allows you to trigger a server-side process from the client side, but remember, direct database changes through client-side scripts should be done carefully, considering all security and performance implications.
-
- User
how to call it in client script
- Assistant
To call a Script Include from a Client Script in ServiceNow, you use the
GlideAjaxclass. Here’s a step-by-step guide to create a Client Script that invokes a Script Include:Step 1: Ensure the Script Include is Client Callable
Ensure your Script Include is designed to be called from a client script. It should extend
AbstractAjaxProcessor:var UpdateStateOfRisk = Class.create(); UpdateStateOfRisk.prototype = Object.create(AbstractAjaxProcessor.prototype); UpdateStateOfRisk.prototype = { initialize: function() { AbstractAjaxProcessor.prototype.initialize.call(this); }, updateState: function() { var sysId = this.getParameter('sys_id'); var newState = this.getParameter('new_state') || 'draft'; if (sysId) { var gr = new GlideRecord('u_risk'); // Use your actual table name if (gr.get(sysId)) { gr.setValue('state', newState); gr.update(); return 'State updated to ' + newState; } else { return 'Record not found'; } } return 'sys_id is required'; }, type: 'UpdateStateOfRisk' };Step 2: Create a Client Script
-
Navigate to:
System Definition > Client Scripts. -
Click on:
Newto create a new client script. -
Configure your client script:
- Name: Provide a name for the client script.
- Table: Select the table where this client script should run (or
Globalif it's not specific to a single table). - Type: Choose when you want the script to run (e.g.,
onLoad,onChange, oronSubmit). - Script: Write the script using
GlideAjaxto call your Script Include.
Here’s an example of a client script:
function onLoad() { var ga = new GlideAjax('UpdateStateOfRisk'); // Make sure this matches your Script Include name ga.addParam('sysparm_name', 'updateState'); // The function name in the Script Include ga.addParam('sys_id', 'your_record_sys_id_here'); // Replace with the actual Sys ID of the record to be updated ga.addParam('new_state', 'draft'); // Optional if default is 'draft' ga.getXMLAnswer(function(response) { alert(response); // Outputs the response from the server }); }Important Considerations
- Selections: Replace
'your_record_sys_id_here'with the actual Sys ID of the record you wish to update. In a real scenario, this might come from a field on the form or a list selection. - Security and Performance: When calling server-side logic from the client, ensure the script is efficient and secure. Avoid exposing sensitive logic or data through client scripts.
- Debugging: Use
console.log()for debugging if needed, and test your scripts in a non-production environment first.
Testing
- Validate by opening a form or performing the intended action that triggers the client script. Check the results to ensure the state is updated correctly.
This setup allows you to leverage the server-side capabilities of Script Includes from the client side, enabling powerful interactions and updates directly from the user interface.
-