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

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

Hey @ninadpatwardhan 
 Is there any thing that asset tag is empty or not and if the asset tag is empty then put some check condition in html code ? elsewise  could you please shere your  ui page  that you have created and client side and html code both 

Hey @Sanjay191 ,

I have used the same code which you have provided

You put all the code in html template ?

 

Created a UI action, printBarCodeLabel() put this function in the UI action. and rest of the code in UI page HTML.

<?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 = RP.getParameterValue("sysparm_id");
        sysparm_ids;
    </g:evaluate>

    <style>
        body, html {
            margin: 0;
            padding: 0;
            width: 100%;
            height: 100%;
            font-size: 10px;
        }

        td {
            font-size: 10px;
            text-align: center;
            vertical-align: bottom;
        }

        .fullscreen {
            position: fixed;
            top: 0;
            left: 0;
            bottom: 0;
            right: 0;
            overflow: auto;
            background: white; /* Just to visualize the extent */
        }

        @media print {
            @page {
                margin: 0;
                width: 95%;
                height: 95vh;
            }
            body {
                margin: 0;
                padding: 0;
                width: 95%;
                height: 95vh;
            }
        }

        table {
            width: 100%;
            height: 100%;
            border-collapse: collapse;
        }

        .barcode-label {
            display: flex;
            flex-direction: column;
            align-items: center;
            margin-bottom: 10px;
        }

        .barcode-label svg {
            margin-bottom: 5px;
        }

        .barcode-text {
            text-align: center;
            vertical-align: bottom;
        }

        .reload-button {
            margin-top: 20px;
        }

        .reload-button button {
            padding: 10px 20px;
            font-size: 16px;
            cursor: pointer;
        }
    </style>

    <div class="fullscreen">
        <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>

            <div class="barcode-container">
                <div class="barcode-label">
                    <svg id="barcode${count}" width="700" height="200"></svg>
                    <div class="barcode-text">
                        <br/>
                        <h6>
                            Model: ${jvar_record.getDisplayValue('model')} | Serial No: ${jvar_record.getDisplayValue('serial_number')}
                        </h6>
                    </div>
                </div>
            </div>

            <script>
                var barcode = '${jvar_record.getDisplayValue('asset_tag')}';
                var labelText = "Asset tag: " +  '${jvar_record.getDisplayValue('asset_tag')}';
                var barbi = JsBarcode("#barcode", barcode, { format: "CODE39", width: 1, height: 29, margin: 2, textMargin: 4, textAlign: "center", fontSize: 15, text: labelText });
                barbi._renderProperties[0].element.id = barbi._renderProperties[0].element.id + count;
            </script>
        </j:forEach>

        <!-- <div class="reload-button">
            <button onclick="window.location.reload();">Back</button>
        </div> -->
       
    </div>

</j:jelly>




please use (in html templete only) the above code  and let me know if anything else 

and Please mark as Helpful