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

Kieran Anson
Kilo Patron

Hi,

Your script include isn't using the 'current' record, it just querying all incident records and looping over them

dabulO7
Tera Contributor

Hi Kieran,

So I'm supposed somehow to push required records(incidents) to some kind of an array and then return it? 
Or is there some other way.

Can you share your code (not a screenshot) and I'll happily amend and provide guidance

Sure, much appreciated, here you are.

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

    dueDateCalendar: function(current) {
        var GRInc = new GlideRecord('incident');
        GRInc.addEncodedQuery('due_dateISNOTEMPTY^due_dateRELATIVEGT@minute@ahead@0^due_dateRELATIVELT@dayofweek@ahead@4');
        GRInc.query();

        while (GRInc.next()) {
            var gdt = new GlideDateTime();
            //gdt.setDisplayValue('2024-06-05 12:00:00');
            var dueDate = new GlideDateTime(GRInc.due_date);

            var dur = new DurationCalculator();

            dur.setSchedule('2a13e7ad87e6ce10764276a7cebb353a');

            dur.calcScheduleDuration(gdt, dueDate);

            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) {
                //  gs.print("Incident: " + GRInc.number + ' ' + 'all time: ' + totalTimeDiff + ' business time: ' + businessTimeDiff);
                return true;
            } else {
                return false;
            }

        }

    },

    type: 'dueDateSchedule'
};