The CreatorCon Call for Content is officially open! Get started here.

Automated Testing Framework (ATF) & Server Side Script

leochavez
Tera Guru

Within a ATF that I've created, I've managed to successfully extract the sysID of a record within the 'sys_user_grmember' table.  Test results show the following:

Step 8 (Record Query):
    Successfully found 2 sys_user_grmember records matching query:
    Group Group Name = Group A

Step 9 (Log):
    Group Member record sysID: 0cc18cacdb4fd340418f5840cf961905

Step 9 verifies that Step 8 is returning the sysID of the first queried record.  That said, I created a Step 10 and configured it to run the Server Side Script:

	var previousStepSysID = '707ffb41dbc3334058c12c8605961966';
	var grpMbrGR = new GlideRecord('sys_user_grmember');
	if (grpMbrGR.get(steps(previousStepSysID).record_id)) {
		outputs.table = 'sys_user_grmember';
		outputs.record_id = grpMbrGR.user.sys_id; // group member sysID
		gs.print("outputs: " + outputs.record_id);
		
		if (outputs.record_id) {
			stepResult.setOutputMessage("Group member found: " + grpMbrGR.user.name + "(" + grpMbrGR.user.sys_id + ")");
			return true;
		}
		
		stepResult.setOutputMessage("No group member record exists.  Verifiy sysID for previous step.");
		return false;
	}

The script above is my first attempt at using a Server Side Script and I'm sure I'm overlooking something, but the goal is to use sysID from Step 8, read the 'sys_user_grmember' and get the group member user sysID.  Test logs do not show any output for Step 10.

Is this the right approach?

- Leo

 

9 REPLIES 9

leochavez
Tera Guru

After opening a ticket with ServiceNow HI, a SME proposed some code, but the test failed after I ran it.  See code and output below.

PROPOSED CODE:
---------------------

var previousStepSysID = '707ffb41dbc3334058c12cxxxxxxxxxx';
gs.info("previousStepSysID: '" + previousStepSysID + "'");
var groupMbrSysID = steps(previousStepSysID).record_id;  // <-- NOT WORKING (UNDEFINED)
gs.info("groupMbrSysID: '" + groupMbrSysID + "'"); 
 
var grpMbrGR = new GlideRecord('sys_user_grmember');
grpMbrGR.get(groupMbrSysID);
gs.info("is valid record: '" + grpMbrGR.isValidRecord() + "'");

PROPOSED CODE OUTPUT:
---------------------

No group member record exists. Verifiy sysID for previous step.
Full logging from step execution:
08:38:59 AM.218: Loading script: jasmine_lib/jasmine_custom.js
08:38:59 AM.256: : previousStepSysID: '707ffb41dbc3334058c12cxxxxxxxxxx'
08:38:59 AM.257: : groupMbrSysID: 'undefined'
08:38:59 AM.259: : is valid record: 'false'

After I modified the code above and hard coded the group member sysID, the test passed.

MODIFIED CODE:
---------------------

var previousStepSysID = '707ffb41dbc3334058c12cxxxxxxxxxx';
gs.info("previousStepSysID: '" + previousStepSysID + "'");
var groupMbrSysID = '0cc18cacdb4fd340418xxxxxxxxxxxxx';  // <-- WORKS
gs.info("groupMbrSysID: '" + groupMbrSysID + "'"); 
 
var grpMbrGR = new GlideRecord('sys_user_grmember');
grpMbrGR.get(groupMbrSysID);
gs.info("is valid record: '" + grpMbrGR.isValidRecord() + "'");

MODIFIED CODE OUTPUT:
---------------------

Group member found: John Snow(f3a9f5b70fd7b10085b0exxxxxxxxxxx)
Full logging from step execution:
08:44:04 AM.59: Loading script: jasmine_lib/jasmine_custom.js
08:44:04 AM.97: : previousStepSysID: '707ffb41dbc3334058c12cxxxxxxxxxx'
08:44:04 AM.97: : groupMbrSysID: '0cc18cacdb4fd340418xxxxxxxxxxxxx'
08:44:04 AM.99: : is valid record: 'true'
08:44:04 AM.107: : outputs: 'f3a9f5b70fd7b10085b0exxxxxxxxxxx'

This is defeats the purpose of this server side script in that I can create separate steps containing hard coded values.

One last note.... I created a 'Log' step displaying the sysID of the (first) record gathered from Step 8 (Record Query) and placed it between Step 8 (Record Query) and Step 10 (Server Script).  In both cases, output showed the correct sysID '0cc18cacdb4fd340418xxxxxxxxxxxxx'.

I believe the lesson here is.... if the previous step is a 'Record Query', the steps() function cannot extract the record sysID from it,...at which point, it must be hard coded.

I hope this sheds light on ATF functionality.

- Leo

leochavez
Tera Guru

A colleague of mine picked up this post and submitted it to the Idea Portal:  https://community.servicenow.com/community?id=view_idea&sysparm_idea_id=3d96d23edb864010190dfb243996...

codycotulla
Tera Guru

Hi Leo,

I believe that the issue with your script is that you are trying to load the wrong output variable from your step 8, assuming that it is a Record Query step. Rather than record_id, you need to get the first_record variable. 

gr.get(steps(PREVIOUS_STEP_SYS_ID).first_record);

Hope this helps you or someone else.

Let me know if you have any questions.

Thanks,

Cody

Mala
ServiceNow Employee
ServiceNow Employee

Very helpful and smart answer it works perfectly 

Helped me too. Thank you!