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

Tried with this code as well, still getting duplicate barcodes.

ninadpatwardhan_0-1723633112870.png

 

 

is there any asset that have empty asset tag or not ?

and also put some check condition out there to does not take the duplicate record beccuse right now in my PDI its working fine 

Do we not need to increment the count variable at the end of the forEach loop?

yes becz of the multiple barcode gen. there will element  like _renderProperties (see when you  debug the barcode and see in console log)  that store as list of the barcode that help to genrate the barcode for the selected items 

Can you please share a screenshot of how unique Barcodes are getting generated for each asset_tag in your PDI?

I tried to put a console.log but I am getting some error in the logs as below:

ninadpatwardhan_0-1723638721642.png



Thanks