Setting valid_to date of Knowledge article to 6 months after published date

neetika
Tera Contributor

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.

10 REPLIES 10

Chuck Tomasi
Tera Patron

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  


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.


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);


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.



https://developer.servicenow.com/app.do#!/api_doc?v=jakarta&id=r_ScopedGlideDateTimeAddDaysLocalTime...