The CreatorCon Call for Content is officially open! Get started here.

Auto Populate Due Date based on Priority (1.Critical, 2.High, 3. Moderate, 4.Low, 5.Planning)

Naman Jain2412
Tera Expert

Hi, 
I want to Auto Populate the Due Date (17/07/2025 10:38:09) based on Priority as (1.Critical, 2.High, 3. Moderate, 4.Low, 5.Planning). Pls let me know the relevant code for this as per the requirement.

 

 

1 ACCEPTED SOLUTION

Hello @Naman Jain2412 -

 

I have used Script Include + Onchange Client Script. Please use the script below -

Script Include -

// Name: DueDateCalculator
// Client Callable: True (important for GlideAjax)
// Description: Calculates a due date based on a given priority using GlideDateTime.

var DueDateCalculator = Class.create();
DueDateCalculator.prototype = Object.extendsObject(AbstractAjaxProcessor, {

    getCalculatedDueDate: function() {
        var priority = this.getParameter('sysparm_priority'); // Get the priority value from the client script
        var gdt = new GlideDateTime(); // Initialize GlideDateTime object with current time

        // Parse priority to an integer for comparison
        var parsedPriority = parseInt(priority);
        if (parsedPriority === 1) { // Critical: Add 60 days
            gdt.addDaysLocalTime(60);
        } else if (parsedPriority === 2) { // High: Add 90 days
            gdt.addDaysLocalTime(90);
        } else if (parsedPriority === 3) { // Moderate: Add 120 days
            gdt.addDaysLocalTime(120);
        } else if (parsedPriority === 4) { // Low: Add 180 days
            gdt.addDaysLocalTime(180);
        } else if (parsedPriority === 5) { // Planning: Add 10 days
            gdt.addDaysLocalTime(10);
        } else {
            return '';
        }
        // This format will typically match how ServiceNow displays date/time fields.
        return gdt.getDisplayValue();
    },

    type: 'DueDateCalculator' // Important: Must match the Script Include name
});

Client Script -

function onChange(control, oldValue, newValue, isLoading, isTemplate) {
    if (isLoading || newValue === '') {
        g_form.clearValue('due_date');
        return;
    }

    var ga = new GlideAjax('DueDateCalculator');
    ga.addParam('sysparm_name', 'getCalculatedDueDate');    // Specify the function within the Script Include to call
    ga.addParam('sysparm_priority', newValue);
	ga.getXML(getDueDateResponse);
    function getDueDateResponse(response) {
        var answer = response.responseXML.documentElement.getAttribute('answer');
        if (answer) {
            g_form.setValue('due_date', answer);
        } else {
            g_form.clearValue('due_date');
        }
    }
}
If my response was helpful, please mark it as correct and helpful.
Thank you.

View solution in original post

13 REPLIES 13

 var gdt = new GlideDateTime();
var priority = parseInt(newValue);

    // Define the due date based on priority
    if (priority == 1) { // Critical
        // Example: 4 hours from now
        gdt.addDaysLocalTime(60);  

    } else if (priority == 2) { // High
        // Example: 1 day from now
        gdt.addDaysLocalTime(90);

    } else if (priority == 3) { // Moderate
        gdt.addDaysLocalTime(120);
    } else if (priority == 4) { // Low  
        gdt.addDaysLocalTime(180);
     } else if (priority == 5) { // Planning
         gdt.addDaysLocalTime(10);
    } else if (priority == '') {
   
        // Optional: Clear or set a default if priority is not matched
        //      (priority == 'None')
        g_form.clearValue('due_date');
        return;

    }

    // Set the due_date field
    // g_form.due_date = gdt.getDisplayValue();
    g_form.setValue('due_date', gdt.getDisplayValue());

Hello @Naman Jain2412 ,

 

Debug the code. Put it alert inside the if statement and see if it is going to the if statement.

 

 

If my response was helpful, please mark it as correct and helpful.
Thank you.

I would like to apply this code for onchange client script however glide date time is server side callable. could you pls correct this code to use onchange client script or let me know to use instead of glide date time

@Naman Jain2412 Sure

If my response was helpful, please mark it as correct and helpful.
Thank you.

@Naman Jain2412 - Why can't you use a combination of Script Include and onchange client script? Is there any reason?

If my response was helpful, please mark it as correct and helpful.
Thank you.