In change management when the "start_date" is accessed via script it shows a different value

Shaji_Kalidasan
Tera Guru

Dear Experts,

 

Greetings for the day

 

I am trying to implement an alert message while trying to click the "Implement" button in the normal change request, so that the users can get an alert if they are implementing the change before the agreed time.

 

function moveToImplement() {
    var message = "Starting this change before agreed time - Are you sure you want to go-ahead? Ok or Cancel?";
    if (confirm(message)) {
        // User clicked "OK"
        g_form.setValue("state", "-1");
        gsftSubmit(null, g_form.getFormElement(), "state_model_move_to_implement");
    }
}

if (typeof window == 'undefined')
    setRedirect();

function setRedirect() {
    current.update();
    action.setRedirectURL(current);
}

  

I am testing the scenario in fix script but it shows a different value, but when I access it using JavaScript Executor in the client side it shows the current value.

I am not sure where I am going wrong, could you please help.

 

var grChangeRequest = new GlideRecord("change_request");
grChangeRequest.addQuery("name", "CHG0005269");
grChangeRequest.setWorkflow(false);
gs.print("Before query");
grChangeRequest.query();
gs.print("After query");

if (grChangeRequest.next()) {
    var now = new GlideDateTime();

    // Check if the field exists before trying to get its value
    if (grChangeRequest.isValidField("start_date")) {
        // Check if the 'start_date' is null, undefined, or evaluates to an empty string
        // True if the item is null, undefined, or evaluates to the empty string.
        if (!JSUtil.nil("start_date")) {
            // Print additional information about the start_date field
            gs.print("start_date field details:");
            gs.print("Field value: " + grChangeRequest.start_date);
            //gs.print("Field type: " + grChangeRequest.start_date.getClassDisplayName());
            //gs.print("Field is valid: " + grChangeRequest.start_date.isValid());
            var plannedImplementationStart = grChangeRequest.getValue("start_date");
            gs.print("now: " + now);
            gs.print("plannedImplementationStart: " + plannedImplementationStart);
            if ((GlideDateTime.subtract(now, new GlideDateTime(plannedImplementationStart)).getNumericValue()) < 0) {
				//Actually it will be an alert instead of gs.print in script action
                gs.print("Starting this change before agreed time - Are you sure you want to go-ahead? Ok or Cancel?");
            }
        }
    } else {
        gs.print("Error: 'start_date' field does not exist on the Change Request table.");
    }
} else {
    gs.print("Error: Change Request with name 'CHG0005269' not found.");
}

/*

OUTPUT Using Fix Script

*** Script: Before query
*** Script: After query
*** Script: start_date field details:
*** Script: Field value: 2022-09-29 18:00:00
*** Script: now: 2024-02-19 10:58:58
*** Script: plannedImplementationStart: 2022-09-29 18:00:00
*** Script: Starting this change before agreed time - Are you sure you want to go-ahead? Ok or Cancel?
[0:00:00.034] Total Time

OUTPUT using g_form.getValue("start_date")

alert(g_form.getValue("start_date"));

2024-02-15 15:00:28 //<--- Prints the correct value

*/

Please let me know your thoughts.  

 

Thank you

Shaji Kalidasan

1 REPLY 1

Shaji_Kalidasan
Tera Guru

Dear Experts

 

The issue is fixed by passing the sys_id instead of number.

 

PFB the updated code for your reference please.

 

var grChangeRequest = new GlideRecord("change_request");
grChangeRequest.addQuery("sys_id", "16d688da9794ce103ea8fa01f053af4d");
grChangeRequest.setWorkflow(false);
gs.print("Before query");
grChangeRequest.query();
gs.print("After query");

if (grChangeRequest.next()) {
    var now = new GlideDateTime();
    // Check if the field exists before trying to get its value
    if (grChangeRequest.isValidField("start_date")) {
        // Check if the 'start_date' is null, undefined, or evaluates to an empty string
        // True if the item is null, undefined, or evaluates to the empty string.
        if (!JSUtil.nil("start_date")) {
            // Print additional information about the start_date field
            gs.print("start_date field details:");
            gs.print("Field value: " + grChangeRequest.start_date);            
            var plannedImplementationStart = grChangeRequest.getValue("start_date");
            gs.print("now: " + now);
            gs.print("plannedImplementationStart: " + plannedImplementationStart);
            if ((GlideDateTime.subtract(now, new GlideDateTime(plannedImplementationStart)).getNumericValue()) > 0) {
		//Actually it will be an alert instead of gs.print in script action
                gs.print("Starting this change before agreed time - Are you sure you want to go-ahead? Ok or Cancel?");
            }
        }
    } else {
        gs.print("Error: 'start_date' field does not exist on the Change Request table.");
    }
} else {
    gs.print("Error: Change Request with name 'CHG0005269' not found.");
}

 

Thank you

 

Sincerely

Shaji Kalidasan