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)
{
//
}
}
}
}