Custom Auto Number for Cases - CloudFronts

Custom Auto Number for Cases

Posted On December 1, 2015 by Admin Posted in 

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)
                { 
                    return; 
                }

                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"];
                        this.CaseNumber_Generation(casenumber);   
                      }
                } 
}
// 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.Update(result);
                        this.recordguid = caserecord.Id;
                    }
                }
            }
}
// Method for updating any entity.
private void Update(Entity caseRecord)
        {
                this.service.Update(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.

 
Example
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.

    2

  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

Conclusion

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.


Share Story :

Secured By miniOrange