1 Dec’15

Dynamics CRMCustom Auto Number for Cases

Currently in Microsoft Dynamics CRM, customer service representative creates a case to track a customer request, question, or a problem. All actions and communications can be tracked in the case entity. ID field in Case entity is automatically generated and default format is CAS-00034-Z7M9F7. This kind of Case ID will not be meaningful for many users and there might be a need to create an ID which consists of customer name so that service representative can easily identify the case just by looking at the Case ID.

In today’s blog, we will show you how to generate customized Case ID with your own format as per the business requirement.

Whenever a case is created Case ID field is automatically generated and lock is acquired on that field. User cannot change the ID generated. In order to achieve a custom generated Case ID we create a plug-in and do customizations in CRM. Here we are considering an example where Account entity is been used. The Case ID generated is like “3-003” where ‘3’ is the unique account number and ‘003’ represents the 3rd case for respective account. When 4th case is created on same account, the counter increases and the Case ID generated will be “3-004”. Thus track can be maintained on cases for different accounts just by seeing the Case ID.

In order to get Custom Auto Number on Case Entity follow the below steps:

1) Plug-in:

Create a synchronous PRE CREATE plugin on Case entity.

The code for case number generation

  public void Execute(IServiceProvider serviceProvider)
            if (serviceProvider != null)
                this.context = (IPluginExecutionContext)serviceProvider.GetService(typeof(IPluginExecutionContext));

                //// Obtain the organization service reference which you will need for web service calls.
                IOrganizationServiceFactory serviceFactory = (IOrganizationServiceFactory)serviceProvider.GetService(typeof(IOrganizationServiceFactory));
                this.service = serviceFactory.CreateOrganizationService(this.context.UserId);
                if (this.context.Depth > 2)

                if (this.context.InputParameters.Contains("Target"))
                    if (this.context.InputParameters["Target"] is Entity)
                        //// Obtain the target entity from input parameters
                        Entity casenumber = (Entity)this.context.InputParameters["Target"];
// Case Number generation
private void CaseNumber_Generation(Entity casenumber)
Entity result = null;
            string caseNumber = string.Empty;
            string accountNumber = string.Empty;
            string counterValue;
            int caseCounter;
 if (casenumber.LogicalName == "incident")
                if (casenumber.Attributes.Contains("customerid"))
                    EntityReference customerId = (EntityReference)casenumber.Attributes["customerid"];

                    //// Retrieve related account.
                    QueryExpression customerquery = new QueryExpression("account");
                    customerquery.ColumnSet = new ColumnSet("new_casecounter", "statecode", "accountnumber");
                    customerquery.Criteria.AddCondition(new ConditionExpression("statecode", ConditionOperator.Equal, 0));
                    customerquery.Criteria.AddCondition(new ConditionExpression("new_casecounter", ConditionOperator.NotNull));
                    customerquery.Criteria.AddCondition(new ConditionExpression("accountid", ConditionOperator.Equal, customerId.Id));
                    EntityCollection customerresult = this.service.RetrieveMultiple(customerquery);
                    if (customerresult.Entities.Count > 0)
                        result = customerresult[0];

                        accountNumber = result.GetAttributeValue<string>("accountnumber");
                        counterValue = result.GetAttributeValue<string>("new_casecounter");
                        bool counterResult = int.TryParse(counterValue, out caseCounter);
                        if (counterResult)
                            caseCounter = caseCounter + 1;
                        if ((caseCounter >= 1) && (caseCounter < 10))
                            caseNumber = string.Format("{0} - 00{1}", accountNumber, caseCounter.ToString(CultureInfo.InvariantCulture));
                        else if ((caseCounter >= 10) && (caseCounter < 99))
                            caseNumber = string.Format("{0} - 0{1}", accountNumber, caseCounter.ToString(CultureInfo.InvariantCulture));
                        //// Case Number update
                        Entity caserecord = new Entity("incident");
                        caserecord.Id = casenumber.Id;
                        casenumber["ticketnumber"] = caseNumber;

                        //// Case counter in account entity updated
                        Entity accountrecord = new Entity("account");
                        accountrecord.Id = result.Id;
                        result["new_casecounter"] = caseCounter.ToString(CultureInfo.InvariantCulture);

                        this.recordguid = caserecord.Id;
// Method for updating any entity.
private void Update(Entity caseRecord)


2) Customizations in CRM

  1. Add Counter field to the entity whose values you will use in Case ID.
  2. Initialize the counter field to 0
  3. Hide the counter field.

We can consider an example where case number is generated through plugin. Requirement is to create Case Number with respect to the account selected. Whenever a new case is created on that particular account, counter increases and case Id is generated.

  1. Create an account with name “Anay Industries”.
    1. Account number- Unique identifier for account created.
    2. Case Counter- By default it is set as 0. This is hidden field and whenever a new case is created the counter increases by 1.
    3. 1

  2. Case ‘Auto number generation’ is created with Customer “Anay Industries”
    Anay Industries has Account Number- 3 and Case counter-2. When this case is created Account Number is selected and counter is increased by 1.


  3. When the record is created Case ID “3-003” is generated where ‘3’ is the Account Number and ‘003’ is the case counter.
  4. 3


Although our example was simple, you can generate complex case numbers as well by applying a different logic.

We hope this have given you a useful information on generating automated Custom Case Number.

Written by

Team Member


One thought on “Custom Auto Number for Cases”

Leave a Reply

Your email address will not be published. Required fields are marked *

Time limit is exhausted. Please reload the CAPTCHA.

Want to streamline your business processes?

  • This field is for validation purposes and should be left unchanged.

Recent Articles

  • Reading more then 10K records in D3FOE OData API

    11 July’ 2018

    Introduction: We all know Dynamics 365 Finance and Operations has limitation of 10K records to be fetched at a time usi...

    Read more
  • Paging in D365 Customer Engagement v9.0

    10 July’ 2018

    Introduction: The Xrm.retrieveMultipleRecords method is used to retrieve a collection of records in Dynamics 365 Custom...

    Read more
  • Set up Dynamics 365 connection in Microsoft Social Engagement

    10 July’ 2018

    Introduction: This blog explains how to Set up Dynamics 365 connection in Microsoft Social Engagement. Steps to be follo...

    Read more
  • Voice of the Customer failed to install

    10 July’ 2018

    Introduction: Many people face issues in installing Voice of Customer solution on v9 environment and trying repeatedly ...

    Read more
  • Scribe Insight AX as a Web Service Find Block issue

    10 July’ 2018

    Introduction: If we need to look up for any value from AX then we do it by using a Find Block in Scribe Insight Eg: Basi...

    Read more