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.

Genarate the barcode for the multiple asset

KumarAyush6265
Tera Contributor

I face some issue in barcode genration for the multiple asset selected from the  list view so please give me any workaround ?

1 ACCEPTED SOLUTION

Sanjay191
Tera Sage
Tera Sage

Hello @KumarAyush6265   Please refer below code and let me if anyerror has occur

UI Page:-

<?xml version="1.0" encoding="utf-8" ?>
<j:jelly trim="false" xmlns:j="jelly:core" xmlns:g="glide" xmlns:j2="null" xmlns:g2="null">
    <j:set var="jvar_alm_asset_id" value="${sysparm_alm_asset}" />
    <g:include_script src="jsbarcode.jsdbx" />

    <script>
        count=0;
    </script>

    <g:evaluate var="sysparm_ids">
        <!-- var sysparm_ids =
        
        var sysparm_ids = RP.getParameterValue("sysparm_id");
        sysparm_ids;
    </g:evaluate>

    <j:forEach var="sys_id" items="${sysparm_ids.split(',')}">
        <g:evaluate var="jvar_record" object="true" jelly="true">
            var gr = new GlideRecord("alm_hardware");
            gr.get("${sys_id}");
            gr;
        </g:evaluate>
        <g:evaluate var="jvar_asset_tag" object="true" jelly="true">
            var gr = new GlideRecord("alm_hardware");
            gr.get("${sys_id}");
            var value = gr.getDisplayValue('asset_tag').toString();
            value;
        </g:evaluate>
       

        <input type='hidden' name='assetTag' id="assetTag" value="${jvar_asset_tag}" />
        <style>
            td {
                font-size: 8px;
                text-align: center;
                vertical-align: top;
            }


            @media print {
                @page {
                    margin: 0;
                }
            }
        </style>


        <table style="width:100%; height:100%">
            <tr>
                <td colspan="2">
                    <!-- Create a valid svg element with width and height attributes -->
                    <svg id="barcode" width="200" height="50"></svg>
                </td>
            </tr>
            <tr>
                <td>Model: ${jvar_record.getDisplayValue('model')}</td>
                <td>Serial No: ${jvar_record.getDisplayValue('serial_number')}</td>
            </tr>
        </table>

        <script>
            //alert( '${jvar_record.getDisplayValue('asset_tag')}');
        var barcode ='${jvar_record.getDisplayValue('asset_tag')}';
       
var labelText = "Asset tag: " +  '${jvar_record.getDisplayValue('asset_tag')}'; //"  Host Name::"  +  gel("hostName").value;//serialNO  Asset
// Link to JsBarcode docs and options: http://lindell.me/JsBarcode/
var barbi = JsBarcode("#barcode", barcode, { format: "CODE39", width: 1, height: 30, margin: 1, textMargin: 1, textAlign: "left", fontSize: 8, text: labelText });
barbi._renderProperties[0].element.id = barbi._renderProperties[0].element.id + count ;
// console.log(barbi._renderProperties[0].element.id);
// console.log(count++);
        </script>
    </j:forEach>
</j:jelly>

UI Page -

 function printBarCodeLabel(){
  var dialog = new GlideDialogWindow('printBarCodeLabel');
    dialog.setTitle('Barcode');
    dialog.setPreference('sysparm_id',g_list.getChecked());
    dialog.render();

}

and please install the ui script jsBarcode as libarary to genrate the barcode 


View solution in original post

19 REPLIES 19

See below it showing  unique barcode but same isssue i get  but at that time i have some asset_tag is empty  on the asset so for the apply the check condition to avoid that mistake 

Sanjay191_0-1723639315454.png

 

Hey Sanjay, 

Thank you for your help. The problem was that I was using a different UI script which was causing this issue. But Now I am able to generate unique Barcodes. Thank you for helping me throughout.

Please mark the helpful and Thank You

ninadpatwardhan
Tera Contributor

Hey @Sanjay191 .

Below is the code of my UI page:

XML:

<?xml version="1.0" encoding="utf-8" ?>
<j:jelly trim="false" xmlns:j="jelly:core" xmlns:g="glide" xmlns:j2="null" xmlns:g2="null">
    <g:include_script src="jsbarcode.jsdbx" />

    <script>
        function download_list() {
            var html = document.getElementById('html_content').innerHTML;
            html = html.toString();
            var ga = new GlideAjax('PDF_util');
            ga.addParam('sysparm_name', 'printPDF');
            ga.addParam('sysparm_html', html);
            ga.addParam('sysparm_sysid', g_form.getUniqueValue());
            ga.addParam('sysparm_number', g_form.getValue('number'));
            ga.getXMLAnswer(showResult);

            function showResult(response) {
                alert(response);
                location.reload();
            }
            GlideDialogWindow.get().destroy();
        }
        function closeWindow() {
            GlideDialogWindow.get().destroy();
        }

        // var barcode = barcode + '${jvar_record.getDisplayValue('number')}';
        // var barbi =
        // JsBarcode("#barcode", barcode,
        // { format: "CODE39", width: 1, height: 30, margin: 1, textMargin: 5, textAlign: "left", fontSize: 8});
       
    </script>
   
    <g:evaluate jelly="true">
        var srno = 0;
        <!-- page page_properties -->
        var page_properties = new GlideRecord('u_generate_pdf_table');
        page_properties.get('8eb9b968833bc2103c33fdc6feaad33d');
        var label_per_row = parseInt(page_properties.getValue('u_label_per_row'));
        var label_height = parseInt(page_properties.getValue('u_label_height'));
        var label_width = parseInt(page_properties.getValue('u_label_width'));
        var label_margin = parseInt(page_properties.getValue('u_label_margin'));
        <!-- change request -->
        var sys_id = RP.getWindowProperties().get('change_id');
        var change = new GlideRecord('change_request');
        change.get(sys_id);
        var change_number = change.number;
        var req_by = change.getDisplayValue('requested_by');
        var short_desc = change.getValue('short_description');

        <!-- change tasks -->
        var change_task = new GlideRecord('change_task');
        change_task.addQuery('change_request',sys_id);
        change_task.query();
        change_task;
    </g:evaluate>
    <button class="btn btn-primary" onclick="download_list()">Download</button>
    <button class="btn btn-default" onclick="closeWindow()" style="margin-right:10px;">Cancel</button>
    <div id="html_content">
        <div class="body">
            <style>
                .body{
                    border: 0.5px solid;
                    margin: auto;
                    width: 100%;
                    height: 100%;
                    padding: 10px;
                }
                .internal-table{
                    /* border: 0.5px solid; */
                    margin: auto;
                    width: 80%;
                    padding: 10px;
                    /* border-collapse: collapse; */
                }
                .internal_tr{
                    /* border: 0.5px solid; */
                    padding: 10px;
                }
                .internal-td{
                    /* border: 0.5px solid; */
                    /* padding: 10px; */
                }
                .custom_padding{
                    padding: ${label_margin}px;
                }
                .custom_height{
                    height: ${label_height}px;
                }
                .custom_width{
                    width: ${label_width}px;
                }
                h1{
                    text-align: center;
                }
                .hor_seperator{
                    border: 1px solid blue;
                }
                h5{
                    font-weight: bold;
                }
            </style>
        <h1>PACKING LIST</h1>
        <hr class="hor_seperator"/>
            <div> <h5> Change tasks: </h5>
                <table class="internal-table">
                    <j:while test="${change_task.next()}">
                        <g:evaluate var="jvar_record" object="true" jelly="true">
                            var ct = new GlideRecord("change_task");
                            ct.get(change_task.sys_id);
                            ct;
                            var srno = srno + 1;
                            var srno_str = srno.toString();
                        </g:evaluate>
                        <script>
                            var barcode = '${jvar_record.getDisplayValue('number')}';
                            var barbi =
                            JsBarcode("#barcode", barcode,
                            { format: "CODE39", width: 1, height: 30, margin: 1, textMargin: 5, textAlign: "left", fontSize: 8});
                        </script>
                        <j:if test="${srno % label_per_row == 0}">
                            <td class="internal-td custom_height custom_width custom_padding">
                                ${change_task.number}
                                <svg id="barcode" width="200" height="50"></svg>
                            </td>
                            <tr></tr>
                        </j:if>
                        <j:if test="${srno % label_per_row != 0}">
                            <td class="internal-td custom_height custom_width custom_padding" >
                                ${change_task.number}
                                <svg id="barcode" width="200" height="50"></svg>
                            </td>
                        </j:if>
                    </j:while>
                </table>
            </div>
            <hr class="hor_seperator"/>
        </div>
    </div>
</j:jelly>



for Client side code I am calling a <script> tag itself so there is nothing in client script. 

Hello @ninadpatwardhan 

I'm not getting you all could you please explain me more about this