San Phan
San Phan

Reputation: 43

How to delete account in dynamics crm using c#?

public void DeleteAccount()
{
    IOrganizationService service;
    Entity account = new Entity("account");
    Guid accountId = account.Id;
    **//accountId empty :(**

    service.Delete("account", accountId);
}

How to delete account in dynamics crm using c#? (I loaded list account using gridview, I don't get accountid)

Upvotes: 0

Views: 283

Answers (2)

The below code will search Account entity with name as test account, retrieve & then delete it. I assume you have initialized IOrganizationService with connection string to your CRM.

IOrganizationService service; //initialize this

QueryByAttribute query = new QueryByAttribute();
query.ColumnSet = new ColumnSet("name");
query.Attributes.AddRange("name");
query.Values.AddRange("test account"); 

Entity accountEntity = service.RetrieveMultiple(query).Entities.FirstOrDefault(); 

if (accountEntity != null) 
{
    Guid accountID = accountEntity.Id;
    service.Delete("account", accountID);
}

Read more

Upvotes: 0

Munavvar Husein Shaikh
Munavvar Husein Shaikh

Reputation: 601

You need to get the account id by using the Retrieve multiple require or you need to hardcode the GUID to delete the record.

Above your code will always return empty GUID because you are creating a new object here.

Upvotes: 2

Related Questions