Tricky ATF Task: Get Related Field Value in Record Query

jmiskey
Kilo Sage

I have started pumping out some ATF testing, thanks in no small part to some of the help I have gotten from the Community here.  I am hoping that they can come through with another problem I have.

We have a Catalog Item where Manager's can set Delegates for their Employees on the Service Portal.  They go out to the Portal, bring up the Form, select their employee, fill out all the Delegation details, and click submit.  Once they submit, it creates a RITM and runs a Workflow that creates a delegation record in the sys_user_delegate table.

One thing that we did do was add a custom field tp the sys_user_delegate table named: u_ritm_number.  For delegate records that were created using this Form, it just captures the RITM number and stores it here, in string format.

So, I am trying to take advantage of this field in my ATF testing.  The first 7 steps of my testing are submitting a random request on the Service Portal.  Then, the 8th step is a Record Query step to confirm that the RITM record is created in the ServiceNow.  Steps 1-8 work flawlessly.

Now, I am trying step 9, which is to confirm that a record is added to the sys_user_delegate table.  What I am trying to do is to get the RITM number from the RITM that was created in the earlier steps, and query the sys_user_delegate table (on the u_ritm_number) field, and see if any records exist with that value.  The issue is, if I try to compare that field in the sys_user_delegate table, it is trying to compare it to the sys_id field from my earlier steps on the RITM.  I am not sure how to get the RITM Number field, and not the sys_id, to compare it to.

Here is what my ATF Steps look like:

find_real_file.png

find_real_file.png

 

And here is what the details on my last (failed) step look like:

find_real_file.png

It looks like it blanked out the value I tried to enter (because it was obviously wrong!).

Does anyone know how to accomplish what I am trying to do?

Thanks

1 ACCEPTED SOLUTION

OK I think I like Gerald's approach better.  It doesn't require a custom step (as he stated, it will not be reused).  If you add the "Run Server Side Validation Script", you should be able to paste the below modified script into the script box.

Find the step that queries the requested item table and returns the RITM sys_ID (I think it is 8 in your screen shot), right click on that step and choose copy sys_ID.  Paste that string in to replace "031b2ab52f7300106c32e83df699b6db" in my code.  That's how you get back to the result from that previous step.

 

(function(outputs, steps, stepResult, assertEqual) {
    var my_query_step_sys_id = "031b2ab52f7300106c32e83df699b6db"; //IMPORTANT: Replace this sting with the sys_id of the step earlier that does the query of the RITM

    var gr = new GlideRecord("sc_req_item");
    gr.get(steps(my_query_step_sys_id).first_record); // opens the "first record" returned by the previous query so we can use the number later

    var gr_d = new GlideRecord("sys_user_delegate"); //now searching the delegate table
    gr_d.addQuery("u_ritm", gr.getValue('number')); //where our string RITM number = the RITM.number
    gr_d.query();
    if (gr_d.next()) {
        //we have a result
        stepResult.setOutputMessage('Success!');
        stepResult.setSuccess();
        //you can set some more output vars here if you'd like or pass them in the success message above (e.g. who is the delegate)
        return true;

    } else {
        stepResult.setOutputMessage('Failure!');
        stepResult.setFailed();
        return false;
    }
})(outputs, steps, stepResult, assertEqual);

View solution in original post

10 REPLIES 10

Under Automated Test Frame Work (ATF) / Administration / Step Configurations

find_real_file.png

 

Your Step Configuration will look something like this:

 

Hi!

So Don is on target, assuming his Script is correct. Rather than creating a new Step Configuration, especially if this won't be a re-occurring situation, select the 'Run Server Side Validation Script' step under Server, and type Don's script in the 'Test script' section. Clear the existing text first before typing in Don's Script. He posted an image so you will have to manually type it as it looks in his image.

Once done, save it, and then use an 'Open Existing Record' step afterwards to open the RITM record Don's Script pulls.

- Gerald

I am getting an error, saying "inputs" is not defined.

In his original post, he mentioned something about defining an Input Variable, but I could not find where I am supposed to do that.  If I try to do within this "Run Server Side Script" method, where/how do I do that?

Thanks

OK I think I like Gerald's approach better.  It doesn't require a custom step (as he stated, it will not be reused).  If you add the "Run Server Side Validation Script", you should be able to paste the below modified script into the script box.

Find the step that queries the requested item table and returns the RITM sys_ID (I think it is 8 in your screen shot), right click on that step and choose copy sys_ID.  Paste that string in to replace "031b2ab52f7300106c32e83df699b6db" in my code.  That's how you get back to the result from that previous step.

 

(function(outputs, steps, stepResult, assertEqual) {
    var my_query_step_sys_id = "031b2ab52f7300106c32e83df699b6db"; //IMPORTANT: Replace this sting with the sys_id of the step earlier that does the query of the RITM

    var gr = new GlideRecord("sc_req_item");
    gr.get(steps(my_query_step_sys_id).first_record); // opens the "first record" returned by the previous query so we can use the number later

    var gr_d = new GlideRecord("sys_user_delegate"); //now searching the delegate table
    gr_d.addQuery("u_ritm", gr.getValue('number')); //where our string RITM number = the RITM.number
    gr_d.query();
    if (gr_d.next()) {
        //we have a result
        stepResult.setOutputMessage('Success!');
        stepResult.setSuccess();
        //you can set some more output vars here if you'd like or pass them in the success message above (e.g. who is the delegate)
        return true;

    } else {
        stepResult.setOutputMessage('Failure!');
        stepResult.setFailed();
        return false;
    }
})(outputs, steps, stepResult, assertEqual);

Thanks Don!  This absolutely rocks!  It does exactly what I want!

Just one small thing to note, in case others come across this for help, the Step name is just called "Run Server Side Script" (the word "Validation is not part of it).

Thanks to all for your help on this!