- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎09-28-2016 12:05 AM
Hello,
I would really appreciate help regarding this matter,
We have started to use SN KM as our knowledge tool,
When ever an article is published we have a field like 'Valid to'.We got a requirement like if this Valid date is less than or equal to today,I want this article as retired.
I have wrote an after BR on insert/Update,but its not working.Can some one help on this
var to_date = new GlideDateTime().getDate();
gs.log('today'+to_date);
var valid_date = new GlideDateTime(current.valid_to).getDate();
gs.log('today valid date '+valid_date);
if(to_date <=valid_date) {
gs.log('Today workflow state'+current.workflow_state);
current.workflow_state='retired';
}
Thanks
Saranya
Solved! Go to Solution.
- Labels:
-
Scripting and Coding
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎09-29-2016 12:58 AM
Hi sranya,
try with this in background script, then add it in scheduled job for single record.
var gr = new GlideRecord('kb_knowledge');
gr.addQuery('sys_id','give any valid date less than today kb article sys_id)//remove this line scheduled job
//gr.addQuery('workflow_state', 'published');//add this line when creates scheduled job
gr.query();
while (gr.next()){
var diff = gs.dateDiff(gs.nowDateTime(),gr.valid_to,true)
gs.print(diff);// remove in scheduled job
if (diff) < 0){
gr.workflow_state = 'retire';
gr.setWorkflow(false);
gr.update();
}
}
After run in background script, verify that record if state changes to retire then create scheduled job.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎09-28-2016 09:26 PM
Hello Balaji,
I have created an on demand scheduled job and it snot checking for the less than today date.Can you help me on it.
Thanks
Saranya
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎09-29-2016 12:58 AM
Hi sranya,
try with this in background script, then add it in scheduled job for single record.
var gr = new GlideRecord('kb_knowledge');
gr.addQuery('sys_id','give any valid date less than today kb article sys_id)//remove this line scheduled job
//gr.addQuery('workflow_state', 'published');//add this line when creates scheduled job
gr.query();
while (gr.next()){
var diff = gs.dateDiff(gs.nowDateTime(),gr.valid_to,true)
gs.print(diff);// remove in scheduled job
if (diff) < 0){
gr.workflow_state = 'retire';
gr.setWorkflow(false);
gr.update();
}
}
After run in background script, verify that record if state changes to retire then create scheduled job.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎09-29-2016 01:26 AM
Hello Balaji,
Thank you very much for the help.Below also got worked
var gr = new GlideRecord('kb_knowledge');
gr.addEncodedQuery('workflow_state!=retired^workflow_state=published');
gr.query();
var to_date = gs.now();
while (gr.next())
{
if(gr.valid_to <=to_date)
{
gr.workflow_state = 'retired';
gr.setWorkflow(false);
gr.update();
}
}
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎09-28-2016 11:27 PM
We had the same requirement and I achieved this via a scheduled job:
***************************************************************************************************************************
var today = gs.now();
var kbArticles = new GlideRecord("kb_knowledge");
kbArticles.addQuery("kb_knowledge_base", "c521a0c8f52202002b89377f21e5fa04"); //IS Knowledge Base
kbArticles.addQuery("workflow_state", "published");
kbArticles.query();
while (kbArticles.next()) {
var diffSeconds = gs.dateDiff(kbArticles.valid_to.getDisplayValue(), today, true);
if (diffSeconds > 0) {
var gr = new GlideRecord('sysapproval_approver');
gr.addQuery('document_id', kbArticles.sys_id);
gr.query();
while (gr.next()) {
gr.setValue('state', 'cancelled');
gr.comment = 'Approval Cancelled as Workflow restarted because the knowledge article expired.';
gr.update();
}
kbArticles.u_next_step = 1020;
kbArticles.update();
}
}
***************************************************************************************************************************
We use u_next_step value to change the workflow_state, thats why you see me updating that in the above code, but you can simply update the workflow_state directly in the above code, if you dont you status flows in servicenow.