Join the #BuildWithBuildAgent Challenge! Get recognized, earn exclusive swag, and inspire the ServiceNow Community with what you can build using Build Agent.  Join the Challenge.

use the due date from the description of the RITM to set the RITM due date.

C_S3
Tera Contributor

Hello All,
We have to use the due date from the description of the RITM to set the RITM due date. Use that same due date (-2 weeks) for each of the tasks on that RITM.

I have created one BR for this but it's not working , please let me knowif I'm doing it wrong. 

(function executeRule(current, previous /*null when async*/ ) {

    var string = current.description;
    gs.info("Due date" + string);
    var date = string.substring(string.indexOf('Due Date: ') + 11,
        string.indexOf('\n')); //if the date ends with a new line
    gs.info("string" + date);
    var dayMonthYear = date.split("/");

    var gDate = new GlideDateTime(dayMonthYear[2].trim() + "-" + dayMonthYear[0].trim() + "-" + dayMonthYear[1].trim() + " 12:00:00");

    current.due_date = gDate;
    current.update();

})(current, previous);

Any help would be appreciated .Thank you!
1 ACCEPTED SOLUTION

Try bellow updated script:

(function executeRule(current, previous /*null when async*/) {

    if (!current.description) {
        gs.info("Description is empty.");
        return;
    }

    // Extract the due date from the description
    var string = current.description;
    gs.info("Description: " + string); // Log the description for debugging
    var dueDateMatch = string.match(/Due Date:\s*(\d{1,2}-\d{1,2}-\d{4})/); // Updated regex for DD-MM-YYYY format

    if (dueDateMatch && dueDateMatch[1]) {
        var dateStr = dueDateMatch[1]; // Get the matched date string
        gs.info("Matched Due Date: " + dateStr); // Log the matched date
        
        // Split the date string into day, month, year
        var dayMonthYear = dateStr.split("-");
        
        // Create a GlideDateTime object
        var gDate = new GlideDateTime();
        gDate.setValue(dayMonthYear[2].trim() + "-" + dayMonthYear[1].trim() + "-" + dayMonthYear[0].trim() + " 12:00:00"); // Adjust order for GlideDateTime

        // Set the due date only if it's valid
        if (gDate) {
            current.due_date = gDate;
            gs.info("Due date set to: " + gDate); // Log the due date set

            // Now set the same due date for each associated sc_task
            var taskGR = new GlideRecord('sc_task');
            taskGR.addQuery('request_item', current.sys_id); // Assuming 'request_item' is the field linking tasks to RITM
            taskGR.query();

            while (taskGR.next()) {
                // Set the due date for the task
                taskGR.due_date = gDate;
                taskGR.update(); // Don't forget to update the record
                gs.info("Due date set for task " + taskGR.number + ": " + gDate);
            }
        } else {
            gs.info("Invalid date conversion.");
        }
    } else {
        gs.info("No valid due date found in description.");
    }

})(current, previous);

View solution in original post

7 REPLIES 7

hi @C_S3 

 

Looks your due date format is 10-10-2024; try bellow updated script once:

(function executeRule(current, previous /*null when async*/) {

    // Ensure the description is not empty
    if (!current.description) {
        gs.info("Description is empty.");
        return;
    }

    // Extract the due date from the description
    var string = current.description;
    gs.info("Description: " + string); // Log the description for debugging
    var dueDateMatch = string.match(/Due Date:\s*(\d{1,2}-\d{1,2}-\d{4})/); // Updated regex for DD-MM-YYYY format

    if (dueDateMatch && dueDateMatch[1]) {
        var dateStr = dueDateMatch[1]; // Get the matched date string
        gs.info("Matched Due Date: " + dateStr); // Log the matched date
        
        // Split the date string into day, month, year
        var dayMonthYear = dateStr.split("-");
        
        // Create a GlideDateTime object
        var gDate = new GlideDateTime();
        gDate.setValue(dayMonthYear[2].trim() + "-" + dayMonthYear[1].trim() + "-" + dayMonthYear[0].trim() + " 12:00:00"); // Adjust order for GlideDateTime

        // Set the due date only if it's valid
        if (gDate) {
            current.due_date = gDate;
            gs.info("Due date set to: " + gDate); // Log the due date set
        } else {
            gs.info("Invalid date conversion.");
        }
    } else {
        gs.info("No valid due date found in description.");
    }

})(current, previous);

Hello @Rajesh Chopade1 ,
It is working for RITM, but for sc_task the due date is not visible until we closed complete the state.

Try bellow updated script:

(function executeRule(current, previous /*null when async*/) {

    if (!current.description) {
        gs.info("Description is empty.");
        return;
    }

    // Extract the due date from the description
    var string = current.description;
    gs.info("Description: " + string); // Log the description for debugging
    var dueDateMatch = string.match(/Due Date:\s*(\d{1,2}-\d{1,2}-\d{4})/); // Updated regex for DD-MM-YYYY format

    if (dueDateMatch && dueDateMatch[1]) {
        var dateStr = dueDateMatch[1]; // Get the matched date string
        gs.info("Matched Due Date: " + dateStr); // Log the matched date
        
        // Split the date string into day, month, year
        var dayMonthYear = dateStr.split("-");
        
        // Create a GlideDateTime object
        var gDate = new GlideDateTime();
        gDate.setValue(dayMonthYear[2].trim() + "-" + dayMonthYear[1].trim() + "-" + dayMonthYear[0].trim() + " 12:00:00"); // Adjust order for GlideDateTime

        // Set the due date only if it's valid
        if (gDate) {
            current.due_date = gDate;
            gs.info("Due date set to: " + gDate); // Log the due date set

            // Now set the same due date for each associated sc_task
            var taskGR = new GlideRecord('sc_task');
            taskGR.addQuery('request_item', current.sys_id); // Assuming 'request_item' is the field linking tasks to RITM
            taskGR.query();

            while (taskGR.next()) {
                // Set the due date for the task
                taskGR.due_date = gDate;
                taskGR.update(); // Don't forget to update the record
                gs.info("Due date set for task " + taskGR.number + ": " + gDate);
            }
        } else {
            gs.info("Invalid date conversion.");
        }
    } else {
        gs.info("No valid due date found in description.");
    }

})(current, previous);