ATF Custom Step Query

Khalid9030
Tera Contributor

Hi All,

 

We have ATF as attached(PFA:Wa_01) in that we have created the custom step Wait N Seconds as attached (PFA:Wait N Second_02) this step will stop the ATF till given seconds

In the Wait N Second step (PFA:Wa_03).

Here Our requirement is ,

when I give 120 seconds in Wait for N seconds step it is waiting for the response till 120 seconds. In some cases, the response is coming from the previous step that is Scheduled Script Execution is less than 120 seconds like for example 50 sec. in that situation I don't want to wait for 120 seconds it will run immediately once the response is received from the previous step (Scheduled Script Execution) and corresponding next step should be run. 

Note: it should be work like timeout option in other steps.

Is this possible to do this requirement? If yes, please let me know what the changes are required.

1 ACCEPTED SOLUTION

Hi Khalid

Please try this. I tried to refactorize the code as per my understanding of the code shared by you.

(function executeStep(inputs, outputs, stepResult, timeout) {


    var inputSeconds = 950; //Ankush: 950 seconds or 120 seconds
    var milliseconds = parseInt(inputSeconds, 10) * 1000;
    var start = new Date().getTime() + milliseconds;
    var incidentFound = false;

    do {
        var gr = new GlideRecord('incident');
        gr.addQuery('active', true);
        gr.addQuery('state', '2');
        gr.query();
        if (gr.hasNext()) {
            // Incident in "In Progress" state found
            incidentFound = true;
            break;
        }
    } while (new Date().getTime() < start);

    stepResult.setOutputMessage('Found incident in progress: ' + incidentFound);
    return incidentFound;
})(outputs, steps, params, stepResult, assertEqual);

 

--
Best Regards
Ankush

P.S. Please mark helpful/correct as appropriate to help the fellow community member in identifying the relevant answers.

View solution in original post

7 REPLIES 7

Khalid9030
Tera Contributor

adding the Wait N seconds step code below. 

 

Description generation script :

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

 

Step execution script :

(function executeStep(inputs, outputs, stepResult, timeout) {
    var seconds = parseInt(inputs.u_seconds2, 10) * 1000;
    var start = parseInt(new Date().getTime()) + seconds;
    while(start>parseInt(new Date().getTime())){
        // do nothing
    }
    stepResult.setOutputMessage('Waited ' + inputs.u_seconds + ' seconds.');
    stepResult.setSuccess();
}(inputs, outputs, stepResult, timeout));

 

 

Ankush Agrawal
ServiceNow Employee
ServiceNow Employee

Hi Khalid

I understand your requirement is for fluentWait (i.e. wait for a max of [timeout or an event occurs]). This is tricky to implement with a generic wait step. 

 

The ideal way to solve this is in 2 steps:

1. Know which condition should be true (e.g.: a record is updated by a scheduled job)

2. Wait for that condition or max timeout, e.g.: 

(function executeStep(inputs, outputs, stepResult, timeout) {
    var seconds = parseInt(inputs.u_seconds2, 10) * 1000;
    var start = parseInt(new Date().getTime()) + seconds;
    while(start>parseInt(new Date().getTime())){
        isConditionTrue(condition)
            break;
    }
    stepResult.setOutputMessage('Waited ' + inputs.u_seconds + ' seconds.');
    stepResult.setSuccess();
}(inputs, outputs, stepResult, timeout));

 

The tricky part here is to pass on the condition.

 

A simpler way would be to use 'Run server side-script' instead of the step 'Wait N seconds'. In that step, use the specific condition (e.g.: the record is updated) with the timeout. This implementation will be similar to the code snippet above, and you should be easily able to add your condition check.

 

--
Best Regards
Ankush

P.S. Please mark helpful/correct as appropriate to help the fellow community member in identifying the relevant answers.

@Ankush Agrawal 

Thanks for your quick response. 

 

For Run server side-script ,could you please give me an example for an incident table with the conditions and timeout parameters.  

 

@Ankush Agrawal 

Hi Ankush,
I have used the below script. but getting error as attached. could you please check the code. 

 

var gr = new GlideRecord('incident');
    gr.addQuery('active', true);
    gr.addQuery('state', '2');
    gr.query();

    while (!gr.hasNext()) { // No incident in "In Progress" state found
        // Wait for 120 seconds
        var inputSeconds = 950;
        var milliseconds = parseInt(inputSeconds, 10) * 1000;
        var start = new Date().getTime() + milliseconds;
       
        while (new Date().getTime() < start) {
            // Wait
        }
       
        stepResult.setOutputMessage('No incident in progress. Waited for ' + inputSeconds + ' seconds.');
        stepResult.setSuccess();
    } else {
        stepResult.setOutputMessage('Incident is in progress.');
        stepResult.setSuccess();
    }

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