How to auto populate the due date field based on priority field in problem and problem_task table

pvv1045330
Tera Contributor

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.

4 REPLIES 4

Tai Vu
Kilo Patron
Kilo Patron

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

harshav
Tera Guru

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.

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.

script code.png

Client script

 

client code.png

 

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:

 

function onChange(control, oldValue, newValue, isLoading, isTemplate) {
    if (isLoading || newValue === '') {
g_form.setReadOnly('due_date', true);
        return;
    }
 
    //Type appropriate comment here, and begin script below
    if (newValue == 1 || newValue == 2) { // if priority is 1 or 2
        var ga = new GlideAjax('Duedatepopulate'); // 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.clearValue('due_date', true);
        g_form.setMandatory('due_date', true);
 
    }
}
 
function priorityValue(response) {
    var answer = response.responseXML.documentElement.getAttribute("answer");
    if (answer) {
g_form.clearValue('due_date', true);
        g_form.setValue('due_date', answer);
        g_form.setReadOnly('due_date', true);
    }