ATF for change requests

Poorva Bhawsar
Mega Sage

Hi Community,

 

We are creating ATFs for different type of changes but we are not able to set the value of the planned start date and end date. If we are setting it to any date, later after that date, ATFs will fail.

 

Also, we want to change the planned start date to current date before moving it to the implement state because it should reach the planned start date. Initially we can’t select current date because of restrictions.

 

How to change it using run script?

 

What test step we have to add.

 

Thanks,

Poorva

6 REPLIES 6

If this fixed your issue please mark my answer as correct.

Shahjay
Mega Guru

Hi @Poorva Bhawsar ,

@brianlan25's custom step covers generating the dates at create time, so I’ll focus on the second part: moving the planned start to now before Implement.

One thing first, in case you try generating dates with a plain Run Server Side Script step. That step only has two outputs, table and record_id, so you can’t pass a date from it into a later Set Field Values step. That’s why it has to be a custom step config with Date/Time output variables, like Brian’s.

For the Implement part, add a Run Server Side Script step just before the step that clicks Implement. Get the change sys_id from the step that created it, then set start_date to the current time. End date can stay as it is, since it’s still after the new start.

 
(function(outputs, steps, params, stepResult, assertEqual) {
    // sys_id of the step that created the change (Submit a Form / Record Insert)
    var chgId = steps('PASTE_CREATE_STEP_SYS_ID').record_id;

    var chg = new GlideRecord('change_request');
    if (!chg.get(chgId)) {
        stepResult.setOutputMessage('Change not found: ' + chgId);
        return false;
    }

    var now = new GlideDateTime();
    chg.setValue('start_date', now.getValue());
    chg.update();

    // re-read so a business rule that blocked the update fails here, not at Implement
    chg.get(chgId);
    if (chg.getValue('start_date') != now.getValue()) {
        stepResult.setOutputMessage('Planned start was not updated on ' + chg.getValue('number'));
        return false;
    }

    outputs.table = 'change_request';
    outputs.record_id = chgId;
    return true;
})(outputs, steps, params, stepResult, assertEqual);


After that step, add an “Open an Existing Record” step on change_request, using the record_id output from the script step, and then click Implement. If you’re using form steps, the form that’s already open still has the old planned start. UI action conditions are also evaluated when the form loads, so if your Implement button depends on the start date, it won’t show until you reload.

Updating from the server gets you past client-side restrictions, like a UI policy or a client script that blocks today’s date. If the restriction is a before business rule that aborts the update, it will still fire, and the step will fail with the message above. You can call chg.setWorkflow(false) before update() to get round it. But that skips every business rule on that update, so I’d only do it if there’s no other option.

steps() needs the sys_id of the earlier step. If you copy this test for your other change types, the copied steps get new sys_ids, so you’d have to update that line every time. With several change types, I’d make this a custom step config too, with a Change reference input, and pass the record in with a data pill.

Two small things on Brian’s script if you reuse it. I’d use new GlideDateTime() instead of new GlideDateTime(gs.nowDateTime()). gs.nowDateTime() returns the display value in the running user’s format and timezone, while the constructor expects UTC in yyyy-MM-dd HH:mm:ss. The fixed GMT hours will also shift by an hour when daylight saving changes. And if the test runs on the window day itself, the loop doesn’t move to the next week, so the start it generates can already be in the past.