Display Field Message (error) when past date selected on Date Type Variable

rishabh31
Mega Sage

Dear Team,

 

Please help here.

 

Currently, the below onChange Client Script (script include called) is functioning fine, but NOT displaying an error field message just below the Variable (date_to_disable_account)

 

 

 

 

 

 

 

function onChange(control, oldValue, newValue, isLoading) {
    if (isLoading || newValue == '') {
        return;
    }

    var disDate = new GlideAjax('validateDisableDate');
    disDate.addParam('sysparm_name', 'nowDateTime');
    disDate.addParam('sysparm_disable_date', g_form.getValue('date_to_disable_account'));
    disDate.getXML(compareDisableDate);

    function compareDisableDate(response) {
        var answer = response.responseXML.documentElement.getAttribute("answer");
        if (answer < 0) { //To allow current date but not past date
            //g_form.addErrorMessage("Date to Disable Account should not be the Past Date");
            g_form.showFieldMsg('date_to_disable_account', 'Date to Disable Account should not be the Past Date', 'error');
            g_form.clearValue('date_to_disable_account');
        }
    }
}

 

 

 

 

 

 

See, addErrorMessage is working fine, but it displays an error msg at the top of the catalog form (which is not desired) and clears this variable date value (which is functioning fine).

I want to display an error message just below the variable (date_to_disable_account) on the catalog form, so I used showFieldMsg and tested in catalog form by choosing any past date and found it functions only to clear the date value but NOT DISPLAYING any error field message in the highlighted area (below the variable).

rishabh31_1-1691133369757.png

 

Requesting to please help to get field message (error) display below the variable.

 

Thank You

 

 

2 ACCEPTED SOLUTIONS

Manmohan K
Tera Sage

Hi @rishabh31 

 

Check with below script

function onChange(control, oldValue, newValue, isLoading) {
    if (isLoading || newValue == '') {
        return;
    }

    var disDate = new GlideAjax('validateDisableDate');
    disDate.addParam('sysparm_name', 'nowDateTime');
    disDate.addParam('sysparm_disable_date', g_form.getValue('date_to_disable_account'));
    disDate.getXML(compareDisableDate);

    function compareDisableDate(response) {
        var answer = response.responseXML.documentElement.getAttribute("answer");

        if (answer < 0) { //To allow current date but not past date
            //g_form.addErrorMessage("Date to Disable Account should not be the Past Date");
          
            g_form.clearValue('date_to_disable_account');
           g_form.showFieldMsg('date_to_disable_account', 'Date to Disable Account should not be the Past Date', 'error');
        }
    }
}

View solution in original post

Rahul Talreja
Mega Sage
Mega Sage

Hi @rishabh31 ,
Can you please try with:

function onChange(control, oldValue, newValue, isLoading) {
    if (isLoading || newValue == '') {
        return;
    }

    var disDate = new GlideAjax('validateDisableDate');
    disDate.addParam('sysparm_name', 'nowDateTime');
    disDate.addParam('sysparm_disable_date', g_form.getValue('date_to_disable_account'));
    disDate.getXML(compareDisableDate);

    function compareDisableDate(response) {
        var answer = response.responseXML.documentElement.getAttribute("answer");

        if (parseInt(answer) < 0) { //To allow current date but not past date
            g_form.clearValue('date_to_disable_account');
           g_form.showFieldMsg('date_to_disable_account', 'Date to Disable Account should not be the Past Date', 'error');
        }
    }
}
Please mark my response correct/helpful as applicable!
Thanks and Regards,
Rahul

View solution in original post

4 REPLIES 4

Manmohan K
Tera Sage

Hi @rishabh31 

 

Check with below script

function onChange(control, oldValue, newValue, isLoading) {
    if (isLoading || newValue == '') {
        return;
    }

    var disDate = new GlideAjax('validateDisableDate');
    disDate.addParam('sysparm_name', 'nowDateTime');
    disDate.addParam('sysparm_disable_date', g_form.getValue('date_to_disable_account'));
    disDate.getXML(compareDisableDate);

    function compareDisableDate(response) {
        var answer = response.responseXML.documentElement.getAttribute("answer");

        if (answer < 0) { //To allow current date but not past date
            //g_form.addErrorMessage("Date to Disable Account should not be the Past Date");
          
            g_form.clearValue('date_to_disable_account');
           g_form.showFieldMsg('date_to_disable_account', 'Date to Disable Account should not be the Past Date', 'error');
        }
    }
}

Hi @Manmohan K ,

 

Thank You🙂, your provided script where lines just interchanged is working fine.

rishabh31_0-1691137958959.png

But I am wondering why earlier it was not working as all things (syntax, method etc) are correct, so how does interchanging the lines make it work, please explain.

 

Marking your response as correct and helpful.

@rishabh31 

 

 If you try to clear the value using  g_form.clearValue(..) straight after the g_form.showFieldMsg(..) it will not display the error message as clearValue overrides the changes done by showFieldMsg function.

 

Instead clear the value prior to displaying the error message for getting expected results

Rahul Talreja
Mega Sage
Mega Sage

Hi @rishabh31 ,
Can you please try with:

function onChange(control, oldValue, newValue, isLoading) {
    if (isLoading || newValue == '') {
        return;
    }

    var disDate = new GlideAjax('validateDisableDate');
    disDate.addParam('sysparm_name', 'nowDateTime');
    disDate.addParam('sysparm_disable_date', g_form.getValue('date_to_disable_account'));
    disDate.getXML(compareDisableDate);

    function compareDisableDate(response) {
        var answer = response.responseXML.documentElement.getAttribute("answer");

        if (parseInt(answer) < 0) { //To allow current date but not past date
            g_form.clearValue('date_to_disable_account');
           g_form.showFieldMsg('date_to_disable_account', 'Date to Disable Account should not be the Past Date', 'error');
        }
    }
}
Please mark my response correct/helpful as applicable!
Thanks and Regards,
Rahul