- 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: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:52 PM
Hello @Naman Jain2412 ,
Please create before insert business rule and try the below code-
(function executeRule(current, previous /*null when async*/) {
// Get the current date and time (server's current time)
var gdt = new GlideDateTime();
// Define the due date based on priority
if (current.priority == 1) { // Critical
// Example: 4 hours from now
gdt.addHours(4);
} else if (current.priority == 2) { // High
// Example: 1 day from now
gdt.addDays(1);
} else if (current.priority == 3) { // Moderate
// Example: 3 days from now
gdt.addDays(3);
} else if (current.priority == 4) { // Low
// Example: 5 days from now
gdt.addDays(5);
} else if (current.priority == 5) { // Planning
// Example: 7 days from now
gdt.addDays(7);
} else {
// Optional: Clear or set a default if priority is not matched
// current.due_date = '';
}
// Set the due_date field
current.due_date = gdt.getDisplayValue(); // Or getInternalValue() for internal format
})(current, previous);
Thank you.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
07-16-2025 10:53 PM
hi @Naman Jain2412,
This is my pseudocode in Incident table. This is Business Rule runs before insert when priority field changes. Can you please try it and let me know the reason?
Thank you,
Long Duong
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
07-17-2025 02:13 AM
server side it's working however I need for onchange client script