Delete an Account and All Related Objects

CRM 1.0

This sample shows how to delete an account and all of its related objects.

Class Reference

Schema Reference

  • account.xsd
  • contact.xsd

Example

[C#]

public void DeleteAccountAndRelatedObjects()
{
   // strServer should be set with the name of the platform Web server
   String strServer = "MyServerName";

   // strVirtualDirectory should be set with the name of the Microsoft CRM
   // virtual directory on the platform Web server
   String strVirtualDirectory = "mscrmservices";
   String strDir = String.Concat("http://", strServer, "/",
                                 strVirtualDirectory, "/");

   // BizUser proxy object
   Microsoft.Crm.Platform.Proxy.BizUser bizUser 
               = new Microsoft.Crm.Platform.Proxy.BizUser ();
   bizUser.Credentials = System.Net.CredentialCache.DefaultCredentials;
   bizUser.Url = String.Concat(strDir, "BizUser.srf");

   // Account proxy object
   Microsoft.Crm.Platform.Proxy.CRMAccount account 
               = new Microsoft.Crm.Platform.Proxy.CRMAccount ();
   account.Credentials = System.Net.CredentialCache.DefaultCredentials;
   account.Url = String.Concat(strDir, "CRMAccount.srf");

   // Contact proxy object
   Microsoft.Crm.Platform.Proxy.CRMContact contact 
               = new Microsoft.Crm.Platform.Proxy.CRMContact ();
   contact.Credentials = System.Net.CredentialCache.DefaultCredentials;
   contact.Url = String.Concat(strDir, "CRMContact.srf");

   String strErrorMsg;
   try
   {
      Microsoft.Crm.Platform.Proxy.CUserAuth userAuth = bizUser.WhoAmI();
      
      // Create a dummy account and contact that you can then delete
      StringBuilder accountXml = new StringBuilder("<account>");
      accountXml.Append("<name>North Wind Traders</name>");
      accountXml.Append("<ownerid type=\"");
      accountXml.Append
        (Microsoft.Crm.Platform.Types.ObjectType.otSystemUser.ToString());
      accountXml.Append("\">");
      accountXml.Append(userAuth.UserId.ToString());
      accountXml.Append("</ownerid></account>");

      StringBuilder contactXml = new StringBuilder("<contact>");
      contactXml.Append("<firstname>Angela</firstname>");
      contactXml.Append("<telephone2>206-555-0102</telephone2>");
      contactXml.Append("<lastname>Barbariol</lastname>");
      contactXml.Append
        ("<emailaddress1>angela@northwindtraders.com</emailaddress1>");
      contactXml.Append("<ownerid type=\"");
      contactXml.Append
        (Microsoft.Crm.Platform.Types.ObjectType.otSystemUser.ToString());
      contactXml.Append("\">");
      contactXml.Append(userAuth.UserId.ToString());
      contactXml.Append("</ownerid></contact>");

      // Create the account and contact
      String accountId = account.Create(userAuth, accountXml.ToString());
      String contactId = contact.Create(userAuth, contactXml.ToString());

      // Create a hierarchy where the account is the parent of the contact
      account.AddSubContact(userAuth, accountId, contactId);
      
      // This call will delete both the account and its contact
      // It will also delete all other objects attached to the account
      account.Delete(userAuth, accountId);
   }
   catch (System.Web.Services.Protocols.SoapException err)
   {
      // Process the platform error here
      strErrorMsg = String.Concat("ErrorMessage: ", err.Message, " ",
                      err.Detail.OuterXml, " Source: ", err.Source);
   }
}

© 2005 Microsoft Corporation. All rights reserved.


Show: