How do I limit the date field so that future dates cannot be selected for Assessment Metrics?

symonflores_23
Tera Guru

This is the survey form - assessment metrics - question data type: date that we want to limit.

symonflores_23_0-1766461306305.pngsymonflores_23_1-1766461354296.png

 

 

3 REPLIES 3

Ankith Sharma
Giga Guru

Hi @symonflores_23 ,

You can handle this using an onChange Client Script on the Assessment Instance Question table.

Create the Client Script
Go to Client Scripts and click New.

Configure the script as follows:

  • Table: as per your requirement

  • Type: onChange

  • Adding a condition is optional if you want to target a specific metric or question

Client Script example:

 

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

var today = new GlideDate();
var selectedDate = new GlideDate(newValue);

if (selectedDate.after(today)) {
g_form.showFieldMsg(control, 'Future dates are not allowed.', 'error');
g_form.clearValue(control);
}
}

 
 

have a look at the code. It prevents users from selecting or submitting future dates and works for Assessment Metrics / Survey date questions.

 

If you find this useful, kindly mark it as Accept as Solution and Helpful.

Regards,
- Ankit
LinkedIn: https://www.linkedin.com/in/sharmaankith/

Ankur Bawiskar
Tera Patron
Tera Patron

@symonflores_23 

normal client script or UI policy won't work on survey question

check this link

Survey Field validation 

💡 If my response helped, please mark it as correct and close the thread 🔒— this helps future readers find the solution faster! 🙏

Regards,
Ankur
Certified Technical Architect  ||  9x ServiceNow MVP  ||  ServiceNow Community Leader

Hemanth M1
Giga Sage
Giga Sage

Hi @symonflores_23 

 

You can't have a field validation on the assessment/survey question (date field is not supported) or Proactive approach(from client side) However you can do below (more reactive approach),

 

1.Write a BR on the assessment instance table when state changes and State changes to complete (you can also handle save scenario here)

2.Glide assessment instance question and check the string value (date entered)

3. check if its greater than today then abort the submission with a message

 

BR:

Change the Survey id here

HemanthM1_0-1766467316250.png

2. Script logic 

HemanthM1_1-1766467416400.png

 

Result:

HemanthM1_2-1766467454397.png

HemanthM1_3-1766467794764.png

 

script:

(function executeRule(current, previous /*null when async*/ ) {

    // Add your code here
    var asmtQuestion = new GlideRecord("asmt_assessment_instance_question");
    asmtQuestion.addQuery("instance", current.sys_id);
    asmtQuestion.addQuery("metric", "6c7d9a60c346f210c3dcd8477d013111"); //mettic = Question sys_id
    asmtQuestion.query();
    if (asmtQuestion.next()) {
        var inputDate = asmtQuestion.getValue("string_value");
        //--- Get today's date---
        var today = new GlideDate();
        today.setDisplayValue(gs.nowDate());

        var gGivenDate = new GlideDate();
        gGivenDate.setDisplayValue(inputDate);
        inputDate = gGivenDate;
        //---compare two dates-----
        if (inputDate.after(today)) {
            gs.addErrorMessage('The given date cannot be after today.');
            current.setAbortAction(true);
        }

    }

})(current, previous);

 

 

 

Accept and hit Helpful if it helps.

Thank you,
Hemanth
Certified Technical Architect (CTA), ServiceNow MVP 2024, 2025