update workflow_state of knowledge publish to retire

salu
Mega Guru

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

1 ACCEPTED SOLUTION

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.


View solution in original post

8 REPLIES 8

Mihir Mohanta
Kilo Sage

1.Make your business rule before insert/ update type.


find_real_file.png



find_real_file.png





Thanks,


Mihir


Hello Mihir,


I have done the same.It not working



Thanks


Saranya


The business rule is should be of Before type not after.Please recheck this thing.



This script I have provided to you is working perfectly for me.



Thanks,


Mihir


BALAJI40
Mega Sage

Hi saranya,



Instead of writing business rule better you can go with scheduled job. It should be better, why because business rule will run when and only insert/update the record only. So if you go with scheduled job set it will run daily/weekly based on your choice.


Just add this line code,


var gr = new GlideRecord('kb_knowledge');


gr.query();


while (gr.next()){


if (gs.dateDiff(gs.nowDateTime(),gr.valid_to,true) < 0){


gr.workflow_state = 'retire';


gr.setWorkflow(false);


gr.update();


}}



or if you want with business rule, Define when to run than just check this condition,


if (gs.dateDiff(gs.nowDateTime(),current.valid_to,true) < 0 ){


current.workflow_state = 'retire';


}