Regarding onchange planned start date Validation in change Request

LokeshwarRV
Tera Contributor

function onChange(control, oldValue, newValue, isLoading) {

    if (isLoading || !newValue) {

        return;

    }var changeType = gForm.getValue('type');

if (changeType.toLowerCase() === 'normal') {

        var ga = new GlideAjax('ValidateStartDateUtils');

        ga.addParam('sysparm_name', 'isFutureDateTime');

        ga.addParam('sysparm_selected_date', newValue);

        ga.getXMLAnswer(function(response) {

            if (response === 'false') {

                gForm.showFieldMsg('start_date', 'Start date must be in the future for Normal changes.', 'error');

                gForm.setValue('start_date', '');

            }

        });

    }

}this is client script

var ValidateStartDateUtils = Class.create();

ValidateStartDateUtils.prototype = Object.extendsObject(AbstractAjaxProcessor, {

    

    isFutureDateTime: function() {

        var selectedDate = this.getParameter('sysparm_selected_date');

        if (!selectedDate) return false;

 

        var selected = new GlideDateTime(selectedDate);

        var now = new GlideDateTime();

 

        return selected.after(now) ? 'true' : 'false';

    }

});and script includes but it is not working 

 

13 REPLIES 13

J Siva
Tera Sage

Hi @LokeshwarRV 

 

Try UI policy to achieve this requirement.

 

UI policy condition should be planned start date is after today.

 

Then in the scripts under 'Execute if false' add the below script.

 

g_form.clearValue('start_date');

g_form.showFieldMsg('start_date','Planned start date should be a future date','error');

 

Also, it should be g_form not gForm

Regards ,

 

Siva

neetusingh
Giga Guru

@LokeshwarRV  - Can you try below -

1. Client Script

Type: onChange

Field: start_date

Table: Change Request (or your custom table)

function onChange(control, oldValue, newValue, isLoading) {

    if (isLoading || !newValue) {

        return;

    }

    var changeType = gForm.getValue('type');

    if (changeType.toLowerCase() === 'normal') {

        var ga = new GlideAjax('ValidateStartDateUtils');

        ga.addParam('sysparm_name', 'isFutureDateTime');

        ga.addParam('sysparm_selected_date', newValue);

        ga.getXMLAnswer(function(response) {

            if (response === 'false') {

                gForm.showFieldMsg('start_date', 'Start date must be in the future for Normal changes.', 'error');

                gForm.setValue('start_date', '');

            }

        });

    }

}

> Note: Be sure the type field exists on the form and has a value like 'normal'.

2. Script Include

Name: ValidateStartDateUtils

Check: Must be client-callable

 

var ValidateStartDateUtils = Class.create();

ValidateStartDateUtils.prototype = Object.extendsObject(AbstractAjaxProcessor, {

 

    isFutureDateTime: function() {

        var selectedDate = this.getParameter('sysparm_selected_date');

        if (!selectedDate) return 'false';

 

        var selected = new GlideDateTime(selectedDate);

        var now = new GlideDateTime();

 

        return selected.after(now) ? 'true' : 'false';

    }

 

});

 

> Important: Make sure the Script Include has:

Name: ValidateStartDateUtils

Client Callable: Checked (true)

 

Additional Notes

  • The client script converts the date to a GlideDateTime-compatible ISO format before sending it to the server, avoiding parsing issues. Confirm that start_date field is a GlideDateTime type (not just date).
  • Make sure the field names 'type' and 'start_date' exactly match your form field names.
  • This client script should be attached to the onChange event of the start_date field.
  • Temporarily add console.log(response); inside getXMLAnswer() callback to verify the response is received.
  • The Script Include must be Client Callable to be accessible from GlideAjax. Ensure the Script Include is global or in the same application scope as your client script.

LokeshwarRV
Tera Contributor
function onChange(control, oldValue, newValue, isLoading) {
    if (isLoading || !newValue) {
        return;
    }
 
    var changeType = g_form.getValue('type');
 
    if (changeType.toLowerCase() === 'normal') {
        var ga = new GlideAjax('ValidateStartDateUtils');
        //alert("hello");
        ga.addParam('sysparm_name', 'isFutureDateTime');
        ga.addParam('sysparm_selected_date', newValue);
        ga.getXMLAnswer(function(response) {
            if (response === 'false') {
                alert("Start date must be in the future for Normal changes");
               
                g_form.showFieldMsg('start_date', 'Start date must be in the future for Normal changes.', 'error');
                g_form.setValue('start_date', '');
            }
        });
    } show feild msg is not displaying
 

function onChange(control, oldValue, newValue, isLoading) {
    if (isLoading || !newValue) {
        return;
    }
 
    var changeType = g_form.getValue('type');
 
    if (changeType.toLowerCase() === 'normal') {
        var ga = new GlideAjax('ValidateStartDateUtils');
        //alert("hello");
        ga.addParam('sysparm_name', 'isFutureDateTime');
        ga.addParam('sysparm_selected_date', newValue);
        ga.getXMLAnswer(function(response) {
            if (response === 'false') {
                alert("Start date must be in the future for Normal changes");
                g_form.clearValue('start_date');
                g_form.showFieldMsg('start_date', 'Start date must be in the future for Normal changes.', 'error');
                
            }
        });
    } 

@LokeshwarRV  Try this