The “Valid to” date on all knowledge article templates cannot exceed a year from the current date.

prash4
Tera Expert

Currently:

The “Valid to” date defaults to a year, which is correct. The submitter can alter that date past the one year and that is not correct. Must not exceed that default value.

 

Default date is one year from today and make it mandatory that the value not exceed this maximum default date.

1 ACCEPTED SOLUTION

Laszlo Balla
ServiceNow Employee
ServiceNow Employee

Default valid to date of an article is based on the value in the 'article_validity' field of the Knowledge base, or set to 2100-01-01 if empty. Question is, whether you want the user to not exceed the default value, regardless of what that is, or you want hard code a limit of 1 year. In both cases, I would go with an onChange client scipt.

 

Option 1: validity check based on default value

The default value is calculated using this script include:

new global.KBCommon().getDefaultValidToDateFromCurrentDate(current.kb_knowledge_base)

Unfortunately this is not available from client scripts, so you would either have to create your own client callable script include, or use a display business rule that gets this data on load of the form and makes it available to the client script. I will go with the latter now.

Display BR:

LaszloBalla_2-1672840072636.png

Add condition to only run this business rule if the user has access to create articles, so it is not triggered every single time the article is opened by someone:

LaszloBalla_1-1672840006961.png

Code (excluding condition):

g_scratchpad.defaultValidity = new global.KBCommon().getDefaultValidToDateFromCurrentDate(current.kb_knowledge_base);

 

Client Script (using JavaScript Date object):

LaszloBalla_3-1672842055382.png

Script:

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

    var validityField = 'valid_to';
	var defaultValidity = new Date(g_scratchpad.defaultValidity);
    var newValidity = new Date(newValue);

	if(newValidity > defaultValidity) {
		g_form.clearValue(validityField);
		g_form.showErrorBox(validityField,'Valid to date cannot exceed the default value of ' + g_scratchpad.defaultValidity);
	} else {
		g_form.hideErrorBox(validityField);
	}

}

 

Option 2: check if valid to date is within a year

This can be done purely on the client side too, using again the standard JS Date object for example. The script include will be again onChange like above, so now I will only paste in the code:

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

    var validityField = 'valid_to';
	var onYearValidity = new Date(new Date().setFullYear(new Date().getFullYear() + 1));
    var newValidity = new Date(newValue);

	if(newValidity > onYearValidity) {
		g_form.clearValue(validityField);
		g_form.showErrorBox(validityField,'Valid to date must be within a year from today');
	} else {
		g_form.hideErrorBox(validityField);
	}

}

 

Hope it helps!

View solution in original post

6 REPLIES 6

Laszlo Balla
ServiceNow Employee
ServiceNow Employee

Default valid to date of an article is based on the value in the 'article_validity' field of the Knowledge base, or set to 2100-01-01 if empty. Question is, whether you want the user to not exceed the default value, regardless of what that is, or you want hard code a limit of 1 year. In both cases, I would go with an onChange client scipt.

 

Option 1: validity check based on default value

The default value is calculated using this script include:

new global.KBCommon().getDefaultValidToDateFromCurrentDate(current.kb_knowledge_base)

Unfortunately this is not available from client scripts, so you would either have to create your own client callable script include, or use a display business rule that gets this data on load of the form and makes it available to the client script. I will go with the latter now.

Display BR:

LaszloBalla_2-1672840072636.png

Add condition to only run this business rule if the user has access to create articles, so it is not triggered every single time the article is opened by someone:

LaszloBalla_1-1672840006961.png

Code (excluding condition):

g_scratchpad.defaultValidity = new global.KBCommon().getDefaultValidToDateFromCurrentDate(current.kb_knowledge_base);

 

Client Script (using JavaScript Date object):

LaszloBalla_3-1672842055382.png

Script:

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

    var validityField = 'valid_to';
	var defaultValidity = new Date(g_scratchpad.defaultValidity);
    var newValidity = new Date(newValue);

	if(newValidity > defaultValidity) {
		g_form.clearValue(validityField);
		g_form.showErrorBox(validityField,'Valid to date cannot exceed the default value of ' + g_scratchpad.defaultValidity);
	} else {
		g_form.hideErrorBox(validityField);
	}

}

 

Option 2: check if valid to date is within a year

This can be done purely on the client side too, using again the standard JS Date object for example. The script include will be again onChange like above, so now I will only paste in the code:

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

    var validityField = 'valid_to';
	var onYearValidity = new Date(new Date().setFullYear(new Date().getFullYear() + 1));
    var newValidity = new Date(newValue);

	if(newValidity > onYearValidity) {
		g_form.clearValue(validityField);
		g_form.showErrorBox(validityField,'Valid to date must be within a year from today');
	} else {
		g_form.hideErrorBox(validityField);
	}

}

 

Hope it helps!

Hi @Laszlo Balla 

 

I have tried your ways and didn't worked. valid to date still be be modified.

Laszlo Balla
ServiceNow Employee
ServiceNow Employee

Can you please elaborate on what's not working? I have tested the above and it worked fine for me.

after creating BR and client script on change. 

 

i tried creating article and valid to value didnt take the default value it has to be within a year not exceed a year