locked
Autonumber issue in custom entity RRS feed

  • Question

  • Hello Team,

    We have implemented auto number functionality through plugin in custom entity. Logic is working perfectly when a single request triggers to server. If multiple requests are reaching server on the same time, same auto number is setting with all the requests. Can expect any resolution on the same.

    Regards,

    Kumar R

    Thursday, December 17, 2015 8:28 AM

Answers

  • Entity entEntity = (Entity)context.InputParameters["Target"];
                          
                QueryExpression queryAutoNumber = new QueryExpression()
                {
                    EntityName = Entities.AutoNumber,
                    ColumnSet = new ColumnSet(Attributes.AutoNumberName, Attributes.AutoCurrentNumber, Attributes.AutoNumberFormat, Attributes.AutoNumberIncrement, Attributes.AutoNumberPrefix, Attributes.AutoNumberSuffix, Attributes.AutoNumberAttributeName, Attributes.AutoNumberId),
                    Criteria =
                    {
                        Conditions = 
                            {
                                new ConditionExpression(Attributes.AutoNumberName, ConditionOperator.Equal, entEntity.LogicalName)
                            }
                    }
                };


                QueryExpression query1 = new QueryExpression()
                {
                    EntityName = "opportunity",
                    ColumnSet= new ColumnSet("new_opportunitynumber"),
                    Criteria = 
                    {
                        Conditions =
                        {
                            new ConditionExpression(Attributes.CreatedOn, ConditionOperator.GreaterEqual, DateTime.Now.Date.AddDays(-1))
                        }
                    },
                    Orders =
                    {
                        new OrderExpression("createdon", OrderType.Descending)
                    }
                };

                EntityCollection entColAutoNumber = service.RetrieveMultiple(queryAutoNumber);

                EntityCollection entColOpportunity = service.RetrieveMultiple(query1);

                int intLatestNumber = Convert.ToInt32(entColAutoNumber.Entities[0].Attributes[Attributes.AutoCurrentNumber].ToString())+ Convert.ToInt32(entColAutoNumber.Entities[0].Attributes[Attributes.AutoNumberIncrement]);
                string strTempFormat = string.Format((entColAutoNumber.Entities[0][Attributes.AutoNumberFormat].ToString()), intLatestNumber);
                string strAutoNumber = entColAutoNumber.Entities[0].Attributes[Attributes.AutoNumberPrefix].ToString() + "" + strTempFormat;

                entColAutoNumber.Entities[0].Attributes[Attributes.AutoCurrentNumber] = intLatestNumber;
                //Update Autonumber entity after increment the latest number
                service.Update(entColAutoNumber.Entities[0]);
                entEntity.Attributes[entColAutoNumber.Entities[0].Attributes[Attributes.AutoNumberAttributeName].ToString()] = strAutoNumber;
    • Marked as answer by Kumar_R Tuesday, January 19, 2016 4:08 AM
    • Unmarked as answer by Kumar_R Wednesday, February 17, 2016 2:30 PM
    • Marked as answer by Kumar_R Wednesday, February 17, 2016 2:30 PM
    Monday, December 21, 2015 7:39 AM

All replies

  • Hello All,

    We have implemented custom plugin to populate auto number in MS CRM 2015 for one of the custom entity. Plugin is working fine when single request is coming to server. If multiple requests are reaching to server, then the same number is updating in all the requests.

    Can any one help me on the same.

    Regards,

    Kumar R

    Thursday, December 17, 2015 7:42 AM
  • Hello,

    Please provide code you use.


    Dynamics CRM MVP
    My blog

    Saturday, December 19, 2015 2:48 PM
    Moderator
  • Entity entEntity = (Entity)context.InputParameters["Target"];
                          
                QueryExpression queryAutoNumber = new QueryExpression()
                {
                    EntityName = Entities.AutoNumber,
                    ColumnSet = new ColumnSet(Attributes.AutoNumberName, Attributes.AutoCurrentNumber, Attributes.AutoNumberFormat, Attributes.AutoNumberIncrement, Attributes.AutoNumberPrefix, Attributes.AutoNumberSuffix, Attributes.AutoNumberAttributeName, Attributes.AutoNumberId),
                    Criteria =
                    {
                        Conditions = 
                            {
                                new ConditionExpression(Attributes.AutoNumberName, ConditionOperator.Equal, entEntity.LogicalName)
                            }
                    }
                };


                QueryExpression query1 = new QueryExpression()
                {
                    EntityName = "opportunity",
                    ColumnSet= new ColumnSet("new_opportunitynumber"),
                    Criteria = 
                    {
                        Conditions =
                        {
                            new ConditionExpression(Attributes.CreatedOn, ConditionOperator.GreaterEqual, DateTime.Now.Date.AddDays(-1))
                        }
                    },
                    Orders =
                    {
                        new OrderExpression("createdon", OrderType.Descending)
                    }
                };

                EntityCollection entColAutoNumber = service.RetrieveMultiple(queryAutoNumber);

                EntityCollection entColOpportunity = service.RetrieveMultiple(query1);

                int intLatestNumber = Convert.ToInt32(entColAutoNumber.Entities[0].Attributes[Attributes.AutoCurrentNumber].ToString())+ Convert.ToInt32(entColAutoNumber.Entities[0].Attributes[Attributes.AutoNumberIncrement]);
                string strTempFormat = string.Format((entColAutoNumber.Entities[0][Attributes.AutoNumberFormat].ToString()), intLatestNumber);
                string strAutoNumber = entColAutoNumber.Entities[0].Attributes[Attributes.AutoNumberPrefix].ToString() + "" + strTempFormat;

                entColAutoNumber.Entities[0].Attributes[Attributes.AutoCurrentNumber] = intLatestNumber;
                //Update Autonumber entity after increment the latest number
                service.Update(entColAutoNumber.Entities[0]);
                entEntity.Attributes[entColAutoNumber.Entities[0].Attributes[Attributes.AutoNumberAttributeName].ToString()] = strAutoNumber;
    • Marked as answer by Kumar_R Tuesday, January 19, 2016 4:08 AM
    • Unmarked as answer by Kumar_R Wednesday, February 17, 2016 2:30 PM
    • Marked as answer by Kumar_R Wednesday, February 17, 2016 2:30 PM
    Monday, December 21, 2015 7:39 AM