Client Script not working

suresh kaliappa
Tera Expert

Hi All , 

 

In sc_req_item table. when we open a form for an existing record then if requested for user is vip then the vip icon should display left to the field. But i tried the script and it was not working. Hence kindly help someone and correct the code if i missed anything. 

 

Note : i unchecked isolate script checkbox. 

 

function onLoad() {
 var callerLabel = $('label.sc_req_item.requested_for');
    if (!callerLabel || !callerField)
        return;
   
    if (!newValue) {
        callerLabel.setStyle({backgroundImage: ""});
        callerField.setStyle({color: ""});
        return;
    }
    g_form.getReference('requested_for', vipCallerCallback);
}

function vipCallerCallback(caller) {
    var callerLabel = $('label.sc_req_item.requested_for').down('label');
    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

Ankur Bawiskar
Tera Patron
Tera Patron

@suresh kaliappa 

you didn't copy the code correctly from the OOTB incident VIP caller client script

Also that OOTB client script is onChange but you want onLoad so couple of lines I removed

Working Script:

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', vipCallerCallback);
}

function vipCallerCallback(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'
        });
        callerField.setStyle({
            color: "red"
        });
    } else {
        callerLabel.setStyle({
            backgroundImage: ""
        });
        callerField.setStyle({
            color: ""
        });
    }
}

Output:

AnkurBawiskar_0-1748335432360.png

 

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

View solution in original post

3 REPLIES 3

Ankur Bawiskar
Tera Patron
Tera Patron

@suresh kaliappa 

did you debug by adding alert?

is that field requested_for available on the form and is populated?

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

Ankur Bawiskar
Tera Patron
Tera Patron

@suresh kaliappa 

you didn't copy the code correctly from the OOTB incident VIP caller client script

Also that OOTB client script is onChange but you want onLoad so couple of lines I removed

Working Script:

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', vipCallerCallback);
}

function vipCallerCallback(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'
        });
        callerField.setStyle({
            color: "red"
        });
    } else {
        callerLabel.setStyle({
            backgroundImage: ""
        });
        callerField.setStyle({
            color: ""
        });
    }
}

Output:

AnkurBawiskar_0-1748335432360.png

 

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

Chaitanya ILCR
Kilo Patron

Hi @suresh kaliappa ,

 

Make it onChange and uncheck the isolate script 

Refer screenshot below

 

 

ChaitanyaILCR_0-1748335869622.png

 

script:

function onChange(control, oldValue, newValue, isLoading) {
    var reqForLabel = $('label.sc_req_item.requested_for');
	var reqForField = $('sys_display.sc_req_item.requested_for');

    if (!reqForLabel || !reqForField)
        return;

    if (!newValue) {
        reqForLabel.setStyle({
            backgroundImage: ""
        });
        reqForField.setStyle({
            color: ""
        });
        return;
    }
    g_form.getReference('requested_for', vipCallerCallback);
}

function vipCallerCallback(caller) {
    var reqForLabel = $('label.sc_req_item.requested_for').down('label');
	var reqForField = $('sys_display.sc_req_item.requested_for');

    if (!reqForLabel || !reqForField)
        return;

    //check for VIP status
    if (caller.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