Is there any way to manipulate field label or field in a scoped app?

Ulf Cederholm
Tera Expert

Apparently the standard dom manipulaton used for for example the VIP icon doesn't work in scoped apps.
https://www.servicenow.com/community/developer-forum/isolate-script-false-and-dom-manipulation-not-w...

 

Does anyone know of a good way to get a retina icon or small picture next to a field in a scoped app using a client script?

 

I can change label color and field color using something like the below code but I haven't found a good way to add icons next to the field.

 

var label = g_form.getLabel('<table>.<field>');
var field = g_form.getControl('sys_display.<table>.<field>');
label.style.color = 'red';
field.style.color = 'red';

 

I can pretty much match the line from Servicenows oob VIP client script on the incident table:
callerLabel.setStyle({backgroundImage: "url(images/icons/vip.gif)", backgroundRepeat: "no-repeat", backgroundPosition: bgPosition, paddingLeft: '30px' });

 

By adding these lines (not sure if the order matters?) to my code above.
label.style.backgroundRepeat = 'no-repeat';
label.style.backgroundPosition = '75% 50%';
label.style.paddingLeft = '30px';
label.style.backgroundImage = 'url(images/icons/vip.gif)';

 

To 'clear' any styling I made I simply use code such as below (thought I'll add this just in case it can help someone).

label.style.color = '';
label.style.backgroundImage = '';

 

I'm hoping though that there is a more efficient/better way to solve this. Any suggestions?

 

Edit: To add a one thing. According to this post field styles doesn't work on reference fields. Not sure if this has changed since 2018 because my code above is applied on a reference field?

https://www.servicenow.com/community/developer-forum/how-to-get-styles-to-work-on-a-form-field-not-j...

 

2 REPLIES 2

Ankur Bawiskar
Tera Patron
Tera Patron

@Ulf Cederholm 

I have seen scripts in scoped app using DOM manipulation.

Did you allow the property with true value for your scoped app? glide.script.block.client.globals

Regards,
Ankur
Certified Technical Architect  ||  9x ServiceNow MVP  ||  ServiceNow Community Leader

Ulf Cederholm
Tera Expert

We didn't have any 'glide.script.block.client.globals' properties before but I've tried to add it both in global and in the scope and set to false. In addition I have made sure the 'Isolate script' field is set to false. Even if that shouldn't be necessary if using the property?

 

The problem were I had was that I could not (and still can't) get <$()> to work in client scripts in scoped apps (as well as <windows> and <document> from what it seems). But the changes I show in my post works. Not sure if they count as DOM manipulation or no though? I was hoping there were a 'better/more elegant' way to do this so I guess that is my question here I suppose.

 

If you have a way to get <$()> to work in scoped apps that would be a great bonus though.