How to extend the Valid to date by 45 days if the article will expire within the next 30 days

Asha Pathak
Tera Contributor

I have to create scheduled job for monthly basis to extend the Valid to date by 45 days if the article will expire within the next 30 days
Please help!!

1 REPLY 1

Deepak Shaerma
Kilo Sage

Hello @Asha Pathak 

1. Navigate to the Scheduled Jobs Module

First, go to System Definition > Scheduled Jobs.

2. Create New Scheduled Job

Click on “New” to create a new job. Click on “Advanced” to open the advanced view where you can write a script.

3. Configure Your Scheduled Job

Under the “When to run” tab, you might choose “Run” as “Periodically” and then set it up to run monthly. Depending on your requirements, you might be precise with the day and time, depending on when you expect the least impact on users or system performance.

4. Script the Logic

// Calculate the date 30 days from now
    var thirtyDaysFromNow = gs.daysAgo(-30);
    
    // Query for articles with ‘valid_to’ date within the next 30 days
    var kbArticle = new GlideRecord('kb_knowledge');
    kbArticle.addQuery('valid_to', '<=', thirtyDaysFromNow);
    kbArticle.query();
    
    while (kbArticle.next()) {
        // For each article, calculate the new ‘valid_to’ date, 45 days from the current ‘valid_to’ date
        var validToDate = kbArticle.getValue('valid_to');
        var newValidToDate = new GlideDateTime(validToDate); // Create GlideDateTime instance with current ‘valid to’ date
        newValidToDate.addDays(45); // Add 45 days to it
        
        // Update the ‘valid_to’ field with the new date
        kbArticle.setValue('valid_to', newValidToDate);
        kbArticle.update();
        
        gs.info("Extended 'valid_to' date for article: " + kbArticle.getDisplayValue('number') + " to " + newValidToDate.getByFormat("yyyy-MM-dd"));
    }


Note: Please Mark this Helpful and Accepted Solution. If this Helps you to understand. This will help both the community and me..
- Keep Learning ‌‌
Thanks & Regards 
Deepak Sharma