Onload script error:- TypeError: $ is not a function

ChanchalG
Tera Expert

Hi,

There is onload script that is giving error on portal as "TypeError: $ is not a function". I have marked the isolate script false but still getting the same error.

Here is the code for you:-

function onLoad() {
    var callerLabel = $('label.sc_req_item.requested_for');
    var callerField = $('sys_display.sc_req_item.requested_for');
    if (!callerLabel || !callerField)
        return;
    g_form.getReference('requested_for', vipCallerCallbackREQ);
}
function vipCallerCallbackREQ(caller) {
    var callerLabel = $('label.sc_req_item.requested_for').down('label');
    var callerField = $('sys_display.sc_req_item.requested_for');
    if (!callerLabel || !callerField)
        return;

    //check for VIP status
    if (caller.vip == 'true') {
        var bgPosition = "95% 55%";
        if (document.documentElement.getAttribute('data-doctype') == 'true')
            bgPosition = "5% 45%";

        callerLabel.setStyle({
            backgroundImage: "url(images/icons/vip.gif)",
            backgroundRepeat: "no-repeat",
            backgroundPosition: bgPosition,
            paddingLeft: '30px'
        });
        //set callerField color
        callerField.setStyle({
            color: "red"
        });
    } else {
        callerLabel.setStyle({
            backgroundImage: ""
        });
        callerField.setStyle({
            color: ""
        });
    }
}
4 REPLIES 4

Ankur Bawiskar
Tera Patron
Tera Patron

@ChanchalG 

the client script looks similar to client script on User table for caller

the above client script is on which table?

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

Its on Requested Item [sc_req_item] table.

@ChanchalG 

is requested_for field on RITM form or not?

what debugging did you perform?

code is running till which line?

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

Robert H
Mega Sage

Hello @ChanchalG ,

 

There's a lot going on in that script that is against ServiceNow best practices, e.g. DOM manipulation and usage of g_form.getReference().

It seems you want to highlight the "Requested for" user field when the selected user is a VIP. Please consider the following alternative approach:

 

Step 1: create a Business Rule

  • Table: sc_req_item
  • Advanced: true
  • When: display
  • Script:
(function executeRule(current, previous /*null when async*/) {

	g_scratchpad.vip = current.requested_for.vip.toString() === 'true';

})(current, previous);​

Step 2: remove all code from your Client Script and replace it with this:

function onLoad() {
    if (g_scratchpad.vip) {
        g_form.addDecoration('requested_for', 'icon-star', 'VIP', 'color-red');
    }
}

Result:

RobertH_0-1743252739164.png

 

Regards,

Robert