How to retrieve the knowledge articles based on the valid to

GBS
Tera Contributor

I’m using the background script below to get the articles that will expire in 2 months from now, I have tried with encoded query but none are working. Can anyone help me with this

 

var gr = new GlideRecord('kb_knowledge');
gr.addQuery('workflow_state', 'published');
gr.addQuery('kb_knowledge_base.title', 'HR');
gr.addEncodedQuery();
gr.query();

if (gr.hasNext()) {
    gs.print('Articles expiring after the end of this month:');
    while (gr.next()) {
        var kbName = gr.kb_knowledge_base.getDisplayValue();
        gs.print('Article Number: ' + gr.number);
        gs.print('Title: ' + gr.short_description);
        gs.print('Knowledge Base: ' + kbName);
        gs.print('Expiration Date: ' + gr.valid_to);
        gs.print('----------------------------------');
    }
} else {
    gs.print('No articles found expiring after the end of this month.');
}
1 ACCEPTED SOLUTION

Hello @GBS 

In that case you can modify the logic 

gr.addQuery('valid_to', gdt);

Hope this helps!

 

"If you found my answer helpful, please like and mark it as an "accepted solution". It helps others find the solution more easily and supports the community!"

 

Thank You
Juhi Poddar

View solution in original post

10 REPLIES 10

GBS
Tera Contributor

@Juhi Poddar how to modify the script if I want the articles which will expire in 1months and also in 2months

Hello @GBS 

To do minimum changes in your previous code, try this while making a query:

gr.addQuery('valid_to', gdt);
gr.addQuery('valid_to', gdt.addMonths(-1));

 Hope this helps!

 

"If you found my answer helpful, please like and mark it as an "accepted solution". It helps others find the solution more easily and supports the community!"

 

Thank You

Juhi Poddar

GBS
Tera Contributor

 

@Juhi Poddar thank you

 

Hello @GBS 

You are most welcome!

Glad to help you resolve your query. 

 

If the solution worked for you, can you please hit like and mark it as an accepted solution

 

Thank You 

Juhi Poddar 

Mark Manders
Mega Patron

You are adding an encoded query without a query in your script. That doesn't do much. 

Can you check this (it worked on my instance):

 

var kbArticle = new GlideRecord('kb_knowledge');
kbArticle.addEncodedQuery('workflow_state=published^kb_knowledge_base.title=HR^valid_toRELATIVELT@month@ahead@2');
kbArticle.query();
if (kbArticle.hasNext()) {
    gs.print('Articles expiring after the end of this month:');
    while (kbArticle.next()) {
        var kbName = kbArticle.kb_knowledge_base.getDisplayValue();
        gs.print('Article Number: ' + kbArticle.number);
        gs.print('Title: ' + kbArticle.short_description);
        gs.print('Knowledge Base: ' + kbName);
        gs.print('Expiration Date: ' + kbArticle.valid_to);
        gs.print('----------------------------------');
    }
} else {
    gs.print('No articles found expiring after the end of this month.');
}

 


Please mark any helpful or correct solutions as such. That helps others find their solutions.
Mark