Help with background script

Abhijit Das7
Tera Expert

Hi Team, 

 

I have requirement where I have to populate clickable Link in string field on alm_asset table using background script.

 

Field: u_instruction (String field)

 

This code is not working; the link is non clickable. 

var linkURL = 'https://example.com';
var link = '<a href="' + linkURL + '" target="_blank">' + linkURL + '</a>';

var message =
    'This customer has unique requirements – Refer to Read Me First document located in Library before servicing device(s). ' +
    'Live link:' + link;

var gr = new GlideRecord("alm_asset");
gr.addEncodedQuery(encodedQuery);
gr.query();

while (gr.next()) {
    gr.u_instructions = "";
    gr.update();
}

 

Thanks in advance.

 

 

1 ACCEPTED SOLUTION

Ankur Bawiskar
Tera Patron

@Abhijit Das7 

you can't add link in a string field. that's OOTB limitation.

It clearly says it's string field and won't support HTML tags/links.

💡 If my response helped, please mark it as correct and close the thread 🔒— this helps future readers find the solution faster! 🙏

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

View solution in original post

3 REPLIES 3

GlideFather
Tera Patron

Hi @Abhijit Das7,

 

to have a clickable link, you need to add it between the [code] tags

 

[code]<a href="https://www.yourlink.com">Link Name</a>[/code]

 

 

Also, if you plan to do it via background script, perhaps create a fix script (it is pretty much the same thing, giving you option to save that code).

 

EDIT: your code will clear the u_instruction field, is that on purpose?

var gr = new GlideRecord("alm_asset");
gr.addEncodedQuery(encodedQuery);
gr.query();

while (gr.next()) {
    gr.u_instructions = "";
    gr.update();
}

 

EDIT 2: 

As Ankur stated in another reply, this is not possible for a string field. What I shared is applicable for journal fields only - Work Notes or Additional Comment, not any other field...

GlideFather_0-1771428552328.png

 

_____
100 % GlideFather experience and 0 % generative AI

Ankur Bawiskar
Tera Patron

@Abhijit Das7 

you can't add link in a string field. that's OOTB limitation.

It clearly says it's string field and won't support HTML tags/links.

💡 If my response helped, please mark it as correct and close the thread 🔒— this helps future readers find the solution faster! 🙏

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

Tanushree Maiti
Mega Sage

Hi @Abhijit Das7 ,

 

As Other expert said  String is not allowed for showing clickable link. Standard string fields will display HTML code as plain text only. 

 
To get a clickable link in a table field using a background script in ServiceNow, you must ensure the field is either a URL type or an HTML type field.
 
The recommended approach is to change the field's type in the dictionary or use an HTML field and format the content with an HTML anchor tag (<a>) in your background script.
 
Sample: 

Create a field u_link Field with type  html. In client script, when creating the link, create links with <a href="url"></a>, try something like this:

for (i=0; i<arr.length; i++) {


var url = "<a href="https://www.servicenow.com" + arr[i] + "></a> ";

g_form.setValue('u_link", url);

}

Please mark this response as Helpful & Accept it as solution if it assisted you with your question.
Regards
Tanushree Maiti
ServiceNow Technical Architect
Linkedin: