- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
08-07-2020 04:57 AM
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.
Solved! Go to Solution.
- Labels:
-
Automated Test Framework
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
08-07-2020 06:00 AM
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
Regards
Ankur
Ankur
✨ Certified Technical Architect || ✨ 9x ServiceNow MVP || ✨ ServiceNow Community Leader

- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
08-07-2020 05:08 AM
"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.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
08-07-2020 05:12 AM
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
Ankur
✨ Certified Technical Architect || ✨ 9x ServiceNow MVP || ✨ ServiceNow Community Leader
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
08-07-2020 05:34 AM
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
}
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
08-07-2020 06:00 AM
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
Regards
Ankur
Ankur
✨ Certified Technical Architect || ✨ 9x ServiceNow MVP || ✨ ServiceNow Community Leader