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

Juhi Poddar
Kilo Patron

Hello @GBS 

Please try the below script:

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

// Calculate the expiration date (2 months from now)
var gdt = new GlideDateTime();
gdt.addMonths(2);

// Use the calculated date for filtering articles expiring within 2 months
gr.addQuery('valid_to', '<=', gdt);

// Execute the query
gr.query();

if (gr.hasNext()) {
    gs.print('Articles expiring within the next two months:');
    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 within the next two months.');
}

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 I have changed 

gr.addQuery('valid_to', '>=', gdt);  to get the future dates but it is also showing the articles valid to is more that 2 months but I want exact 2 months

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

GBS
Tera Contributor

@Juhi Poddar it worked. Thank you!