how to create Account in dynamics crm using asp.net?

325 Views Asked by At
public List<AccountEntityModels> RetriveRecord()
{           
  using (OrganizationService service = new OrganizationService("MyConnectionString"))               
  {               
    QueryExpression query = new QueryExpression
    {                    
      EntityName = "account",
      ColumnSet = new ColumnSet("name")
    };
    List<AccountEntityModels> info = new List<AccountEntityModels>();
    EntityCollection accountRecord = service.RetrieveMultiple(query);

    if (accountRecord != null && accountRecord.Entities.Count > 0)                    
    {                   
      AccountEntityModels accountModel;

      for (int i = 0; i < accountRecord.Entities.Count; i++)
      {                  
        accountModel = new AccountEntityModels();
        if (accountRecord[i].Contains("name") && accountRecord[i]["name"] != null)
          accountModel.AccountName = accountRecord[i]["name"].ToString();

        info.Add(accountModel);
      }
    }
    return info;
  }
}
1

There are 1 best solutions below

3
Arun Vinoth-Precog Tech - MVP On BEST ANSWER

The above code is for retrieving all the CRM accounts, storing in generic List collection and returning it.

If you want to create an account, use this code.

Entity account = new Entity("account");
account["name"] = "test account";
Guid _accountId = _service.Create(account);

Read more for samples explained clearly, also about _service initialization.