- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
01-04-2023 04:00 AM
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.
Solved! Go to Solution.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
01-04-2023 06:30 AM
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:
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:
Code (excluding condition):
g_scratchpad.defaultValidity = new global.KBCommon().getDefaultValidToDateFromCurrentDate(current.kb_knowledge_base);
Client Script (using JavaScript Date object):
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!
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
01-04-2023 06:30 AM
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:
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:
Code (excluding condition):
g_scratchpad.defaultValidity = new global.KBCommon().getDefaultValidToDateFromCurrentDate(current.kb_knowledge_base);
Client Script (using JavaScript Date object):
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!
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
01-10-2023 05:13 AM
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
01-10-2023 05:18 AM
Can you please elaborate on what's not working? I have tested the above and it worked fine for me.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
01-10-2023 06:35 AM
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