In ATF, How to impersonate specify a member of a certain group who approves Change Request

Jayesh1
Giga Contributor

Hi,

In order to test a Change workflow I would like to have a step to impersonate a member of a certain group that can approve the change request. I would like to make it generic so that any member of a group can be selected. Has anyone tried this? 

 Please provide me "Run Server Side Script" for Change request.

Jayesh.

1 ACCEPTED SOLUTION

Hi Jayesh,

So once change request is submitted 6 approvals are generated for it. you need to do that one by one. 

So you are saying you need to impersonate six times and every time different user should be there in impersonation

Try this; I believe you need to run this 6 times

1) in the 1st run server side script step directly query sysapproval_approver table to get the approver

2) from the 2nd run server side script step onwards add the query of state is in requested so that it won't pick the 1st record which got approved

and same for other scripts

Sample below

Note: this will only work when you know there will be 6 approvals; consider in future that approval goes to 8 users then it will fail

This is non-tested

 

(function(outputs, steps, stepResult, assertEqual) {
// add test script here

// give here the step sys_id for submit form of change request

var changeRecordSysId = steps('sys_id_of_step').record_id; 

var gr = new GlideRecord("sysapproval_approver");
gr.addQuery('state','requested');
gr.addQuery('sysapproval',changeRecordSysId);  
gr.query();
if(gr.next()) {

outputs.table = 'sys_user';

outputs.record_id = gr.approver;

stepResult.setOutputMessage("Approver user found " + gr.approver.name);

stepResult.setSuccess();

return true;

}

sharing links as well for help

https://community.servicenow.com/community?id=community_question&sys_id=0661776adbb3234423f4a345ca96...

https://community.servicenow.com/community?id=community_question&sys_id=5940b2fadbef63809a64e15b8a96...

Regards
Ankur

 

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

View solution in original post

5 REPLIES 5

Hitoshi Ozawa
Giga Sage
Giga Sage

"Impersonate user" is no longer recommended because the user may be inactive or if the user is active, it would lower the security.

Currently, it is recommended to "Create user" in ATF step.

https://docs.servicenow.com/bundle/orlando-application-development/page/administer/auto-test-framewo...

https://community.servicenow.com/community?id=community_question&sys_id=98ed71ccdbcd0018414eeeb5ca96...

Ankur Bawiskar
Tera Patron
Tera Patron

Hi Jayesh,

Please see below sample script

Note:

1) Ensure you give valid step sys_id i.e. the sys_id of step which submits the change request form

2) Ensure in the next step you use Impersonate User Step and Select the output of this Run Server Side Script step i.e. record_id

(function(outputs, steps, stepResult, assertEqual) {
// add test script here

// give here the step sys_id for submit form of change request

var changeRecordSysId = steps('sys_id_of_step').record_id; 

var gr = new GlideRecord("change_request");
gr.addQuery('sys_id', changeRecordSysId);  
gr.query();
if(gr.next()) {

var group = gr.assignment_group;

var member = new GlideRecord('sys_user_grmember');
member.addQuery('group', group);
member.addQuery('user.active', true); // check for active user from group
member.setLimit(1);
member.query();

if(member.next()){

outputs.table = 'sys_user';

outputs.record_id = member.user;

stepResult.setOutputMessage("Active group member found " + member.user.name);

stepResult.setSuccess();

return true;

}

else{

stepResult.setOutputMessage("Failed to find active member for the group");

stepResult.setFailed();

return false;

}

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

Regards
Ankur

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

Hi Ankur,

Thanks for the reply.

In my scenario, A change request goes for approval for 6 group members and total number of approvals are 50.

So i have to impersonate for 6 times with one user(Approval) of each assignment group.

Could you please update your script for one assignment group and rest i'll do it. Please provide if condition like 

if(assignment group =='abc'){

// select a user to impersonate

}

Hi Jayesh,

So once change request is submitted 6 approvals are generated for it. you need to do that one by one. 

So you are saying you need to impersonate six times and every time different user should be there in impersonation

Try this; I believe you need to run this 6 times

1) in the 1st run server side script step directly query sysapproval_approver table to get the approver

2) from the 2nd run server side script step onwards add the query of state is in requested so that it won't pick the 1st record which got approved

and same for other scripts

Sample below

Note: this will only work when you know there will be 6 approvals; consider in future that approval goes to 8 users then it will fail

This is non-tested

 

(function(outputs, steps, stepResult, assertEqual) {
// add test script here

// give here the step sys_id for submit form of change request

var changeRecordSysId = steps('sys_id_of_step').record_id; 

var gr = new GlideRecord("sysapproval_approver");
gr.addQuery('state','requested');
gr.addQuery('sysapproval',changeRecordSysId);  
gr.query();
if(gr.next()) {

outputs.table = 'sys_user';

outputs.record_id = gr.approver;

stepResult.setOutputMessage("Approver user found " + gr.approver.name);

stepResult.setSuccess();

return true;

}

sharing links as well for help

https://community.servicenow.com/community?id=community_question&sys_id=0661776adbb3234423f4a345ca96...

https://community.servicenow.com/community?id=community_question&sys_id=5940b2fadbef63809a64e15b8a96...

Regards
Ankur

 

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