knowledge article to valid

avinash1289
Tera Contributor

how to write script for knowledge article to valid to should not allow more than one year from the created date and previous date and when click on publish button it should show the error message in servicenow

1 REPLY 1

Anvin
Tera Expert

Hello,

Following script may be helpful.

 

Proceed with an before BR, with condition state changes to published (if so, you can remove the first if condition)

 

    // Validate the publish date when the Publish button is clicked
    if (current.state == 'published') { // Assuming 'published' is the state for Publish button
        var createdDate = new GlideDateTime(current.sys_created_on);
        var publishDate = new GlideDateTime(current.valid_to);
        var oneYearFromCreated = new GlideDateTime(current.sys_created_on);
        oneYearFromCreated.addYearsUTC(1); // Add one year to the created date

        // Check if the valid_to date is within one year of the created date
        if (publishDate.after(oneYearFromCreated) || publishDate.before(createdDate)) {
            gs.addErrorMessage("The 'Valid To' date must be within one year from the created date and cannot be earlier than the created date.");
            current.setAbortAction(true); // Prevent the record from being saved
        }
    }