How to auto populate the due date field based on priority field in problem and problem_task table
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
11-13-2023 06:11 PM
Hi,
I have requirement auto populate due date field based on priority field in problem and problem_task table.
ex: If priority is p1 due date is field should be auto populate 7, also field should be read only
if priority is p2 due date is field should be auto populate 14, also field should be read only
if priority is p3 or 4,due date field is mandatory and user will be enter the manually
I have written before business rules but it's working whenever we change priority and save the data then only it is is auto populating with the value.
I want auto populate when ween priority change then due date will be mandatory and auto populate values.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
11-13-2023 07:19 PM - edited 11-13-2023 07:21 PM
Hi @pvv1045330
"I want auto populate when ween priority change then due date will be mandatory and auto populate values."
Then you will need an OnChange Client Script to achieve this.
Sample below.
#ClientCallable Script Include
var CLProblemUtilsAJAX = Class.create();
CLProblemUtilsAJAX.prototype = Object.extendsObject(AbstractAjaxProcessor, {
calculateDueDate: function(){
var priority = this.getParameter('sysparm_priority');
var gdt = new GlideDateTime();
if(priority >= 3){
return '';
}
gdt.addDaysUTC(7 * parseInt(priority));
return gdt;
},
type: 'CLProblemUtilsAJAX'
});
#OnChange Client Script
function onChange(control, oldValue, newValue, isLoading, isTemplate) {
if (isLoading || newValue === '') {
return;
}
var ga = new GlideAjax('global.CLProblemUtilsAJAX');
ga.addParam('sysparm_name', 'calculateDueDate');
ga.addParam('sysparm_priority', g_form.getValue('priority'));
ga.getXMLAnswer(function(response) {
if (!response) {
g_form.clearValue('due_date');
g_form.setReadOnly('due_date', false);
g_form.setMandatory('due_date', true);
return;
}
g_form.setValue('due_date', response);
g_form.setReadOnly('due_date', true);
});
}
Let me know if it works for you.
Cheers,
Tai Vu
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
11-13-2023 07:35 PM - edited 11-13-2023 07:37 PM
in order to achieve this you need a client script and script include.
Client Script
function onChange(control, oldValue, newValue, isLoading, isTemplate) {
if (isLoading || newValue === '') {
return;
}
if (newValue == 1 || newValue == 2) { // if priority is 1 or 2
var ga = new GlideAjax('testScriptPriority'); // set your scriptInclude name
ga.addParam('sysparm_name', 'priorityDateSet'); // name of the function in your script include
ga.addParam('sysparm_priority', newValue);
ga.getXML(priorityValue);
}
if (newValue == 3 || newValue == 4) { // if priority is 3 or 4
g_form.setMandatory('due_date', true);
}
}
function priorityValue(response) {
var answer = response.responseXML.documentElement.getAttribute("answer");
if (answer) {
g_form.setValue('due_date', answer);
g_form.setReadOnly('due_date', true);
}
}
now in your script include add this function
priorityDateSet: function() {
var priority = this.getParameter('sysparm_priority');
var userDateFormat = gs.getSession().getUser().getDateFormat(); // if your field is dateTime change this to datetime
var newDate = new GlideDateTime();
if (priority == 1) {
newDate.addDaysLocalTime(7);
}
if (priority == 2) {
newDate.addDaysLocalTime(14);
}
var gdt = new GlideDateTime();
gdt.setDisplayValue(newDate, userDateFormat); // don't forget to convert it otherwise dates might cause an issue depends on user date/time format. eventhough if it works for you it might impact other users so don't ignore this line.
return gdt.getLocalDate(); // if your field is dateTime change this to datetime
},
Hope this answers your question.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
11-13-2023 08:32 PM - edited 11-13-2023 09:23 PM
Hi @harshav ,
Thanks for replay, I tried above with datetime field but it will not return any values, it's retune empty value.
if I tried above it will retune only date values.
Client script
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
11-13-2023 09:36 PM
Hi @harshav ,
I have change my script it's working as excepted.
priorityDateSet: function() {
var priority = this.getParameter('sysparm_priority');
var newDate = new GlideDateTime();
if (priority == 1) {
newDate.addDaysLocalTime(7);
}
if (priority == 2) {
newDate.addDaysLocalTime(14);
}
return newDate;
},
type: 'Duedatepopulate'
Client script: