Join the #BuildWithBuildAgent Challenge! Get recognized, earn exclusive swag, and inspire the ServiceNow Community with what you can build using Build Agent.  Join the Challenge.

Adapting OOTB "Highlight VIP Caller" client script for SC_TASK

Abbottronix
Tera Guru

I'm trying to adapt the OOTB "Highlight VIP Caller" client script to apply to the Requested By (opened_by) and Requested For (request_item.requested_for) fields on the sc_task table. I copied the original script and swapped out the table and fields, but I'm getting this error:

 

Abbottronix_0-1751006993831.png

 

This is the script I'm using for the Requested By (opened_by) field:

Abbottronix_1-1751007147914.png

 

function onChange(control, oldValue, newValue, isLoading) {
	var callerLabel = $('label.sc_task.opened_by');
	var callerField = $('sys_display.sc_task.opened_by');
	if (!callerLabel || !callerField)
		return;
	
	if (!newValue) {
		callerLabel.setStyle({backgroundImage: ""});
		callerField.setStyle({color: ""});
		return;
	}
	g_form.getReference('opened_by', vipCallerCallback);
}

function vipCallerCallback(caller) {
	var callerLabel = $('label.sc_task.opened_by').down('label');
	var callerField = $('sys_display.sc_task.opened_by');
	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' });
		callerField.setStyle({color: "red"});
	} else {
		callerLabel.setStyle({backgroundImage: ""});
		callerField.setStyle({color: ""});
	}
}
1 ACCEPTED SOLUTION

HI @Abbottronix ,

 

if the field is made read-only using UI Policy or client script

update your script like this

function onChange(control, oldValue, newValue, isLoading) {
	var callerLabel = $('label.sc_task.opened_by');
	var callerField = $('sys_display.sc_task.opened_by');
	if (!callerLabel || !callerField)
		return;
	
	if (!newValue) {
		callerLabel.setStyle({backgroundImage: ""});
		callerField.setStyle({color: ""});
		return;
	}
	g_form.getReference('opened_by', vipCallerCallback);
}

function vipCallerCallback(caller) {
	var callerLabel = $('label.sc_task.opened_by').down('label');
	var callerField = $('sys_display.sc_task.opened_by');
	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' });
		g_form.setReadOnly('opened_by',false);
		callerField.setStyle({color: "red"});
		g_form.setReadOnly('opened_by',true);

	} else {
		callerLabel.setStyle({backgroundImage: ""});
		callerField.setStyle({color: ""});
	}
}

 

if the field is made read-only dictionary level (try to make it read-only using UI policy or client script)

if  not looks like there is no workaround for adding styles like color for fields which are read-only at the dictionary leve

function onChange(control, oldValue, newValue, isLoading) {
    if (isLoading) {
        g_form.setReadOnly('opened_by', true);
    }
    var callerLabel = $('label.sc_task.opened_by');
    var callerField = $('sys_display.sc_task.opened_by');
    if (!callerLabel || !callerField)
        return;

    if (!newValue) {
        callerLabel.setStyle({
            backgroundImage: ""
        });
        callerField.setStyle({
            color: ""
        });
        return;
    }
    g_form.getReference('opened_by', vipCallerCallback);
}

function vipCallerCallback(caller) {
    var callerLabel = $('label.sc_task.opened_by').down('label');
    var callerField = $('sys_display.sc_task.opened_by');
    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'
        });
        g_form.setReadOnly('opened_by', false);
        callerField.setStyle({
            color: "red"
        });
        g_form.setReadOnly('opened_by', true);

    } else {
        callerLabel.setStyle({
            backgroundImage: ""
        });
        callerField.setStyle({
            color: ""
        });
    }
}

 

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

Regards,
Chaitanya

View solution in original post

7 REPLIES 7

Abbottronix_1-1751430235452.png

 

Perfect! Thank you, you've been so helpful!

 

pavani_paluri
Tera Guru
Tera Guru

Hi Abbott,

Could you Please try using below:
Instead of: var callerField = $('sys_display.sc_task.opened_by');
Use: var callerField = document.getElementById('sys_display.sc_task.opened_by');
And for the label:var callerLabel = document.querySelector('label[for="sc_task.opened_by"]');

 

Mark it helpful if this helps you to understand. Accept solution if this give you the answer you're looking for
Kind Regards,
Pavani P

It's now giving me this error 

Abbottronix_0-1751249113313.png