Pass parameters from script include to a script action

fabrizio95360
Tera Expert

I need to move this function of my script include into a script action. but I do not know how I can do it. This function has 5 parameters. How can I make sure I pass all 5?

 

my function in script include: 

updateConsistencyResult: function(group_mdef_ids, not_consistent_entity_ids, consistent_entity_ids, num_not_consistent_entities, num_consistent_entities) {
        var mdt_not_consistent = new GlideRecord('sn_grc_metric_data_task');
        mdt_not_consistent.addEncodedQuery('metric.base_metric_definition.sys_idIN' + group_mdef_ids + '^metric.profile.sys_idIN' + not_consistent_entity_ids);
        mdt_not_consistent.orderByDesc('due_date');
        mdt_not_consistent.setLimit(num_not_consistent_entities);
        mdt_not_consistent.query();
        mdt_not_consistent.u_consistency_check_result = 'not_consistent';
        mdt_not_consistent.updateMultiple();
        var mdt_consistent = new GlideRecord('sn_grc_metric_data_task');
        mdt_consistent.addEncodedQuery('metric.base_metric_definition.sys_idIN' + group_mdef_ids + '^metric.profile.sys_idIN' + consistent_entity_ids);
        mdt_consistent.orderByDesc('due_date');
        mdt_consistent.setLimit(num_consistent_entities);
        mdt_consistent.query();
        mdt_consistent.u_consistency_check_result = 'consistent';
        mdt_consistent.updateMultiple();
    },
1 ACCEPTED SOLUTION

shloke04
Kilo Patron

Hi @fabrizio95360 ,

 

You can make use of an JSON object to pass your parameters. Please see steps below to achieve this:

 

1. Create an event by navigating to "Registry" module and fill in the details on the event registration page.

2. Updated Script Include below:

Script Include:

updateConsistencyResult: function(group_mdef_ids, not_consistent_entity_ids, consistent_entity_ids, num_not_consistent_entities, num_consistent_entities) {
        
		var myObject = {groupMdef:group_mdef_ids,notConsistent:not_consistent_entity_ids,consisEntity:consistent_entity_ids,numNot:num_not_consistent_entities,numConsistent:num_consistent_entities};
        gs.eventQueue('Event Name', null, new global.JSON().encode(myObject), '');

},

Script Action:

var myObject = new global.JSON().decode(event.parm1);
		
		var mdt_not_consistent = new GlideRecord('sn_grc_metric_data_task');
        mdt_not_consistent.addEncodedQuery('metric.base_metric_definition.sys_idIN' + myObject.groupMdef + '^metric.profile.sys_idIN' + myObject.notConsistent);
        mdt_not_consistent.orderByDesc('due_date');
        mdt_not_consistent.setLimit(myObject.numNot);
        mdt_not_consistent.query();
        mdt_not_consistent.u_consistency_check_result = 'not_consistent';
        mdt_not_consistent.updateMultiple();
        var mdt_consistent = new GlideRecord('sn_grc_metric_data_task');
        mdt_consistent.addEncodedQuery('metric.base_metric_definition.sys_idIN' + myObject.groupMdef + '^metric.profile.sys_idIN' + myObject.consisEntity);
        mdt_consistent.orderByDesc('due_date');
        mdt_consistent.setLimit(myObject.numConsistent);
        mdt_consistent.query();
        mdt_consistent.u_consistency_check_result = 'consistent';
        mdt_consistent.updateMultiple();
Hope this helps. Please mark the answer as correct/helpful based on impact.

Regards,
Shloke

View solution in original post

4 REPLIES 4

Fabian Kunzke
Kilo Sage
Kilo Sage

Hey,

 

i don't like the way this is scripted, but thats probably more for an architectual discussion. To answer your question:

You can pass an object as a parameter:

// build the object
var object = {
"group_mdef_ids": group_mdef_ids, 
"not_consistent_entity_ids": not_consistent_entity_ids,
"consistent_entity_ids": consistent_entity_ids, 
"num_not_consistent_entities": num_not_consistent_entities, 
"num_consistent_entities": num_consistent_entities
};

When triggering an event, you can pass this object then as an event parameter. You might have to parse it through a JSON conversion (encode as string, then parse to get the object again), but this way you can pass multiple parameters as one object.

 

Regars

Fabian

shloke04
Kilo Patron

Hi @fabrizio95360 ,

 

You can make use of an JSON object to pass your parameters. Please see steps below to achieve this:

 

1. Create an event by navigating to "Registry" module and fill in the details on the event registration page.

2. Updated Script Include below:

Script Include:

updateConsistencyResult: function(group_mdef_ids, not_consistent_entity_ids, consistent_entity_ids, num_not_consistent_entities, num_consistent_entities) {
        
		var myObject = {groupMdef:group_mdef_ids,notConsistent:not_consistent_entity_ids,consisEntity:consistent_entity_ids,numNot:num_not_consistent_entities,numConsistent:num_consistent_entities};
        gs.eventQueue('Event Name', null, new global.JSON().encode(myObject), '');

},

Script Action:

var myObject = new global.JSON().decode(event.parm1);
		
		var mdt_not_consistent = new GlideRecord('sn_grc_metric_data_task');
        mdt_not_consistent.addEncodedQuery('metric.base_metric_definition.sys_idIN' + myObject.groupMdef + '^metric.profile.sys_idIN' + myObject.notConsistent);
        mdt_not_consistent.orderByDesc('due_date');
        mdt_not_consistent.setLimit(myObject.numNot);
        mdt_not_consistent.query();
        mdt_not_consistent.u_consistency_check_result = 'not_consistent';
        mdt_not_consistent.updateMultiple();
        var mdt_consistent = new GlideRecord('sn_grc_metric_data_task');
        mdt_consistent.addEncodedQuery('metric.base_metric_definition.sys_idIN' + myObject.groupMdef + '^metric.profile.sys_idIN' + myObject.consisEntity);
        mdt_consistent.orderByDesc('due_date');
        mdt_consistent.setLimit(myObject.numConsistent);
        mdt_consistent.query();
        mdt_consistent.u_consistency_check_result = 'consistent';
        mdt_consistent.updateMultiple();
Hope this helps. Please mark the answer as correct/helpful based on impact.

Regards,
Shloke

@fabrizio95360  - Please do cross check the correct object names in your Script action as defined within your Script include as provided by me, just to be absolutely sure.

Let me know how this goes.

Hope this helps. Please mark the answer as correct/helpful based on impact.

Regards,
Shloke

Rajdeep Ganguly
Mega Guru


To move your function from a script include to a script action, you can follow these steps:

1. Navigate to System Policy > Script Actions in your ServiceNow instance.
2. Click on New to create a new script action.
3. Fill in the necessary fields such as Name, Table, and When to run.
4. In the Script field, paste your function and modify it to accept parameters from the current record. You can access the current record using the current object.
5. To pass the parameters, you can either hard code them in the script or use fields from the current record.

Here is a sample code for your script action:

javascript
(function executeRule(current, previous /*null when async*/) {
var group_mdef_ids = current.group_mdef_ids;
var not_consistent_entity_ids = current.not_consistent_entity_ids;
var consistent_entity_ids = current.consistent_entity_ids;
var num_not_consistent_entities = current.num_not_consistent_entities;
var num_consistent_entities = current.num_consistent_entities;

var mdt_not_consistent = new GlideRecord('sn_grc_metric_data_task');
mdt_not_consistent.addEncodedQuery('metric.base_metric_definition.sys_idIN' + group_mdef_ids + '^metric.profile.sys_idIN' + not_consistent_entity_ids);
mdt_not_consistent.orderByDesc('due_date');
mdt_not_consistent.setLimit(num_not_consistent_entities);
mdt_not_consistent.query();
while (mdt_not_consistent.next()) {
mdt_not_consistent.u_consistency_check_result = 'not_consistent';
mdt_not_consistent.update();
}

var mdt_consistent = new GlideRecord('sn_grc_metric_data_task');
mdt_consistent.addEncodedQuery('metric.base_metric_definition.sys_idIN' + group_mdef_ids + '^metric.profile.sys_idIN' + consistent_entity_ids);
mdt_consistent.orderByDesc('due_date');
mdt_consistent.setLimit(num_consistent_entities);
mdt_consistent.query();
while (mdt_consistent.next()) {
mdt_consistent.u_consistency_check_result = 'consistent';
mdt_consistent.update();
}
})(current, previous);


Please note that you need to replace the current.group_mdef_ids, current.not_consistent_entity_ids, etc. with the actual field names from your record.


nowKB.com

For asking ServiceNow-related questions try this :
For a better and more optimistic result, please visit this website. It uses a Chat Generative Pre-Trained Transformer ( GPT ) technology for solving ServiceNow-related issues.
Link - https://nowgpt.ai/

For the ServiceNow Certified System Administrator exams try this :
https://www.udemy.com/course/servicenow-csa-admin-certification-exam-2023/?couponCode=NOW-DEVELOPER