how do we display images dynamicaly in catalog item
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
02-13-2019 10:57 PM
Friends any idea..how do we display images dynamicaly in catalog item..Ex when I select device it should also change image at the place of test image..I tried with UI macro but not working.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
04-07-2020 10:45 PM
I have a same requirement. Any update on this.
Regards,
Pankaj
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
04-07-2020 10:54 PM
Hi,
Write an onchange client script on your select box field.
Script:
// Get all the images of your document
var image =document.images;
image[<position where your current image is>].src = <URL of your new image>;
//Lets say I want to replace the below iphone image to attachment clip icon.
var image =document.images;
alert(image[3].src);
image[4].src = 'https://demo010.service-now.com/images/icons/attachment.gifx';
Kindly mark it correct and helpful if it solves your issue.
Thanks,
Priyanka

- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
01-15-2021 02:08 PM
Hi,
I had the same challenge. Select a model in a variable and then display the image and description for the selected model.
- Create a Model Variable of type reference to the cmdb_model table.
- Create a Ajax script include to retrieve the data for the model when it is selected.
- Create a Macro variable that has a macro that displays and updates the image and description.
- You'll need a macro for the standard interface.
- You'll need a widget for the portal interface.
- Create an On Change script for the Model variable that calls the AJAX script include and calls a function on the macro to update the image and the short description.
Code for the AJAX Script Include
Shout out to Amandosierra, who's reply to this community question explains how to retrieve the picture information.
var VariableModelUtilsAjax = Class.create();
VariableModelUtilsAjax.prototype = Object.extendsObject(AbstractAjaxProcessor, {
getModelInfo: function() {
var modelId = this.getParameter('sysparm_model');
var result = this._getModelInfo(modelId);
var resultString = JSON.stringify(result);
var xmlDoc = this.getDocument();
var root = this.getRootElement();
var item = xmlDoc.createElement("model_data");
var textNode = xmlDoc.createTextNode(resultString);
item.appendChild(textNode);
root.appendChild(item);
},
_getModelInfo: function(modelId) {
if (typeof scriptName == "undefined") {
// we will add the scriptName to the function Name
scriptName = this.type;
}
var functionName = "Function: getModelInfo\n";
functionName = scriptName + functionName;
try {
// Put content here
var data = {};
var gr = new GlideRecord("cmdb_model");
if (gr.get(modelId)) {
data.name = gr.getValue('name');
data.picture = gr.getDisplayValue('picture');
data.description = gr.getValue('description');
data.short_description = gr.getValue('short_description');
}
return data;
} catch (e) {
// enter what you want to happen if there is an error here
var errorMessage = functionName + e;
gs.addInfoMessage(errorMessage);
gs.log(errorMessage);
}
},
type: 'VariableModelUtilsAjax'
});
Code for the Macro Variable Macro
<?xml version="1.0" encoding="utf-8" ?>
<j:jelly trim="false" xmlns:j="jelly:core" xmlns:g="glide" xmlns:j2="null" xmlns:g2="null">
<div id="itamModelImageDiv" >
<table>
<tr>
<td> <img id="itamModelImage" style="display: none;" src=""/>
</td>
<td style="vertical-align:top">
<div id="itamModelDescription" ></div>
</td>
</tr>
</table>
</div>
<script>
function itamUpdateImage(imageId, description){
var imageDiv = document.getElementById("itamModelImageDiv");
var image = document.getElementById("itamModelImage");
image.style.display = "none";
var src = imageId + "?t=large";
image.src = src;
var divPadding = "0px";
if(imageId != ""){
image.style.display = "block";
divPadding = "15px"
}
var descriptionNode = document.getElementById("itamModelDescription");
descriptionNode.innerHTML = description;
descriptionNode.style.paddingLeft = divPadding;
}
</script>
</j:jelly>
Code for the Macro Variable Widget
Put this in the widget's HTML Template field. Because the script is in the template field, it can be called from the On Change catalog client script.
<div id="itamModelImageDiv" >
<table>
<tr>
<td> <img id="itamModelImage" style="display: none;" src=""/>
</td>
<td style="vertical-align:top">
<div id="itamModelDescription" ></div>
</td>
</tr>
</table>
</div>
<script>
function itamUpdateImage(imageId, description){
var imageDiv = document.getElementById("itamModelImageDiv");
var image = document.getElementById("itamModelImage");
image.style.display = "none";
var src = imageId + "?t=large";
image.src = src;
var divPadding = "0px";
if(imageId != ""){
image.style.display = "block";
divPadding = "15px"
}
var descriptionNode = document.getElementById("itamModelDescription");
descriptionNode.innerHTML = description;
descriptionNode.style.paddingLeft = divPadding;
}
</script>
Code for the Model On Change script
function onChange(control, oldValue, newValue, isLoading) {
if (isLoading || newValue == '') {
return;
}
var model = newValue;
var ga = new GlideAjax("VariableModelUtilsAjax");
ga.addParam("sysparm_name", "getModelInfo");
ga.addParam("sysparm_model", model);
ga.getXML(getModelData);
//Type appropriate comment here, and begin script below
function getModelData(response) {
var xmlDoc = response.responseXML;
var modelData = xmlDoc.getElementsByTagName("model_data")[0];
var modelObjectText = modelData.childNodes[0].nodeValue;
var modelObject = JSON.parse(modelObjectText);
//alert("itamUpdateImage");
itamUpdateImage(modelObject.picture, modelObject.description);
//alert(modelObject.picture);
}
}
Hope this helps.
Let me know if you have any qeustions.
Thanks,
Cody
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
10-02-2021 08:40 PM
Hi
Thank you very much and well thought!!
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
02-01-2021 09:24 PM
All
I have a similar requirement on the Service Portal form. From what i have tried Macros dont work on Portals. Any suggestions on getting this working on the Portal?
Thanks
Aman