Server Side ATF Script Assistance Needed - How do I get the sys_id of a record?

Casey23
Tera Guru

I am currently working on an ATF script to test change workflows. As part of the change workflow, the change sends multiple approvals out to members of the CAB group. My understanding is that there aren't any test steps that would allow me to query and approve all the records at once. However, from what I've been seeing, I can run a server side script and should be able to approve all of the records at once. Part of the issue is that I don't know how to get the sys_id of the change record that ATF is creating in order to query the "document_id" field in the "sysapproval_approver" table. Does anyone know how I can accomplish this? 

1 ACCEPTED SOLUTION

Casey23
Tera Guru

I ended up using a modified version of the script in this article: https://www.servicenow.com/community/developer-forum/atf-accessing-reference-variable-sys-id-in-serv...

 

Hopefully this helps someone out! Also thanks @Ankur Bawiskar!

(function(outputs, steps, params, stepResult, assertEqual) {
    
    var chgRec = new GlideRecord('change_request');
    chgRec.get(steps('033d33be1bc30e10fcfd5395604bcb4a').record_id); // Get the sys_id of the change from the step where it is created

    if (chgRec.isValidRecord()) { // Validate the change exists
        // gs.log("Change request found with ID: " + chgRec.sys_id);

		// Search for approvals related to this change request
        var apprRec = new GlideRecord('sysapproval_approver');
        apprRec.addQuery('document_id', chgRec.sys_id);
        apprRec.query();

		// Set all the approvals to approved
        var allUpdated = true;
        while (apprRec.next()) {
            apprRec.state = 'approved'; // Assuming 'approved' is the correct state value
            if (!apprRec.update()) {
                //gs.log("Failed to update approval record with ID: " + apprRec.sys_id);
                allUpdated = false;
            } else {
                //gs.log("Approval record updated with ID: " + apprRec.sys_id);
            }
        }

        // If all approvals are approved, return true, otherwise return false
		if (allUpdated) {
            return true;
        } else {
            return false;
        }
    } else {
        //gs.log("No change request found with the provided ID.");
        return false;
    }
})(outputs, steps, params, stepResult, assertEqual);

 

 

View solution in original post

2 REPLIES 2

Zach Koch
Giga Sage
Giga Sage

This thread may help you

ATF thread 

 

If this information helped resolve your issue, please remember to mark response correct and thumbs up to help future community members on this information, thanks!

Casey23
Tera Guru

I ended up using a modified version of the script in this article: https://www.servicenow.com/community/developer-forum/atf-accessing-reference-variable-sys-id-in-serv...

 

Hopefully this helps someone out! Also thanks @Ankur Bawiskar!

(function(outputs, steps, params, stepResult, assertEqual) {
    
    var chgRec = new GlideRecord('change_request');
    chgRec.get(steps('033d33be1bc30e10fcfd5395604bcb4a').record_id); // Get the sys_id of the change from the step where it is created

    if (chgRec.isValidRecord()) { // Validate the change exists
        // gs.log("Change request found with ID: " + chgRec.sys_id);

		// Search for approvals related to this change request
        var apprRec = new GlideRecord('sysapproval_approver');
        apprRec.addQuery('document_id', chgRec.sys_id);
        apprRec.query();

		// Set all the approvals to approved
        var allUpdated = true;
        while (apprRec.next()) {
            apprRec.state = 'approved'; // Assuming 'approved' is the correct state value
            if (!apprRec.update()) {
                //gs.log("Failed to update approval record with ID: " + apprRec.sys_id);
                allUpdated = false;
            } else {
                //gs.log("Approval record updated with ID: " + apprRec.sys_id);
            }
        }

        // If all approvals are approved, return true, otherwise return false
		if (allUpdated) {
            return true;
        } else {
            return false;
        }
    } else {
        //gs.log("No change request found with the provided ID.");
        return false;
    }
})(outputs, steps, params, stepResult, assertEqual);