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

Dear Laszlo,

 

Good day!

As per SNOW - If the Article Validity field is blank, the default date in the Valid to field for the knowledge article is set to January 1, 2100; it is found here - https://docs.servicenow.com/bundle/sandiego-servicenow-platform/page/product/knowledge-management/co....

How can we set it to 1 year from the date the KB article was published and not January 1, 2100 since that is too long for an article to get reviewed again?  It's just that we are also thinking of a solution to a scenario wherein a KB author might forget to add a date on the Article Validity field, thus setting the Valid date field to "January 1, 2100", which we are trying to avoid.  We would like to change that to 1 year so that in case a KB author forgets or misses adding a date on the Article Validity field - it will only last for a year for it to stay valid.

 

Thank you!

Best Regards,
Mikee Laurel

Hi Mikee,

 

The same article you are referring to mentions that January 1, 2100 is only used as default valid to date, if the Article Validity field is empty on the Knowledge Base.

 

The default value of the Valid to field is derived from the Article Validity field configured for the knowledge base (see Create a knowledge base). The Valid to date starts from the date the article was created until the number of days specified in the Article Validity field. If the Article Validity field is blank, the default date in the Valid to field for the knowledge article is set to January 1, 2100.

Therefore, set the Article Validity field on the Knowledge Base to 365 (it accepts a number, not a date). This will ensure that a default valid to date is calculated for new knowledge articles with a date "365 days from today". If the author would attempt to change that, the client scripts I provided above can help prevent that.

Hope it helps!