How to call a script include from a field style

Dolly M
Tera Guru

Hi all,

 

My requirement is to show color codes in server table based on nearing/expired EOS dates.

 

  • Red color EOS within 3 month (less than or equal to 4 months)
  • Amber color EOS within 6 month (Between 4 to 8 months)
  • Green color EOS in 1 yrs 0r above. (More than 8 months)

I have created a script include but it isn't working. PFB -

 

Script include -

 

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

    redColour: function(current) {
        var server = new GlideRecord('cmdb_ci_server');
        server.addEncodedQuery('operational_statusIN1,9^u_os_eosISNOTEMPTY');
        server.query();
        while (server.next()) {
            var gdt1 = new GlideDate(server.u_os_eos);
            var gdt2 = new GlideDate();
            gdt2.addMonthsUTC(4);
            if (gdt1 <= gdt2) {
                return false;
            } else {
                return true;
            }
        }
    },

    type: 'EOSColourCodes'
};
 
Field style -
DollyM_0-1754457236460.png

 

1 ACCEPTED SOLUTION

Dolly M
Tera Guru

Thanks all,

I was able to get this sorted using Field Styles scripting.

 

DollyM_0-1754460825295.png

 

DollyM_1-1754460847997.png

 

DollyM_3-1754460882634.png

 

 

View solution in original post

8 REPLIES 8

Ankur Bawiskar
Tera Patron
Tera Patron

@Dolly M 

it's the usual method how you do from any server side script

you are passing current but not using it.

you should use current.sys_id in query to check for the current record and then check for the months logic

If my response helped please mark it correct and close the thread so that it benefits future readers.

Regards,
Ankur
Certified Technical Architect  ||  9x ServiceNow MVP  ||  ServiceNow Community Leader

Hi @Ankur Bawiskar ,

 

Do you means she should change this line- 

        server.addEncodedQuery('operational_statusIN1,9^u_os_eosISNOTEMPTY');
 
to addquery and pass current records sys_id here.
 
Regards,
Nikhil Bajaj
 
Please appreciate my efforts, help and support extended to you by clicking on – “Accept as Solution”; button under my answer. It will motivate me to help others as well.
Regards,
Nikhil Bajaj

kaushal_snow
Mega Sage

Hi @Dolly M ,

 

Unfortunately, Field Style scripts cannot directly invoke Script Include, they execute in a simplified context where GlideAjax or server-side calls aren’t supported. The field style syntax only allows inline expression logic and cannot call external server-side code. Instead, use a Client Script to call a client-callable Script Include via GlideAjax.

 

Try this once and let me know. If you find this helpful, please accept this as a solution and hit the helpful button..

 

 

Thanks and Regards,
Kaushal Kumar Jha - ServiceNow Consultant - Lets connect on Linkedin: https://www.linkedin.com/in/kaushalkrjha/

Dolly M
Tera Guru

Thanks all,

I was able to get this sorted using Field Styles scripting.

 

DollyM_0-1754460825295.png

 

DollyM_1-1754460847997.png

 

DollyM_3-1754460882634.png