- 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-17-2025 02:02 AM
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
07-17-2025 02:09 AM
Hello @Naman Jain2412 ,
Debug the code. Put it alert inside the if statement and see if it is going to the if statement.
Thank you.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
07-17-2025 02:11 AM
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
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
07-17-2025 02:12 AM
@Naman Jain2412 Sure
Thank you.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
07-17-2025 02:16 AM
@Naman Jain2412 - Why can't you use a combination of Script Include and onchange client script? Is there any reason?
Thank you.