Need to show VIP icon beside the requested for into the loaner order table.

lucky24
Tera Contributor

Hi Team,

I need to display the VIP icon beside "requested for" in the Loaner Asset Order table (sn_hamp_loaner_asset_order), which is part of the scoped application "Hardware Asset Management". 
I have created an on-load client script on the (sn_hamp_loaner_asset_order) table where I can fetch the values from the script include, but  I think "document.getElementsByClassName" is not working due to scoped application.

function onLoad() {
    alert("Loaner VIP");
    var ga = new GlideAjax('global.gsUtils');
    var reqFor = g_form.getValue('requested_for');
    ga.addParam('sysparm_name', 'getUser');
    ga.addParam('sysparm_user', reqFor);
    ga.getXML(callback);

    function callback(response) {
        alert("Loaner VIP Callback");
        var answer = response.responseXML.documentElement.getAttribute("answer");
        alert(answer);
        var user = JSON.parse(answer); //convert the json formatted string that is returned to an object
        alert(" Line 14");
        var reqforLabel = document.getElementsByClassName('col-xs-12 col-md-3 col-lg-4 control-label')[1]; //This label is the 2nd on the form
	        alert("Line 15");
        var reqforField = document.getElementById('sys_display.sn_hamp_loaner_asset_order.requested_for');
        alert("Loaner VIP 3");
        if (user.vip == '1' || user.vip == 1 || user.vip == true) {
          reqforLabel.setStyle({
                  backgroundImage: "url(images/icons/vip.gif)",
                 backgroundRepeat: "no-repeat",
                 backgroundPosition: "5% 45%"
             });


            reqforField.style.color = 'red';
        } else {
            reqforLabel.style.backgroundImage = '';
            reqforField.style.color = '';
        }

    }
}

 

After the line number 14 code is not running, can someone please help me here what is the issue?


2 REPLIES 2

Chaitanya ILCR
Kilo Patron

Hi @lucky24 ,

Uncheck the Isolate script check box and try

ChaitanyaILCR_1-1748531102995.png

 

try that and see 

 

try this script if still doesn't work 

keep lsolate script = false(unchecked)

use script

function onLoad() {
	var reqforLabel = $('label.sn_hamp_loaner_asset_order.requested_for');
	var reqforField = $('sys_display.sn_hamp_loaner_asset_order.requested_for');
	if (!reqforLabel || !reqforField)
		return;
	
	
	var ga = new GlideAjax('global.gsUtils');
    var reqFor = g_form.getValue('requested_for');
    ga.addParam('sysparm_name', 'getUser');
    ga.addParam('sysparm_user', reqFor);
    ga.getXMLA(vipCallerCallback);
}

function vipCallerCallback(user) {
	var reqforLabel = $('label.sn_hamp_loaner_asset_order.requested_for').down('label');
	var reqforField = $('sys_display.sn_hamp_loaner_asset_order.requested_for');
	if (!reqforLabel || !reqforField)
		return;
	
	//check for VIP status
	if (user.vip == '1' || user.vip == 1 || user.vip == 'true' || user.vip == true) {
		var bgPosition = "95% 55%";
		if (document.documentElement.getAttribute('data-doctype') == 'true')
			bgPosition = "5% 45%";
			
		reqforLabel.setStyle({backgroundImage: "url(images/icons/vip.gif)", backgroundRepeat: "no-repeat", backgroundPosition: bgPosition, paddingLeft: '30px' });
		reqforField.setStyle({color: "red"});
	} else {
		reqforLabel.setStyle({backgroundImage: ""});
		reqforField.setStyle({color: ""});
	}
}

 

Please mark my answer as helpful/correct if it resolves your query.

Regards,
Chaitanya

Ankur Bawiskar
Tera Patron
Tera Patron

@lucky24 

ensure Isolate Script = False for your client script.

Also for your reference you can refer the OOTB incident table client script for VIP

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