How to call a script include from a field style to highlight only specific records' fields in list

dabulO7
Tera Contributor

Hello Folks!

I want to call a script include from a field style to 'highlight specific fields (due date field where the date is less that 48 hours from now).
I created a script include which seems to be working fine (tested in console) - doing its job.

But when I want to call it from a field style value, it highlights all of the fields on the list view.


Field style:

style incident.png

Script Include:
script 1.png
Where is the catch? What am I missing?

BR

1 ACCEPTED SOLUTION

Let me know if you want anything further explained 🙂 

var dueDateSchedule = Class.create();
dueDateSchedule.prototype = {
    initialize: function() {},

    dueDateCalendar: function(current) {
        var encodedQuery = 'due_dateISNOTEMPTY^due_dateRELATIVEGT@minute@ahead@0^due_dateRELATIVELT@dayofweek@ahead@4';
		//Validate that the current record passes the above encodedQuery condition
        var passedEncodedQuery = GlideFilter.checkRecord(current, encodedQuery);

		//If it doesn't (e.g due date is empty)
		//we return false to not apply the field style
        if (!passedEncodedQuery)
            return false;

		//Assumption is the following segment of your code worked
		var now = new GlideDateTime();
        var dur = new DurationCalculator();

        dur.setSchedule('2a13e7ad87e6ce10764276a7cebb353a');
        dur.calcScheduleDuration(now, current.getValue('due_date'));

		var businessTimeDiff = Math.round(dur.getSeconds() / 3600); //Business time in hours
            var totalTimeDiff = Math.round(dur.getTotalSeconds() / 3600); //Total time in hours

        if (businessTimeDiff <= 48 && businessTimeDiff >= 0)
			return true;
		
		return false;
    },

    type: 'dueDateSchedule'
};

View solution in original post

8 REPLIES 8

Let me know if you want anything further explained 🙂 

var dueDateSchedule = Class.create();
dueDateSchedule.prototype = {
    initialize: function() {},

    dueDateCalendar: function(current) {
        var encodedQuery = 'due_dateISNOTEMPTY^due_dateRELATIVEGT@minute@ahead@0^due_dateRELATIVELT@dayofweek@ahead@4';
		//Validate that the current record passes the above encodedQuery condition
        var passedEncodedQuery = GlideFilter.checkRecord(current, encodedQuery);

		//If it doesn't (e.g due date is empty)
		//we return false to not apply the field style
        if (!passedEncodedQuery)
            return false;

		//Assumption is the following segment of your code worked
		var now = new GlideDateTime();
        var dur = new DurationCalculator();

        dur.setSchedule('2a13e7ad87e6ce10764276a7cebb353a');
        dur.calcScheduleDuration(now, current.getValue('due_date'));

		var businessTimeDiff = Math.round(dur.getSeconds() / 3600); //Business time in hours
            var totalTimeDiff = Math.round(dur.getTotalSeconds() / 3600); //Total time in hours

        if (businessTimeDiff <= 48 && businessTimeDiff >= 0)
			return true;
		
		return false;
    },

    type: 'dueDateSchedule'
};

works like a charm Kieran, thank you very much! I learnt about this GlideFilter.checkRecord. Nice! Thank you once again

Kieran,

one more question: if I wanted to create a schedule job to check daily with specific time conditions whether this is a right time to send a notification, can I make use of this and do something like that? The outcome does not seem to be right

var GRInc = new GlideRecord('incident');     GRInc.addEncodedQuery('due_dateISNOTEMPTY^due_dateRELATIVEGT@minute@ahead@0^due_dateRELATIVELT@dayofweek@ahead@7');
        GRInc.query();

    var now = new GlideDateTime();
    //now.setDisplayValue('2024-06-07 08:00:00');
    var dur = new DurationCalculator();

    dur.setSchedule('2a13e7ad87e6ce10764276a7cebb353a');
    dur.calcScheduleDuration(now, GRInc.getValue('due_date'));

    var businessTimeDiff = Math.round(dur.getSeconds() / 3600); //Business time in hours
    var totalTimeDiff = Math.round(dur.getTotalSeconds() / 3600); //Total time in hours - just to check whether schedule works.
	
while(GRInc.next()){
    if (businessTimeDiff <= 56 && businessTimeDiff >= 48) {
     gs.eventQueue('incident.due_date_2days', GRInc); 
   } else if { (businessTimeDiff <= 8 && businessTimeDiff >= 0) {
     gs.eventQueue('incident.due_date_today', GRInc);
    }

 

Hiya,

You need to do your evaluation inside your while statement, see below amendment 

var GRInc = new GlideRecord('incident');
GRInc.addEncodedQuery('due_dateISNOTEMPTY^due_dateRELATIVEGT@minute@ahead@0^due_dateRELATIVELT@dayofweek@ahead@7');
GRInc.query();

while (GRInc.next()) {
    var now = new GlideDateTime();
    //now.setDisplayValue('2024-06-07 08:00:00');
    var dur = new DurationCalculator();

    dur.setSchedule('2a13e7ad87e6ce10764276a7cebb353a');
    dur.calcScheduleDuration(now, GRInc.getValue('due_date'));

    var businessTimeDiff = Math.round(dur.getSeconds() / 3600); //Business time in hours
    var totalTimeDiff = Math.round(dur.getTotalSeconds() / 3600); //Total time in hours - just to check whether schedule works.


    if (businessTimeDiff <= 56 && businessTimeDiff >= 48) {
        gs.eventQueue('incident.due_date_2days', GRInc);
    } else if (businessTimeDiff <= 8 && businessTimeDiff >= 0) {
        gs.eventQueue('incident.due_date_today', GRInc);
    }
}