Bulk deleting and de-activavting accounts in CRM 3.0

In CRM 3.0, we are only able to see 250 records per page.If we have to delete 5000 + records, it becomes nights mare. There is a method in SDK (bulk delete). First of all , we need to retrieve the “Accountid” of all accounts and then I will bulk delete .It is bit risky , make it sure that you are entering the web reference of Test server .

try

{

CrmService service = new CrmService();
service.Credentials = System.Net.CredentialCache.DefaultCredentials;
ColumnSet cols = new ColumnSet();
cols.Attributes = new string[] { “accountid” };
ConditionExpression condition = new ConditionExpression();
QueryExpression query1 = new QueryExpression();
query1.EntityName = EntityName.account.ToString();
RetrieveMultipleRequest retrieve = new RetrieveMultipleRequest();
retrieve.Query = query1;

RetrieveMultipleResponse retrieved = (RetrieveMultipleResponse)service.Execute(retrieve);

BusinessEntityCollection ret = retrieved.BusinessEntityCollection;

for (int i = 0; i < ret.BusinessEntities.Length; i++)

{

account aba1 = (account)ret.BusinessEntities[i];
service.Delete(EntityName.account.ToString(),ab1.accountid.Value);

// in order to de-activate all accounts , use these piece of codes

SetStateAccountRequest stat = new SetStateAccountRequest();
stat.AccountState = AccountState.Inactive;stat.AccountStatus = -1;
stat.EntityId = aba1.accountid.Value;
SetStateAccountResponse stateSet = (SetStateAccountResponse)service.Execute(stat);

}

}

catch (System.Web.Services.Protocols.SoapException ex)

{

Console.WriteLine(ex.Message + “.” + ex.Detail.InnerText);

}
}
}


Advertisements

1 Response to “Bulk deleting and de-activavting accounts in CRM 3.0”


  1. 1 Andon April 27, 2008 at 10:29 pm

    Ok… I’m new to programming in the CRM. But isn’t it quicker to simply set the DeletionStateCode of AccountBase to 2…

    Is there any disadvantage of doing this i.e. I’m I skipping some background code that need to be exectued? I performed this on a test database and it did not cause any problems…


Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s





%d bloggers like this: