Script Action and Example

Script Actions

The Script actions feature allows to create server side scripts that can run on server side and perform multiple tasks asynchronously. It will run or execute after the event has triggered in the system scheduler. For example: modifying records, 

 

A script action can only be triggered by an event using gs.eventQueue() method. 

Script actions are maintained in the System Policy —> Script Actions [sysevent_script_action] table in ServiceNow.

 

Available Fields

For Script actions we have these fields.

  • Name: Add a unique name for a script action. Generally to maintain script actions we should maintain the naming conventions. 

Example: Let's take example, we have to create a script action for Close incidents/Problems when Change request gets closed, so name will be Incident-Probelm Closure

  • Application: The application that contains this script. Example: Global or scoped application name.
  • Event name: We have to select an event through which this script actions will be executed. If event is not there in event list we have to register the vent frist in event registry.
  • Active: Check box used to make it active/ disable the scripts. Note: It is unselected by default.
  • Execution Order: The order in which the script will be executed.
  • Condition script: script the conditions under which the Script Action should execute..
  • Script - script the logic to execute when the Script Action runs.

There are two objects.

  • event: The event that caused this to be invoked.

We can get the event.param1 or event.param2 parameters in scripts.

  • current: the event scheduled on behalf of (incident for example).

 

Procedure to Create Script Actions

First we need to create an event. If we want to use existing events then fine, otherwise create a new event in the event registry. For this go to the event registry and click create a new.

We have to give a unique event name for the event and description.

In scoped application, we have to give a unique suffix and then event name will be auto-populated with application scope name and the given suffix.

Select the table name and we can mention the Fired by as business rule or any other Server side scripts name by which event will be fired/triggered.

 

Next steps to create Script actions:

Navigate to application navigator and search Script action or type sysevent_script_action.list and hit enter. You can see the existing available Script actions in your instance. Now click a New button to add a new script action.

Use Case scenario:

Let's create a Script action for Auto-close the related incident, incident tasks and Problems when the Change request gets closed after successful implementations.

 

Event name: change.request.state.closed

Table name: Change request [change_request]

 

Name: Auto-close the CHG related records

Description: Auto-close the change request related records.

 

Script:

autoCloseIncidents();

function autoCloseIncidents() {

	var chg_sysId = event.parm1.toString();
	var grCHGRecord = new GlideRecord('change_request');
	grCHGRecord.addQuery('sys_id=' + chg_sysId);
	grCHGRecord.query();
	if (grCHGRecord.next()) {

		var grIncident = new GlideRecord('incident');
		grIncident.addQuery('rfc=' + grCHGRecord.getUniqueValue());
		grIncident.query();
		while (grIncident.next()) {

			grIncident.state = '6'; // resolved value
			grIncident.close_code = 'Solved (Work Around)';
			grIncident.close_notes = 'Automatically closed due to the associated change request,' + grCHGRecord.getValue('number') + ', being resolved.';
			grIncident.setWorkflow(false);
			grIncident.autoSysFields(false);
			grIncident.update();
		}

		var grProblem = new GlideRecord('problem');
		grProblem.addQuery('rfc=' + grCHGRecord.getUniqueValue());
		grProblem.query();
		while (grProblem.next()) {

			grProblem.state = '106'; // resolved value
			grProblem.resolution_code = 'fix_applied';
			grProblem.fix_notes = 'Auto-closed as related change request get closed';
			grProblem.setWorkflow(false);
			grProblem.autoSysFields(false);
			grProblem.update();
		}
	}
}

 

Fill out this information and click the Save button.

 

Business rule:

Condition:

State CHANGES_TO Closed


Scripts:

 

gs.eventQueue('change.request.state.closed', current, current.getUniqueValue());

 

 

 

Now onwards, When change requests are marked as closed, they will trigger an event that automatically closes the associated Incidents and Problems.

 

Conclusion:

In this way we can use the Script's actions as per need.

 

Some Useful Links

Script actions 

Create a Script to Respond to the Event

 

Feel free to mark helpful and bookmark this article.

Thanks,

Sagar Pagar

Comments

Popular posts from this blog

#BuildWithCreatorStudio Challenge

System Properties & it's Usage in ServiceNow