Can we update a record in ATF using run server side script?

Siddhant4
Kilo Contributor

Hi, I had a question regarding ATF.
I have a catalog task with a variable in variable set named 'Next Step'. I need to update the variable in order to close the task, but I'm not sure as to how exactly to update the record.
The variable doesn't come from the RITM, therefore I cannot simply use 'Set Variable Value' step as the variable won't show. I thought of using 'run server side script' but the script doesn't update the variable on sc_task. Below script:

(function(outputs, steps, stepResult, assertEqual) {
// add test script here
var STEP_15_SYS_ID= 'd9f8dd67db805410bea7550a48961930'; // step sys_id


var step_16_output = steps(STEP_15_SYS_ID);

var grtask = new GlideRecord('sc_task');
grtask.addQuery('request_item',step_16_output.first_record);
grtask.query();
if(grtask.next()){
gs.log("Found task : " + grtask.number);
gs.print(grtask.request_item.variables.next_step);
grtask.request_item.variables.next_step='Solution Approved, Build Required';
grtask.update();
outputs.table = 'sc_task';
outputs.record_id = grtask.sys_id;
}

})(outputs, steps, stepResult, assertEqual);

Now, I'm not sure how to go about the problem. Is this possible through ATF?

1 ACCEPTED SOLUTION

Ankur Bawiskar
Tera Patron
Tera Patron

Hi,

So I assume Step 15 is record query and it checks RITM record; so update code as below in Run Server Script step

you need to return the step result whether success or failure and return statement

(function(outputs, steps, stepResult, assertEqual) {
// add test script here
var STEP_15_SYS_ID= 'd9f8dd67db805410bea7550a48961930'; // step sys_id


var step_16_output = steps(STEP_15_SYS_ID).first_record;

var message = '' + step_16_output;

var grtask = new GlideRecord('sc_task');
grtask.addQuery('request_item',step_16_output);
grtask.query();
if(grtask.next()){
gs.info("Found task : " + grtask.number);
gs.info(grtask.request_item.variables.next_step);
grtask.request_item.variables.next_step='Solution Approved, Build Required';
grtask.update();

outputs.table = 'sc_task';
outputs.record_id = grtask.sys_id;

message = message + 'Found Task ' + grtask.number;

stepResult.setOutputMessage(message);

stepResult.setSuccess();
return true;
}

})(outputs, steps, stepResult, assertEqual);

Mark Correct if this solves your issue and also mark 👍 Helpful if you find my response worthy based on the impact.
Thanks
Ankur

Regards,
Ankur
Certified Technical Architect  ||  9x ServiceNow MVP  ||  ServiceNow Community Leader

View solution in original post

14 REPLIES 14

Hi,
This is the whole script

(function(outputs, steps, stepResult, assertEqual) {
    // add test script here
	var STEP_7_SYS_ID= 'd9e5d5efdb405410bea7550a48961999';   // step sys_id
	
	
	var step_14_output = steps(STEP_7_SYS_ID).first_record;
	var message = '' + step_14_output;
			
	var grtask = new GlideRecord('sc_task');
	grtask.addQuery('request_item',step_14_output);
	grtask.query();
	if(grtask.next()){
		gs.log("Found task : " + grtask.number);
		gs.log("Found 1 variable : "+ grtask.request_item.variables.next_step);
		gs.log("Found 2 variable : "+ grtask.request_item.u_validation_details);
		grtask.request_item.variables.next_step='Solution Approved, Build Required';
		grtask.request_item.u_validation_details='Details';
		grtask.update();
		gs.log("Row Count : "+ grtask.getRowCount());
		gs.log("Found 1 variable : "+ grtask.request_item.variables.next_step);
		gs.log("Found 2 variable : "+ grtask.request_item.u_validation_details);
		outputs.table = 'sc_task';
		outputs.record_id = grtask.sys_id;
		message = message + 'Found Task ' + grtask.number;
		stepResult.setOutputMessage(message);
		stepResult.setSuccess();
		return true;
	}
			
})(outputs, steps, stepResult, assertEqual);

And the test result is this
find_real_file.png

Hi,

if you see in the message the sys_id of the catalog task you found is different than the one you have opened using Open an Existing Record step

Have you used output of Run Server Side Script properly in the next step to open catalog task

Regards
Ankur

Regards,
Ankur
Certified Technical Architect  ||  9x ServiceNow MVP  ||  ServiceNow Community Leader

Hi Ankur, 

The first id that is logged, is the id of my requested item and not the catalog task.

Hi Siddhant,

ok got it. so when you use Open an Existing Record and trying to open the TASK; you are saying variables are not updated?

Regards
Ankur

Regards,
Ankur
Certified Technical Architect  ||  9x ServiceNow MVP  ||  ServiceNow Community Leader

Yes, I'm opening the catalog task as to check whether the variables have been updated. But the variables aren't getting updated.