logo

NJP

ATF Step Configuration for Getting User Group Member

Import · Sep 06, 2020 · article

I've been creating ATF custom step configurations to make creating tests easier. One situation I ran encountered a few times was needing to get a user for a group. I created a custom step configuration that gets the user for a group and that user's manager, since I found I often needed the user's manager as well.

Here is the information for the test configuration:

Step Environment: Server Independent

Category: Server

Description Script:

function generateDescription() {
    // the global variable 'step' represents the current glide record
    var description = "";

    // your code here
    description = "Run the step using the following parameters.";
    if (!gs.nil(step.inputs.u_group.getDisplayValue())) {
        var text = step.inputs.u_group.getDisplayValue() + "";
        description += "\n-- u_group: " + text.trim();
    }

    var outparams = "";
    outparams += "\n--u_user = The user who is a member of the group";
    outparams += "\n--u_manager = The user who is the manager of the user";
    description += "\n\n The step outputs values in following output parameters\n";
    description += outparams;

    return description;


}
generateDescription();

Executing Script:

(function executeStep(inputs, outputs, stepResult, timeout) {
    // Get the input parameters
    var record = (!gs.nil(inputs.u_group)) ? inputs.u_group + "" : "";
    // var table = (!gs.nil(inputs.u_table)) ? inputs.u_table + "" : "";

    // add content to the message;
    var message = "";

    // set the "success" variable to false if there is an expected result in the script
    var success = true;

    var searchTable = "sys_user_group";

    var grGroup = new GlideRecord(searchTable);
    if (grGroup.get(record)) {

        var grUgm = new GlideRecord("sys_user_grmember");
        grUgm.addQuery("group", record);
        grUgm.addQuery("user.active", true);
        grUgm.addQuery("user.manager.active", true);
        grUgm.query();
        if (grUgm.next()) {
            outputs.u_user = grUgm.getValue("user") + "";
            outputs.u_manager = grUgm.user.manager.getValue() + "";
            message += "\nUser " + grUgm.getValue("user") + "";
            message += "\nManager " + grUgm.user.manager.getValue() + "";
            message += "\n outputs.u_user " +  outputs.u_user + "";
            message += "\noutputs.u_manager " + outputs.u_manager + "";


        } else {
            success = false;
            message += "\nCould not get the User for the group\nQuery = " + grUgm.getEncodedQuery();
        }

    } else {
        success = false;
        message += "\nCould not get the group record from the group ID\nQuery = " + grGroup.getEncodedQuery();
    }

    if (success) {
        stepResult.setOutputMessage('Success!' + message);
        stepResult.setSuccess();
    } else {
        stepResult.setOutputMessage('Failure!\n' + message);
        stepResult.setFailed();
    }


}(inputs, outputs, stepResult, timeout));

Input Variables

  • Group: Type = Document ID, Column Name = u_group

Out put Variables

  • User: Type = Document ID, Column Name = u_user
  • Manager: Type = Document ID, Column Name = u_manager

I hope someone finds this useful.

Thanks,

Cody

View original source

https://www.servicenow.com/community/developer-articles/atf-step-configuration-for-getting-user-group-member/ta-p/2324261