Microsoft CRM 2013/2015 Auto Number Generator

Microsoft CRM 2013/2015 Auto Number Generator

This solution is designed to offer auto-incrementing values for any field in your Microsoft Dynamics CRM System.  It can even handle multiple unique Auto-Number fields within the same entity.  PlanetXRM AutoNumber generates unique sequential values and can be used without creating any custom code.  This value can be generated before a record is created or when a record is inserted, updated, deleted and/or when fields change.  Once installed and configured for a particular attribute for a particular entity, the AutoNumber Action can be called via a workflow (no code), a JavaScript function or plugin.  This allows the generated auto number value to conform to your specific business processes or custom requirements.

Installation and Configuration

Email us at info@go-planet.com for a quote or to purchase the solution.  Once you receive the AutoNumber solution, import it into your CRM system.  Then, create an AutoNumber record with values that define how the number will be generated.  Finally, create a workflow, a JavaScript function or Plugin that calls the AutoNumber Action in your system.  The following sections will explain, in detail, how to configure and implement the Auto Number solution in your system.

  1. Navigate to the CRM Menu > Settings > Auto Number.
    Auto Number Record Example
  2. Choose the name of the entity.
  3. Enter the attribute name to which this auto-number value will be saved.
  4. Ensure that the combination of Entity Logical Name and Attribute Name will be unique among the active records in the AutoNumber entity.
  5. Enter the fixed number size. This is the number of digits that the auto number value should be when generated.
  6. Enter the starting Counter value. For example, if you wish to have your first record have a 1, then enter 0. Or if you wish to have your first record be 1000, enter 999.
  7. Enter the increment by value. For example, if you enter 1, and the counter is at 4, the next number will be 5. Otherwise, if for example, you enter 5, and the number is 10, the next number will be 15.
  8. Choose Yes or No for Zero pad. If Yes is chosen, and the Counter value is 1 and the Fixed Number Size is 6, the next number generated will be 000002. If No is chosen, the next number will be 6.
  9. Optionally, enter a Prefix value. In the screenshot above, the next number generated would be MY-001013.
  10. Optionally, enter a Postfix value. In the screenshot above, that auto-number would not have a Postfix value. But, if, for example, a Postfix value was entered as –END, the next auto number value generated would be MY-001013-END.
  11. Optionally, enter a description for this auto number definition.
  12. Save the record.
  13. Decide how you will call the AutoNumber Action in your system: Workflow (No Code Solution), JavaScript (Code Solution) or via Plugin (Code Solution).
  14. Jump to the section in this document that describes how to implement your chosen solution.
    • a. After record create (PlanetXRM Tools Workflow Activity – No Code Solution)
    • b. After record update (PlanetXRM Tools Workflow Activity – No Code Solution)
    • c. After record delete (PlanetXRM Tools Workflow Activity – No Code Solution)
    • d. After record field(s) change (PlanetXRM Tools Workflow Activity – No Code Solution)
    • e. Before the record is saved through the form (JavaScript – Code Solution)
    • f. On field value change on the form (JavaScript – Code Solution)
    • g. On CRM Ribbon button click (JavaScript – Code Solution)
    • h. Inside of a custom plugin (Plugin – Code Solution)

No Code Solutions: Use the PlanetXRM Tools AutoNumber Workflow Activity

The AutoNumber solution comes with a Workflow Activity called PlanetXRM Tools > AutoNumber. You can add a step in your workflow to call the Planet XRM AutoNumber Activity and it will return the next Auto-Number value for your Entity and Attribute combination. This Activity can be called within any workflow on create, update, delete or when certain fields change on a record. An simple example is depicted below.

  • 1. Create a new workflow or modify an existing workflow.
  • 2. Add a new step, in the appropriate place to suit your logic.
    • a. In the example below, this sample workflow is simply setting the next auto number value when the record is created to the account number field on the Account entity.
  • 3. Choose the PlanetXRM Tools > AutoNumber option as shown in the screenshot below.
  • Auto Number Example Workflow Step 1
  • 4. Click the Set Properties button.
  • Auto Number Workflow Step 2
  • 5. Enter the values for the auto-number generation. At a minimum, you must specify the Entity and Attribute Name field values. Optionally, you can override the prefix and postfix values for this specific auto-number. If the prefix override and postfix override fields are left blank, it will generate the auto-number according to any prefix or postfix values as configured in the AutoNumber record for the given entity and attribute combination.
  • Auto Number Workflow Step 3
  • 6. Click Save and Close. Then, add an Update Step like shown below (or any other intermediary steps, as needed for your requirements).
  • Auto Number Workflow Step 4
  • 7. Set the value of the accountnumber field to the returned autonumber value from the previous workflow step. Click the Set Properties button.
  • Auto Number Workflow Step 5
  • 8. Put your cursor inside of the Account Number field. Then, on the right hand side, choose the “generate the next autonumber” value from the “Look for”
    dropdown under the “Local Values” section. Then, click the Add button. Finally, click the OK button to add the value to the account number field
    on the left (see screenshots below). Then click “Save and Close”.
  • AutoNumber Workflow step 6
  • AutoNumber Workflow step 7
  • AutoNumber Workflow step 8
  • AutoNumber Workflow step 9
  • 9. Then, add the workflow stop (succeeded) step below the update step. Finally save the workflow and activate it. Now, when users create new accounts,
    a new auto-number will be generated and saved to the account number field automatically.

Code Solutions: JavaScript Example

The PlanetXRM AutoNumber Action can be called via JavaScript. Included in the PlanetXRM AutoNumber solution is a sample web resource called pt_AutoNumberSample.js.
Inside this JavaScript file is a method called CallAutoNumberAction that could be used as a template for your own method calls. This method could be configured to be
executed during the on field change or during a custom action on your form(s).

AutoNumber Sample Entity

To see the form in action, navigate to the AutoNumberSample entity form. Go to Advanced Find > choose the Auto Number Sample entity from the look for dropdown.
Click the “+ New” button and you’ll be taken to the form below.

AutoNumberExampleForm1

Next, enter a Contract Start Date and the OnChange form event will fire and call the CallAutoNumberAction JavaScript method. The success method, as defined in the asynchronous call, will set the value of the auto-number returned to the Contract Number field in the form, as shown below.

AutoNumberExampleForm2

The Auto Number Sample entity has the OnChange event registered to execute the CallAutoNumberAction JavaScript method for the Contract Start Date field on the form.

AutoNumberExampleJS

Modifying the Template Code

  • 1. Navigate to Settings > Customizations > Customize the system. This will open a window with all the components of your CRM system. Inside of this window, navigate to the Web Resources and open the pt_AutoNumberSample.js file.
  • 2. Copy the contents into your favorite text editor.
  • 3. Modify the script where there are comments numbered 1 – 6.
  • 4. Save the file as a new web resource and register the web resource on the form of your choice.

Code Snippet

    // -----------------------------------------------
    // Modify this method to suit your requirements
    // -----------------------------------------------
    function CallAutoNumberAction()
    {
        try
        {
        // Get the form type and current contract number value
        var formType = Xrm.Page.ui.getFormType();
		
        // 1a. Auto-number generated on Create = 1, Update = 2 and/or Bulk Edit = 6
        if (formType != 1) {
            return;
        }
		
        // 2. Change to your entity name and attribute name configured in the AutoNumber record
        var entity = 'pt_autonumbersample';
        var attribute = 'pt_contractnumber';
	
        // 3. Put your (optional) custom logic here to generate a prefix and/or postfix
        var contractDate = Xrm.Page.getAttribute("pt_contractstartdate").getValue();
        var fiscalYear = getFiscalYear(new Date(contractDate));
        var prefix = "FY" + fiscalYear + "-";
		
        // 4. Call the GenerateXMLRequest method, supplying your entity name, attribute name,
        //     and prefix (optional argument), postfix (optional argument)
        var XMLRequest = GenerateXMLRequest(entity, attribute, prefix, '');
		
        // Call AutoNumberAction, defining success and failure callback methods
        CustomActionExecutor.Execute(
        {
            requestXml: XMLRequest,
            async: true,
            successCallback: function(result){
                try {
                    var autoNum = result.autonumber_value;
					
                    // 5. Handle the auto-number value you get back below
                    // Change code below this line to fit your business logic
                    Xrm.Page.getAttribute("pt_contractnumber").setValue(autoNum);
                }
                catch (ex) 
                {
                    // 6a. Customize this function to handle errors however you wish
                    alert("error getting returned auto number = " + e.message);
                }
            },
            errorCallback: function (e) {
                // 6b. Customize this function to handle errors however you wish
                alert("error = " + e.message);
            }
        });
        }
        catch (e)
        {
            // 6c. Customize this function to handle errors however you wish
            alert(" Error: " + e.message);
        }
    }

Code Solutions: Plugins

For more complex requirements, the AutoNumber Action could be called via a plugin in the Pre-Validation, Pre-Operation, Main Operation or Post-Operation stages.
Tip: when using the CrmSvcUtil.exe to generate the early binding classes, don’t forget to add /generateActions to your command. An example is below:

    CrmSvcUtil.exe /url:https://yourcrmsystem.com/XRMServices/2011/Organization.svc /out:XRM_Entities.cs  
    /username:user1 /password:pass /namespace:XRM_Entities /serviceContextName:xrmContext /generateActions

Early Binding Example Code

    
    pt_AutoNumberActionRequest request = new pt_AutoNumberActionRequest();
    // required values
    request.entity_logicalname = "pt_autonumbersample";
    request.attribute_name = "pt_contractnumber";
    // optional values
    request.postfix_override = "FY2015"; 
    request.prefix_override = "-XYZ";

    pt_AutoNumberActionResponse response = (pt_AutoNumberActionResponse) service.Execute(request);
    string autoNumber = response.autonumber_value;

Late Binding Example Code

    OrganizationRequest request = new OrganizationRequest("pt_AutoNumberAction");
    // required values
    request["entity_logicalname"] = "pt_autonumbersample";
    request["attribute_name"] = "pt_contractnumber";
    // optional values
    request["postfix_override"] = "FY2015"; 
    request["prefix_override"] = "-XYZ";

    OrganizationResponse response = service.Execute(request);
    if (response["autonumber_value"] != null)
    {
	    string autoNumberValue = response["autonumber_value"].ToString();
    }

Removing an Auto-Number Action from your System

First, deactivate any workflows, plugins or JavaScript functions that call your AutoNumber Action for a particular entity and attribute. Second, deactivate or delete the record that defines the AutoNumber action for the respective entity and attribute.

Comments are closed.