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

Ankur Bawiskar
Tera Patron
Tera Patron

@Naman Jain2412 

on which table?

you can always use before insert/update business rule.

What should be set for Due date for different Priority?

Example: for P1 it should be 2 days from the time ticket was created etc?

If my response helped please mark it correct and close the thread so that it benefits future readers.

Regards,
Ankur
Certified Technical Architect  ||  9x ServiceNow MVP  ||  ServiceNow Community Leader

Naman Jain2412
Tera Expert

sn_risk_mitigation_task

for P1 you can consider 2 days for p2 4 days p3 6 days p4 10 days p5 12 days.
if we use onchange client script then it is or business rule, based on this pls let me know the code

@Naman Jain2412 

you can use addDays() method in server side (business rule) and handle the logic.

what did you start with and where are you stuck?

If my response helped please mark it correct and close the thread so that it benefits future readers.

Regards,
Ankur
Certified Technical Architect  ||  9x ServiceNow MVP  ||  ServiceNow Community Leader

Nikhil Bajaj9
Giga Sage

Hi @Naman Jain2412 ,

 

My suggestion will be you please try code on your own and if you are facing some issue, then ask for help rather than asking for complete code.

 

Regards,

Nikhil Bajaj

Please appreciate my efforts, help and support extended to you by clicking on – “Accept as Solution”; button under my answer. It will motivate me to help others as well.
Regards,
Nikhil Bajaj