- 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 01:05 AM
You can Use ng-bind-html to Render HTML .
Note the below is tested in PDI and its working:-
Replace the part where the Description value is displayed with the following:
<div ng-bind-html="data.description"></div>
Widget Server Script
In the widget’s server script, fetch the Description field value and ensure it is returned as part of the data object:
(function() {
var modelId = $sp.getParameter('sys_id'); // Replace with your logic to get the model's sys_id
var gr = new GlideRecord('cmdb_model');
if (modelId && gr.get(modelId)) {
data.description = gr.getValue('description');
}
})();
Widget Client Script
To render HTML safely, inject the $sce service into the client script:
(function() {
// Inject Angular's $sce service
angular.module('sn.$sp').controller('myWidgetCtrl', function($scope, $sce) {
$scope.data.description = $sce.trustAsHtml($scope.data.description);
});
})();
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-20-2024 02:28 AM
Hi Ravi,
Thanks for the code, but unfortunately its not working.
Please see my code:
HTML:
<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">
<pre>{{item.description}}</pre>
</div>
</div>
</li>
</ul>
</div>
Server script:
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');
var desc = grInfo.description;
accessory.description = decodeHTML(desc);
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.
});
}
No code in the Client controller.
- 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-20-2024 01:09 AM
Hi @Onkar Pandav ,
To display text exactly as it is written in HTML, including spaces and line breaks, use the <pre> tag; this stands for "preformatted text" and ensures the text is rendered with its original formatting preserved.
<pre>
your text
here
test test
</pre>