Join the #BuildWithBuildAgent Challenge! Get recognized, earn exclusive swag, and inspire the ServiceNow Community with what you can build using Build Agent.  Join the Challenge.

Adding an image to table.

coolgirl
Mega Expert

Hello,

              I would like to add an image to a field in the table. I have chosen the field type as Image (the image icon is available in System UI -> Images) and set the business rule, but the image is not getting displayed in the table. Can someone help me how to go about it?

function onDisplay(current, g_scratchpad) {

  var gr = new GlideRecord('u_inventory');

  gr.addQuery('u_product_name',current.u_product_name);

  gr.query();

  while(gr.next()){

  if(gr.u_product_quantity == 0){

                    current.u_availability = "images/star.png";

  }

                            if(gr.u_product_quantity > 0){

                    current.u_availability = "images/pic2.jpg";

  }

  }

}

Thanks,

Sam

6 REPLIES 6

nagaraju8
Kilo Guru

Hi Sam,


Instead of business rule, try this it is working fine for me.



1. Create a new UI macro 'Example' and put some html there to keep the image in it. somewhat like <img src = 'abc.png'/>


2. Go To Formatters and create a new Formatter
Name - Incident Image      
Formatter - Example.xml


        Table - Incident


3. Go to form layout for Incident form and drag the "Incident Image" from available to selected bucket.



Thanks,


Nagaraju


Hi Nagaraju,


                              Thanks for your input. By using formatters, I am able to add an image to the records,but I would like to display an image based on an if condition and for that I guess I need to use business rule/client script. Have any idea on how to go about it?



Thanks,


Sam


Hi Sam,



You can use if statements in a UI macro using jelly



eg:


        <g2:evaluate jelly="true" expression="


                  var imageURL = 'images/star.png'


                  var current = new GlideRecord('yourtable');


                  current.addQuery('sys_id', jelly.jvar_sys_id);


                  current.setLimit(1);


                  current.query();


                  if (current.next()) {


                            var gr = new GlideRecord('u_inventory');


                            gr.addQuery('u_product_name',current.u_product_name);


                            gr.setLimit(1);


                            gr.query();


                            if(gr.next()){


                                      if(gr.u_product_quantity > 0){


                                                imageURL = 'images/pic2.jpg';


                                        }


                            }



                  }" />


<img src = '$[imageURL]'/>



(Haven't tested it but that's the idea anyway!)


Hi Ahmed,


                        I tried your code, but the condition is not working. And i would like the image to be displayed in a list view of the table records,for which I am unable to choose the formatter in the list layout.



Thanks,


Sam