- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
07-16-2025 10:09 PM
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.
Solved! Go to Solution.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
07-17-2025 02:26 AM
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');
}
}
}
Thank you.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
07-16-2025 10:14 PM
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.
Ankur
✨ Certified Technical Architect || ✨ 9x ServiceNow MVP || ✨ ServiceNow Community Leader
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
07-16-2025 10:21 PM - edited 07-16-2025 10:23 PM
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
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
07-16-2025 10:44 PM
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.
Ankur
✨ Certified Technical Architect || ✨ 9x ServiceNow MVP || ✨ ServiceNow Community Leader
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
07-16-2025 10:48 PM
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
Regards,
Nikhil Bajaj