Looking for PowerObjects? Don’t worry, you’re in the right place! We’ve been part of HCL for several years, and we’ve now taken the final step in our acquisition journey: moving our website to the HCL domain. Nothing else is changing – we are still fanatically focused on Microsoft Business Applications!

PowerObjects Blog 

for Microsoft Business Applications


CRM 2011 Auto Numbering with PowerAutoNumber and Look-up Fields

Post Author: Joe D365 |

One feature that can be difficult to achieve in Dynamics CRM without some development help is automatic unique numbering of entities. The ability to have Accounts, Phone Calls, or any custom entity that you create assigned a custom unique ID can be a huge aid in organizing and tracking records.

To help with this, PowerObjects has created a custom PowerPack solution called PowerAutoNumber that allows you to quickly and easily set up automatic numbering of any entity in Dynamics CRM 2011. All you need to do is install the solution, choose your ID format, and point it to the entity on which it should be applied. Once you have it set up, you can have an ID automatically added to any text or numeric field in any record in CRM. This ID can even include dates or information from another field on the record, and it takes no custom coding whatsoever.

You can try PowerAutoNumber for free, and learn how to use it in our short User Guide.

There is also a neat trick you can do by taking advantage of a little bit of JavaScript to create a unique ID number that dynamically includes the name of the customer (or any other name that you would like to pull) from a look-up field. For our demo, we are going to automatically populate the "Title" field of every case we create with an ID that starts with the name of the customer. Thus if the case is for Microsoft, and the current ticket number is 1019, the "Title" of our case will become Microsoft-1019

CRM 2011 auto numbering

To do this:

  1. Create a hidden text field on the form you wish to have this automatic number added to. In our demo we have created a single line text field named new_powerhiddenfield and unchecked the "Visible by Default" box in its properties, thus making it a hidden field.
  2. Set PowerAutoNumber to use this field when generating its numbers.

    Here we have set the Target Attribute (the field where PowerAutoNumber will add the ID) to "Title". We have also set Field Format to {f:new_powerhiddenfield}-{a:1}, which means that any number that PowerAutoNumber creates will start with the contents of the new_powerhiddenfield and be followed by a dash and the current number PowerAutoNumber is at.

  3. The final piece of the puzzle is to create some JavaScript to pull the name of the record that is contained in the customer look-up field into our new_powerhiddenfield so that PowerAutoNumber can reference it when it goes to add in the ID. See the the code below:[sourcecode language="javascript"]
    function autonumber() {

    var autofield = Xrm.Page.data.entity.attributes.get("customerid").getValue()[0].name;

    Xrm.Page.data.entity.attributes.get("new_powerhiddenfield").setValue(autofield);

    }
    [/sourcecode]

    The code above creates a function called autonumber. The second line grabs the name in text form of the record in the customerid field. The third line then writes that name to the new_powerhiddenfield on the form.

    To get this code to work with whichever fields your application is using, just replace customerid with the look-up field that you wish to pull your name from, and new_powerhiddenfield with whichever field you have PowerAutoNumber set to use in creating its field format for creating IDs.

    Then add the JavaScript as a web resource to the form you wish to use it on

    and have it call the autonumber function on the onsave event.

    The code will execute when you save your form, writing the name of the record from your look-up (in this case from the customerid field) to the hidden field (in this case new_powerhiddenfield). PowerAutoNumber will then fetch the information from your hidden field and use it in its field format.

    Want to see more of Dynamics CRM can do with a little JavaScript magic?

    Happy CRM'ing!

Joe CRM
By Joe D365
Joe D365 is a Microsoft Dynamics 365 superhero who runs on pure Dynamics adrenaline. As the face of PowerObjects, Joe D365’s mission is to reveal innovative ways to use Dynamics 365 and bring the application to more businesses and organizations around the world.

PowerObjects Recommends