Setting valid_to date of Knowledge article to 6 months after published date
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎06-05-2017 06:03 AM
Hi all,
I have written a business rule to set the valid_to date of Knowledge article to '6 months after published date'
This runs when n article is published, so an 'After' 'update' BR is used when 'State' changes to 'published'.
(function executeRule(current, previous /*null when async*/) {
var gdt= current.published.getGlideObject();
//gs.addInfoMessage("time:"+gdt);
current.valid_to = gdt.addMonthsLocalTime(6);
})(current, previous);
Above is the script, but its not working. Something is missed.
Please correct me.

- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎06-05-2017 06:10 AM
Hi Neetika,
Change this to a BEFORE update BR and you should be fine. See the Business Rules Best practices below for when to use Before and when to use After rules.
Reference:
Business Rules - ServiceNow Wiki
Business Rules Best Practices - ServiceNow Wiki
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎06-05-2017 10:10 PM
Hi Chuck,
Thanks for your reply.
The issue I am facing here is that the published date is changing to 6 months later not the valid_to date.
Valid_to date becomes blank.

- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎06-06-2017 05:01 AM
I believe it's because of the way you are doing the getGlideObject(). It's a reference, not a copy of the value.
Try this in your before role (note: untested code)
var gdt = new GlideDateTime();
gdt.setValue(current.getValue('published'));
current.valid_to = gdt.addMonthsLocalTime(6);
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎09-20-2017 09:22 AM
Tested and working code.
var gdt = current.valid_to.getGlideObject();
workflow.info("Valid To Before: " + current.valid_to);
workflow.info("GDT Before: " + gdt);
gdt.addMonths(6);
workflow.info("gdt.addMonths(6): " + gdt);
current.valid_to = gdt;
workflow.info("current.valid_to: " + current.valid_to);
The issue, is that the "Add" functions, gdt.AddMonths(), do not return the object it just added months too. It is a standalone function that just does its work, and.. nothing. You were setting current.valid_to to void(), which is undefined. I do not know the reasoning for returning void(), it is confusing to the user, but more knowledgable people than me decided to return nothing.
Also, published and valid_to are GlideDates, not GlideDateTimes, which may make a difference. As a rule of thumb, I always check the Table to see what kind of piece of data it is expecting.