Thursday, March 20, 2014

Barcode on a SSRS Report

Recently I was tasked with adding a barcode to the SalesPackingSlip report.  I had never done a barcode before so I wasn't really sure how this would turn out.

To help me out, I used WMSPickingList_OrderPick as an example.

The first thing we want to do here is create some parameters for the CustFormLetterDocument and the corresponding form. You can get to this form by going Accounts receivable -> Setup -> Forms -> Form setup.  We want to add 2 parameters to the Packing slip section.

As you can see I have added two parameters.  One is the 'Bar code setup', which contains the Font, Font size, and other information that will help the report display the bar code the way you want.  The other is just in case the customer would like to not display the barcode on the report.

The next thing we want to do is add some fields to our SalesPackingSlipHeaderTmp.  I picked the Header tmp table because my barcode will be the PackingSlipId which will, of course, be in the header.
Add the following fields:

  BarCode of type BarCodeString
  BarcodeFontName of type FontName
  BarcodeFontSize of type FontSize

I think the BarCodeString field is obvious, but why the other two?  Well, we want to be able to get the information from the BarCode setup.  So, if the customer wants to change the barcode font or size, the report doesn't have to be changed.

Now we need to modify some code.  The SalesPackingSlipDP class is where we will do all these modifications.  First we start by declaring some class level variables:

    BarcodeSetupId                      barcodeSetupId;
    BarcodeSetup                         barcodeSetup;
    Barcode                                 barcode;
    NoYes                                   showBarcode;

As we use these variables, I will explain what they are used for.
First, we are going to create a method to initialize these variables.

private void initBarcode()
{
    showBarcode     = CustFormletterDocument::find().showPackingSlilpIdBarCode;
    barcodeSetupId  = CustFormletterDocument::find().barcodeSetupId;
    barcodeSetup    = BarcodeSetup::find(barcodeSetupId);
    barcode         = BarcodeSetup.barcode();
}

As you can see here, we are getting the two parameters spoken of earlier.  Then we are using that barcodeSetupId parameter to get all the barcodeSetup information.  As you will see later, this will contain our FontSize and FontName.
The next method we want will actually get the text we want and convert it to a barcode.

private BarCodeString barCode()
{
    str barCodeContents = custPackingSlipJour.PackingSlipId;

    if (!barCodeContents)
    {
        return '';
    }

    if (barcodeSetup.validateBarcode(strUpr(barCodeContents)))
    {
        barcode.string(true,strUpr(barCodeContents));
        barcode.encode();
    }
    else
    {
        throw(error(strFmt("@SYS41409", barcode.barcodeType(), strUpr(barCodeContents))));
    }

    if (!barcode)
    {
        return '';
    }

    return barcode.barcodeStr();
}

Here we are checking to see if there is actual text to encode and then using the barcodeSetup to validate that the text we are trying to encode, fits the criteria in the barcodeSetup (such as Maximum/Minimum length). Then we simply return the barcode string.

The rest is pretty easy!  In our ProcessReport method of the DP class, we should call 'this.initBarcode()' and in our setSalesPackingSlipHeaderTmp we should have the following:

if (showBarcode == NoYes::Yes)
{
        salesPackingSlipHeaderTmp.barCode                    = this.barcode();
        salesPackingSlipHeaderTmp.barcodeFontName            = barcodeSetup.fontName;
        salesPackingSlipHeaderTmp.barcodeFontSize            = barcodeSetup.fontSize;
}

This will only add the barcode if the showBarcode box is checked in the parameters.

The only thing left to do is in visual studio.  
Add a new textbox and set it to use the barCode field you added to the HeaderTmp table.
The only tricky part here is using an expression for your FontName and FontSize.

For FontName, simply go in and add the field as an expression.  Very straight forward.
For FontSize, however, you need to do it a little different.  Go into the FontSize and add an expression, except you will need to encapsulate your field in the Cstr() method along with adding + "pt" + at the end.  This will make the 25pt that the report needs.  Here is an example:



That's it.  Not as difficult as I thought it was going to be, however, there were several new concepts that I had to learn.



No comments:

Post a Comment