- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
12-19-2024 11:56 PM
Hello,
I have some text in the 'Description' field of the 'Product Model [cmdb_model]' table.
Now, I want to show same field value in the service portal widget with proper formatting but it is showing value along with HTML tags.
Please see below screen shots.
Service portal widget value:
I was able to remove the tags but, I want that text should have same formatting as that of actual/backend field.
How to achieve that in the widget??
Solved! Go to Solution.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
12-20-2024 10:26 PM
Hello @Onkar Pandav
Use the below code:-
<div class="panel-body">
<ul class="grid-list" role="list">
<li class="item-card-column" ng-repeat="item in data.accInfo">
<div class="short-desc">
{{item.short_description}}
</div>
<div class="row">
<div class="column1">
<img ng-src="{{item.image+'.iix'}}" style="width:-webkit-fill-available;" />
</div>
<div class="column2">
<div ng-bind-html="item.description"></div>
</div>
</div>
</li>
</ul>
</div>
Add client logic
function($scope, $sce) {
$scope.data.accInfo.forEach(function(item) {
item.description = $sce.trustAsHtml(item.description);
});
}
server
data.accSysId = $sp.getParameter("sysId");
data.accInfo = [];
var grInfo = new GlideRecord('cmdb_model');
grInfo.addQuery('sys_id', data.accSysId);
grInfo.query();
if (grInfo.next()) {
var accessory = {};
accessory.sysid = grInfo.getValue('sys_id');
accessory.name = grInfo.name.getDisplayValue();
accessory.model = grInfo.model_number.toString();
accessory.price = grInfo.cost.toString();
accessory.short_description = grInfo.getValue('short_description');
accessory.image = grInfo.getValue('picture');
accessory.description = grInfo.getValue('description'); // Raw HTML description
data.accInfo.push(accessory);
}
If you found my response helpful, I would greatly appreciate it if you could mark it as "Accepted Solution" and "Helpful."
Your support not only benefits the community but also encourages me to continue assisting. Thank you so much!
Thanks and Regards
Ravi Gaurav | ServiceNow MVP 2025,2024 | ServiceNow Practice Lead | Solution Architect
CGI
M.Tech in Data Science & AI
YouTube: https://www.youtube.com/@learnservicenowwithravi
LinkedIn: https://www.linkedin.com/in/ravi-gaurav-a67542aa/
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
12-21-2024 02:10 AM
Hello @Onkar Pandav
Here is the updated script that will meet your requirement:
Server Script:
(function () {
data.accSysId = $sp.getParameter("sysId");
data.accInfo = [];
var grInfo = new GlideRecord('cmdb_model');
grInfo.addQuery('sys_id', data.accSysId);
grInfo.query();
if (grInfo.next()) {
var accessory = {};
accessory.sysid = grInfo.getValue('sys_id');
accessory.name = grInfo.name.getDisplayValue();
accessory.model = grInfo.model_number.toString();
accessory.price = grInfo.cost.toString();
accessory.short_description = grInfo.getValue('short_description');
accessory.image = grInfo.getValue('picture');
accessory.description = grInfo.getDisplayValue('description');
gs.addInfoMessage(accessory.description); //to debug only
data.accInfo.push(accessory);
}
function decodeHTML(str) {
var a = str.replace(/<\/?[^>]+(>|$)/g, ""); //Remove tags
var b = a.replace(/&/g, '&'); //Retain any ampersands that are just ampersands
return b.replace(/&#(\d+);/g, function(match, dec) {
return String.fromCharCode(dec); //Returns the special character from the decimal code representation and returns the entire decoded string.
});
}
})();
HTML Script:
<div class="panel-body">
<ul class="grid-list" role="list">
<li class="item-card-column" ng-repeat="item in data.accInfo">
<div class="short-desc"> {{item.short_description}} </div>
<div class="row">
<div class="column1">
<img ng-src="{{item.image+'.iix'}}" style="width:-webkit-fill-available;"/>
</div>
<div class="column2">
<div ng-bind-html="::data.accInfo[0].description"></div>
</div>
</div>
</li>
</ul>
</div>
Result:
Note:
- No need to add decodeHTML
- <div ng-bind-html="::data.accInfo[0].description"></div> will render the description field value with same format
Hope this helps!
"If you found my answer helpful, please like and mark it as an "accepted solution". It helps others find the solution more easily and supports the community!"
Thank You
Juhi Poddar
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
12-20-2024 02:23 AM
Hi Tejas,
<pre> is also not working. Please see below screen shot.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
12-21-2024 02:10 AM
Hello @Onkar Pandav
Here is the updated script that will meet your requirement:
Server Script:
(function () {
data.accSysId = $sp.getParameter("sysId");
data.accInfo = [];
var grInfo = new GlideRecord('cmdb_model');
grInfo.addQuery('sys_id', data.accSysId);
grInfo.query();
if (grInfo.next()) {
var accessory = {};
accessory.sysid = grInfo.getValue('sys_id');
accessory.name = grInfo.name.getDisplayValue();
accessory.model = grInfo.model_number.toString();
accessory.price = grInfo.cost.toString();
accessory.short_description = grInfo.getValue('short_description');
accessory.image = grInfo.getValue('picture');
accessory.description = grInfo.getDisplayValue('description');
gs.addInfoMessage(accessory.description); //to debug only
data.accInfo.push(accessory);
}
function decodeHTML(str) {
var a = str.replace(/<\/?[^>]+(>|$)/g, ""); //Remove tags
var b = a.replace(/&/g, '&'); //Retain any ampersands that are just ampersands
return b.replace(/&#(\d+);/g, function(match, dec) {
return String.fromCharCode(dec); //Returns the special character from the decimal code representation and returns the entire decoded string.
});
}
})();
HTML Script:
<div class="panel-body">
<ul class="grid-list" role="list">
<li class="item-card-column" ng-repeat="item in data.accInfo">
<div class="short-desc"> {{item.short_description}} </div>
<div class="row">
<div class="column1">
<img ng-src="{{item.image+'.iix'}}" style="width:-webkit-fill-available;"/>
</div>
<div class="column2">
<div ng-bind-html="::data.accInfo[0].description"></div>
</div>
</div>
</li>
</ul>
</div>
Result:
Note:
- No need to add decodeHTML
- <div ng-bind-html="::data.accInfo[0].description"></div> will render the description field value with same format
Hope this helps!
"If you found my answer helpful, please like and mark it as an "accepted solution". It helps others find the solution more easily and supports the community!"
Thank You
Juhi Poddar
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
12-22-2024 04:49 AM
Hey Ravi & Juhi,
Thank you so much. You guys are awesome!!
Appreciate your help!
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
12-22-2024 04:51 AM
Hello @Onkar Pandav
You are most welcome!
I am glad that I was able to help you resolve the issue.
Thank You
Juhi Poddar