It's all about the answers!

Ask a question

How to script a bolded string in Script Based Default Value?

Joann Luu (10710) | asked Mar 24 '15, 7:41 p.m.

I am trying to put a bolded string as a Default Value for the Description field, but only for one work item. Therefore, I have to use Script Based Default instead of Multi-Line HTML. How do I need to return the string to get it to show up bolded in the Description field? The Description field is a Large HTML. Cntl+B in the text field manually will work.

I have tried:

1) return "<b>Bold this string</b>";

2) return <b>"Bold this string"</b>;

3) return <b>'Bold this string'</b>;

4) return '<b>Bold this string</b>';

5) var str = "Bold this string";

    var bld = str.bold();

    return str;

None of these work. My colleagues have not had luck either. There's got to be a way. Please help.

One answer

permanent link
Ralph Schoon (63.3k33646) | answered Mar 25 '15, 3:42 a.m.
edited Mar 25 '15, 3:47 a.m.
As far as I can tell, the syntax is to return a string. Like

return "This is a string";

The Description attribute is of type large HTML. Technically the content syntax is HTML. So the return value would be "&lt;b&gt;bold text&lt;/b&gt;". This will be the content of the description if you boldfaced the text.

However, this won't work. The description will show "&lt;b&gt;bold text&lt;/b&gt;". The reason behind this is that the field is internally managed as XML. To set the XML field from a string, there are two methods. One is to create a valid XML from a plain string. This is the one that apparently has been used. The other one is to expect a valid XML and just put it in.

The method that has been chosen changes special characters like < and > into escape sequences. This allows to return something like "1 &lt; 2" without causing an invalid content. Unfortunately the consequence is that you can't really return HTML syntax, because the tags will return with escaped &lt; and &gt; and this results in the result above.

The full HTML syntax that I am aware of is

return "Plain text&lt;br/&gt;&lt;b&gtbold text&lt;/b&gt&lt;br/&gt&lt;i&gtitalic text&lt;/i&gt&lt;br/&gt&lt;a href=””&gt;Forum Link&lt;/a&gt&lt;br/&gtUser link to &lt;b&gt@ralph &lt;/b&gt&lt;br/&gtWork Item link to Defect 3 &lt;br/&gt"

However, this does not work with the method chosen for implementation. Instead

return "Plain text”; User link to @ralph;  Work Item link to Defect 3"

will result in a valid link, user mentions and work item link being created. This unfortunately does not allow to create breaks or bold face text however.

The chosen method allows user with almost no experience return their texts and can't corrupt the attribute value, however, It does not allow to use the full HTML syntax.

Ralph Schoon commented Mar 25 '15, 3:48 a.m.

Please note, I had to escape the special characters in the answer as well, because otherwise the whole syntax would have been messed up. This is a common problem......

Joann Luu commented Mar 25 '15, 7:58 p.m. | edited Mar 26 '15, 3:26 a.m.

Thank you for the response. Just to make sure I understand. Are you saying, it is not possible to create bolded text in a Scripted Default Value? sad face.

Ralph Schoon commented Mar 26 '15, 3:28 a.m. | edited Mar 26 '15, 3:29 a.m.

I believe, from my observations and the description above, that it is not possible to create HTML tags in descriptions with JavaScript today. This is due to how a string is converted and stored. This makes it impossible to create bold text using scripts as well.

Your answer

Register or to post your answer.

Dashboards and work items are no longer publicly available, so some links may be invalid. We now provide similar information through other means. Learn more here.