script to add picture to HTML variable?

gbunce
Kilo Expert

I am creating an order guide for our new hire process.

I have used reference fields to various catalog items to present to the user for selection.

Upon selecting from the list of devices (be it phone, PC, monitors, etc.) I update an HTML field with the description from the catalog item selected. (below script)

Is there a way I can also grab the picture from that catalog item and insert that into the description field prior to the text?

function onChange(control, oldValue, newValue, isLoading) {
    if (isLoading || newValue == '') {
          return;
    }

var desc = g_form.getValue('y2d_pick_pc_lsb');
var itm = new GlideRecord('sc_cat_item');
itm.addQuery('name', desc);
itm.query();
if(itm.next()) {
  g_form.setValue('y2d_pc_desc_htm',itm.description);
}
}

BTW, I should also add that this script will not work in the service portal so I need to change it to callback.

1 ACCEPTED SOLUTION

Brad Tilton
ServiceNow Employee
ServiceNow Employee

Apparently you can't do a g_form.getValue() on an image field. I know you can do it server side with gr.picture.getDisplayValue() so you could do a GlideAjax call for it, but that seems like overkill. Unfortunately, the only other way I could find would be to inspect the DOM for it, which could break during any release and won't work in service portal.



I would go with glideajax.


View solution in original post

4 REPLIES 4

Brad Tilton
ServiceNow Employee
ServiceNow Employee

Hi Greg,



I think you'd need to create an img tag in your html with the src="<sysid of attachment held in the picture field on the catalog item>.iix"


Thanks Brad, I can get that to work if I build out the url manually as such


"<img src=\"
https://xxx.service-now.com/a63feebe4f063240fccb76601310c7e3.iix\"><br
>"


but I can't seem to use itm.picture in the above script to return the value a63feebe4f063240fccb76601310c7e3.iix which appears to be what's in the 'picture' field on the record


Perhaps I just don't understand how an 'image' field works?


Brad Tilton
ServiceNow Employee
ServiceNow Employee

Apparently you can't do a g_form.getValue() on an image field. I know you can do it server side with gr.picture.getDisplayValue() so you could do a GlideAjax call for it, but that seems like overkill. Unfortunately, the only other way I could find would be to inspect the DOM for it, which could break during any release and won't work in service portal.



I would go with glideajax.


papalapapiricoi
Kilo Contributor

Use Server Script, for example:

 if (cat_item.next())
{
var item = {};
item.count = count.getAggregate('COUNT', 'u_sc_item_action.u_catalog_item');
item.name = cat_item.name.getDisplayValue();
item.short_description = cat_item.short_description.getDisplayValue();
item.picture = cat_item.picture.getDisplayValue();
item.sys_id = cat_item.sys_id.getDisplayValue();
items.push(item);
}