Display info message on field that how many leave balance they have for that selected type?

Deepika61
Tera Contributor

Hi All,

 

Actually we have record producer to submit a leave request , in that there is field "Leave category"(casual, annual, sick leave), so my requirement was like whenever user select leave type there need display info message on field that how many leave balance they have for that  particular selected type

 

Deepika61_0-1708408649001.png

Please help me to achieve this

Thanks

Deepika

9 REPLIES 9

James Chun
Kilo Patron

@Deepika61 

 

You need to provide more information such as where is the leave balance stored and how it is stored.

Otherwise, I can only provide a suggestion to use onChange Client Script with AJAX to fetch the data.

 

Thanks

@James Chun 

Thanks for the response 

Basically leaves stored in Yearly leaves table , here it will like based on the region they have stored the leave with particular category

 

Yearly leaves table we have leave type filed and reamining leave days , based on that and based on requested for we need fetch the date , but i am not how to write a logic on that

Deepika61_0-1708509771556.png

 

 

So i need a field message that if particular user select annual leave as a category , then it will show his balance on the field message

 

Thanks

Deepika

@Deepika61,

 

You can use AJAX for that, create a client callable Script Include. Example below:

 

 

var leaveUtil = Class.create();
leaveUtil.prototype = Object.extendsObject(AbstractAjaxProcessor, {

    getRemainingBalance: function () {

        var user = this.getParameter('sysparm_user');
        var leaveType = this.getParameter('sysparm_leave_type');
        var leaveGr = new GlideRecord(''); //add your table name here
        leaveGr.addEncodedQuery('') //add query here, something like subjectPerson = user + leave_type = leaveType
        leaveGr.query();

        if (leaveGr.next()) {
            return leaveGr.getValue('')//add remaining leave days field name
        } else {
            return;
        }

    },
    type: "leaveUtil"

});

 

 

 

And create an onChange Client Script on the 'Leave Category' field

 

 

function onChange(control, oldValue, newValue, isLoading) {
	if (isLoading || newValue == '') {
		return;
	}

	var ga = new GlideAjax('leaveUtil');
	ga.addParam('sysparm_name', 'getRemainingBalance');
	ga.addParam('sysparm_user', '');//add user's sys_id
	ga.addParam('sysparm_leave_type', newValue);
	ga.getXML(parseResponse);

	function parseResponse(response) {
		var answer = response.responseXML.documentElement.getAttribute("answer");
		g_form.addInfoMessage(gs.getMessage("User's remaining balance is ") + answer);
	}

}

 

 

 

You can modify the scripts above to meet your needs.

If you struggle with AJAX, there are plenty of articles/documentation online that can guide you.

 

Thanks

@James Chun 

I have tried like this , but its not working , can you please guide where i am missing

 

Script include:

 

var leavebalanceinfo = Class.create();
leavebalanceinfo.prototype = Object.extendsObject(AbstractAjaxProcessor, {

leaveinfo: function() {

var type = this.getParameter('sysparm_type');
var user = this.getParameter('sysparm_user');

var leave = new GlideRecord('u_yearly_leave');
leave.addQuery('u_leave_type', type);
leave.addQuery('assigned_to', user);

leave.query();
if (leave.next()) {

return leave.getValue('u_remaining_leave_days');
}


},

 


type: 'leavebalanceinfo'
});

 

Catalog Client script

 

function onChange(control, oldValue, newValue, isLoading) {
    if (isLoading || newValue == '') {
        return;
    }
    var userId = g_form.getReference('requested_for');
    var category = g_form.getValue('u_leave_category');
    var gr = new GlideAjax('leavebalanceinfo');
 
    gr.addParam('sysparm_name', 'leaveinfo');
 
    gr.addParam('sysparm_type', category);
    gr.addParam('sysparm_user', userId);
 
    gr.getXMLAnswer(validation);
 
    function validation(response) {
 
var answer = response.responseXML.documentElement.getAttribute("answer");
//g_form.addInfoMessage(gs.getMessage("User's remaining balance is ") + answer);
 
            g_form.showFieldMsg('u_leave_category','This is your current leave balance :'+answer, 'info');
 
 
 
 
 
        
    }
}

 

Thanks

Deepika