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

Rajesh Chopade1
Mega Sage

hi @C_S3 

Try bellow updated script once:

(function executeRule(current, previous /*null when async*/ ) {
    // Ensure the description contains "Due Date:"
    if (current.description && current.description.indexOf('Due Date:') !== -1) {
        var string = current.description;
        gs.info("Original Description: " + string);

        // Extract the due date
        var dateStart = string.indexOf('Due Date: ') + 11; // Start after "Due Date: "
        var dateEnd = string.indexOf('\n', dateStart); // Find the end of the date
        var date = string.substring(dateStart, dateEnd !== -1 ? dateEnd : string.length).trim(); // Trim to avoid spaces

        // Log extracted date
        gs.info("Extracted Due Date: " + date);

        // Convert the extracted date to a GlideDateTime object
        var dayMonthYear = date.split("/");
        if (dayMonthYear.length === 3) {
            // Create the GlideDateTime
            var gDate = new GlideDateTime(dayMonthYear[2].trim() + "-" + dayMonthYear[0].trim() + "-" + dayMonthYear[1].trim() + " 12:00:00");

            // Adjust due date by subtracting 2 weeks
            gDate.addDays(-14);

            // Set the RITM due date
            current.due_date = gDate;

            // Update the RITM
            current.update();

            // Now set the due date for each task associated with this RITM
            var taskGR = new GlideRecord('sc_task');
            taskGR.addQuery('request_item', current.sys_id);
            taskGR.query();
            while (taskGR.next()) {
                taskGR.due_date = gDate; // Set the same due date for the task
                taskGR.update(); // Update the task
            }
        } else {
            gs.error("Failed to parse the due date from the description.");
        }
    } else {
        gs.warn("Description does not contain 'Due Date:'. No action taken.");
    }
})(current, previous);

 

i hope my answer helps you to resolve your issue, if yes please mark my answer helpful and correct.

thank you

rajesh

Hello @Rajesh Chopade1 ,
Thank you for your quick response. I have tried the above script in After, insert, update BR. But I'm getting today's date in due date field of RITM.

Ensure that the date extracted from the description is in the correct format. If the format is not recognized, it may default to today's date.

Make sure the Business Rule is set to execute after the record is inserted or updated, but also check if it is configured to run for the correct table (e.g.,  sc_req_item).

 

Try bellow script once :

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

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

    // Extract the due date from the description
    var string = current.description;
    var dueDateMatch = string.match(/Due Date:\s*(\d{1,2}\/\d{1,2}\/\d{4})/); // Adjust regex to match your format

    if (dueDateMatch && dueDateMatch[1]) {
        var dateStr = dueDateMatch[1]; // Get the matched date string
        var dayMonthYear = dateStr.split("/");

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

        // Set the due date
        current.due_date = gDate;
        gs.info("Due date set to: " + gDate);
    } else {
        gs.info("No due date found in description.");
    }

})(current, previous);

Hello @Rajesh Chopade1 ,
I'm still getting today's date . I'm attaching description's image.

C_S3_0-1727791536714.png