One of my favourite things about Microsoft Dynamics 365 is that it includes a fantastic xRM platform that enables users to leverage what it can already do but also extend the out of the box system and build alongside and on top of it. With every release since 2013 there has been a huge expansion to the out of the box functionality. This allows users to get even more value out of what is already a fantastic product. This is clearly evidenced by functionality such as Microsoft Word and Excel Templates, the Editable Grid Control, Entitlements, SLAs and Real Time workflows. All of this functionality was not available in the CRM 2011 version of the product – a version many companies are still running.

With the popularity of more standard functionality surrounding mobility and with the excitement surrounding Field Service scenarios within Dynamics I wanted to build a simple solution which could be used to generate and read barcodes by using as much out of the box functionality as possible, and if possible, no code. I didn’t manage to not include any code at all in the solution I present in this post because the no code alternative is actually more work than not, but nevertheless, I do provide a no code alternative for the specific area in which I do.

The solution generates a bar code using a simple JavaScript function in a field within a CRM form and then the word template reads the code generated to create a bar code using a bar code font. You can then use the barcode reader control on a single line text field to read the barcode on the tablet or mobile application. I have purposefully kept this modular so you can insert this functionality in many scenarios as discussed above. Examples of where this functionality can be used:

  • For parcel pick up – think simple delivery where you wish to return something and the parcel is collected. The employee scans your parcel and normally gets you to sign.
  • For logging a particular item which can link to your product catalog or inventory within Dynamics 365 for Operations
  • For drop of a parcel and confirmation of delivery
  • Many many more!!

I have built the solution with a few extensions around it for a parcel pick up scenario. This includes:

  • Form customisations
  • New Entity (Shipment Log)
  • Simple Javascript function
  • Word Template with Barcode font
  • Workflow to generate the Word Template automatically
  • Form with barcode control on single line of text field and also the signature field
  • Simple Plugin which matches the two records with the barcode scanned and the Case with the barcode number on it to match the originating case and the shipment log (This was deployed using the new spkl framework – check it out here!) This is so a you can get a full circle use case of the application, right down to be able to report on it when its completed.

The guide below will be a walk-through of how to create the solution above. The actual items needed for just the barcode generation and reader are the word template and the mobile bar code control – thats it.

Some gotchas before we start..

  • You MUST include asterisks (*) before and after your barcode number, so in your font, when you read it using a scanner, the scanner knows when to start and stop.
  • You MUST NOT include any spaces in your barcode. The World Template can be quite finicky with this and took a while for me to get this so the control from the XML mapping didn’t put a space in. It can be done, just with patience.

I recommend the normalities when building custom solutions – create a new solution and your own publisher and prefix. The one used in this solution is cat_

Getting started – laying the groundwork

Create a new entity ‘Shipment Log’ which is enabled for mobile.

Customise your form for the Shipment Log with new fields – Barcode number (single line of text), Confirmation/Signing (Multiple lines of text), Name and Status (Optionset)

Add your controls on the Barcode Number field (barcode scanner – single line of text) and Pen Control for Confirmation/Signing (Multiple Lines of Text) – note: the type of field is important, if these are not correct your control will not be available.

Double click your field (or click properties) on the form designer with your field highlighted and click on the Controls tab. Select ‘Add Control’

 

Select Barcode Scanner and click Add

 

Remember to click Phone and Tablet radio buttons so they are highlighted as shown

 

Do the same for your Pen Control by clicking on your multiple line of text field

 

You will notice both of these controls are not available for the web

Create a new field on Case which is simple line of text called ‘Barcode Field’ on the Case entity.

Create a business rule which completes the name with the same value as the barcode. This is so a user doesn’t have to manually enter a name.

Create your Javascript library and function

I’ll be honest with you – this is not complicated. I just used a generic solution from the interwebs to create this and modified it to include the asterisks (*) at the start and end of the number and to populate my new field on the Case entity form.  Code is below. You can do the same, or make a nicer one, theres lots of ways.

function createBarCode() {

var text = "";
var possible = "ABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789";

for (var i = 0; i < 8; i++)
text += possible.charAt(Math.floor(Math.random() * possible.length));

text = "*" + text + "*";
Xrm.Page.getAttribute("cat_barcodefield").setValue(text);
}

Register it on the change of the ID field for the Case entity, and so it populates when its been saved. (You can modify where it’s operating if you want)

Add your JavaScript library to the Case entity and your JavaScript function to your event handler

Alternatives to JavaScript

You can use a similar solution to the auto-generation of the ID’s number solution I described in this post here to create your bar-code number, ensuring you still add your asterisks(*).

Create your Word Template with your barcode font

If you don’t have a barcode font already in word, you can download one. I used a code39 type barcode font in this example, but i recommend reading up and ensuring you download them from a trustworthy source.

I explain how to make a word template in this post here specifically with more detail. When you first open your template and go to open your XML Schema mapping and have your list of fields in your template, right click the Barcode Field (the one which is populated by the JavaScript function above) and add a Rich Text control. To get rid of the space, for some reason it adds, when you do this, I had to highlight the control and you will see the extra space – ensure you delete that as it breaks the bar code reader and won’t scan the correct code.

When you add the custom control, you will notice an extra space it adds, ensure this is removed whilst still keeping the content control

The content control has been added and the space is removed

I added some static text and images to my template and you can do the same or include other field mappings depending on your scenario.

Upload your word template back in the settings area (so its not a personal template) and can be used by workflows.

Real Time Workflow to auto-generate your Word Template

A user can maually go to the ‘…’ button on the entity, click Word Template and choose which one to run and manually get the Word Template. They could even use the Ribbon Workbench to change the Ribbon to show this button on the main set of buttons at the top of the form. I chose to generate the whole thing automatically for the user. (There are lots of options!)

Create a Real Time Workflow to auto-generate your template on the change of the barcode font field.

Use the Action ‘SetWord Template’ and the Entity is None(global). Set Properties to be the name of your template you called your file when you loaded it, and ensure to put the entity your running it against (which is the entity is operates on – in this case – Case)

Add an Icon

Add an icon for your new entity, especially if your using this for an actual client/organisations piece of functionality

How to Test

You can test this where you: Create a case, Generate a barcode number from your JavaScript function, then your Word Template as the video below demonstrates. I then print the word template on a printer.

Open your Mobile or Tablet app and create a new Shipment Log record. In the blank form you will see the barcode field . Click the field to display barcode reader control in the right hand side of the field then click the icon that appears to minimise the application and show the barcode scanner.

 

 

Click the field to see the barcode scanner function. Click the icon to begin the reader

With the Word Template you just printed, scan the barcode you have on this document to then autopopulate the ‘Barcode Number’ field in the form. Save the record

Finishing Touches

To get the most benefit from this I thought it was important to look at ways to match up the shipment log with the case record. I do this using a simple plugin registered on create of a new shipment log record using Retrieve Multiple. It adds the (*) to the barcode field in the code and queries CRM to retrieve the Cases with the same barcode number. It should only retrieve one and then it matches this in a lookup field for ‘Shipment Log’ on the Case. I added also then Quick View form of that Shipment Log info so the detail is available immediately from the Case form.

The plugin code is also below – It is just an example and recommend beefing it up so its more reliable for production use rather than proof of concept.

if (context.InputParameters.Contains("Target"))
  {
  Entity target = (Entity)context.InputParameters["Target"];

 IOrganizationServiceFactory serviceFactory = (IOrganizationServiceFactory)serviceProvider.GetService(typeof(IOrganizationServiceFactory));
  IOrganizationService service = serviceFactory.CreateOrganizationService(context.UserId);

 string barcode = target["cat_barcodenumber"].ToString();
  barcode = "*" + barcode + "*";

 string fetchCases = @"<fetch version='1.0' output-format='xml-platform' mapping='logical' distinct='false'>
  <entity name='incident'>
  <attribute name='title' />
  <attribute name='ticketnumber' />
  <attribute name='createdon' />
  <attribute name='incidentid' />
  <attribute name='caseorigincode' />
  <order attribute='title' descending='false' />
  <filter type='and'>
  <condition attribute='statecode' operator='eq' value='0' />
  <condition attribute='cat_barcodefield' operator='eq' value='" + barcode.ToString() + @"' />
  </filter>
  </entity>
  </fetch>";

 EntityCollection casesFound = service.RetrieveMultiple(new FetchExpression(fetchCases));

 Entity caseFound = casesFound.Entities[0];
  caseFound["cat_associatedshipmentlog"] = target.ToEntityReference();
  service.Update(caseFound); 

}

AOB

You can definitely improve on this solution – a perfect solution is not what it sets out to be. This is meant to be a starting point for something that can either be used in practice or built upon for a multitude of different scenarios. I would like to think the hard work is done in the logic of generation and reading the barcode that is infact achieved using standard, out of the box functionality. It is then up to your requirement to address what you need to add around that, how a business would use the solution in a process and further report on it.

I hope it helps!