-1

I want to create autonumber plugin on account entity. cases are if account name = Morgan Stanely then account number = MORG00001 and if account name = Morgan Motor then account number = MORG00002 and if account name is ABC Tech then account number should be ABCT00001.

AccountNumber takes first 4 characters of account name and append 4 zeros and increment it by 1 if accont name with same 4 characters are already there.

Method name: newaccounname()

Code which I have written:

    public void updaterecord(EntityCollection account)
    {
        int totalrecords = account.Entities.Count;
        string[] name = new string[totalrecords];
        string append = "0000";
        string value = "1";
        int number = int.Parse(value);

        string[] accountnumber = new string[totalrecords];
        for (int i = 0; i < totalrecords; i++)
        {
            ////check the accountname and take first 4 characters from it.

            name[i] = account.Entities[i].Attributes["name"].ToString();
            string partialstring = name[i].Substring(0, 4);
            if (entity.Attributes.Contains("accountnumber") == false)
            {

                string anumber = string.Concat(partialstring, append, value);
                entity.Attributes.Add("accountnumber", anumber.ToString());
            }
            else if (entity.Attributes.Contains("accountnumber") == true)
            {
                newaccountname(account);
            }

        }
    }
    public void newaccountname(EntityCollection account)
    {
        int totalrecords = account.Entities.Count;
        string[] name = new string[totalrecords];
        string[] accountnumber = new string[totalrecords];
        for (int i = 0; i < totalrecords; i++)
        {
            string accountNumber = entity.GetAttributeValue<string>("accountnumber");
            accountnumber[i] = account.Entities[i].Attributes["accountnumber"].ToString();
            name[i] = account.Entities[i].Attributes["name"].ToString();
            string paccountnumber = accountnumber[i].Substring(0, 4);
            if(name[i] == paccountnumber)
            {
                //
            }
        }


    }
}
vidhi
  • 69
  • 2
  • 9
  • What is your question? – Polyfun Dec 20 '17 at 10:18
  • how can I take records which are previously created and increment the number by 1. for example if account name = Morgan Stanely andaccount number = MORG00001 and if account name = Morgan Motor then account number = MORG00002 – vidhi Dec 20 '17 at 10:20
  • 2
    It would be much clearer if you tell us exactly where in the code you posted you are having a problem. – Polyfun Dec 20 '17 at 10:23
  • before taking a substring it is adviseable to check whether there are enough letters to take. Otherwise a `ArgumentOutOFRangeException` will be the resulting outcome... – Mong Zhu Dec 20 '17 at 10:29
  • Please check out this article too: [How to Implement Robust Auto-Numbering Using Transactions in Microsoft Dynamics CRM](https://www.akaes.com/blog/how-to-implement-robust-auto-numbering-using-transactions-in-microsoft-dynamics-crm/) – Aron Dec 20 '17 at 12:33

1 Answers1

1

well, I am not going to solve the quiz for you:), but here are some tips

  • you need to remove all spaces form the name first

  • you need to compare the 4 character in the name you have (after removing the space) with only every 4characters of the names in the list so when you compare "MORG00001" with "Morgan Motor" you are comparing "MORG" with "MORG" which give you true in this case

  • Count how many "MORG" you have and the new account will be [First4Letters][0000][Count+1]

Hussein Khalil
  • 1,395
  • 11
  • 29