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

Chaitanya ILCR
Mega Patron

Hi @Abbottronix 

Uncheck the isolate script checkbox of the client script 

 

 

Regards 

Chaitanya 

Hi @Abbottronix ,

 

you script works I have tried it on the opened_by field on the sc_task table 

just make the isolate script field false on the client script record

ChaitanyaILCR_0-1751249779483.png

 

Thanks for marking my response as helpful

 

if you find it helpful and your issue is resolved, could you please mark it as a solution and close the thread as it would help the future readers

 

Regards

Chaitanya

That almost fixed everything, only remaining problem is that the text is showing like this:

Abbottronix_1-1751250643511.png

When it should be showing up red like this:

Abbottronix_2-1751250692552.png

 

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