Delete an Account and All Related Objects

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 = "https://" + strServer + "/" + strVirtualDirectory + "/";

   // BizUser proxy object
   Microsoft.CRM.Proxy.BizUser oBizUser = new Microsoft.CRM.Proxy.BizUser ();
   oBizUser.Credentials = System.Net.CredentialCache.DefaultCredentials;
   oBizUser.Url = strDir + "BizUser.srf";

   // Account proxy object
   Microsoft.CRM.Proxy.CRMAccount oAccount = new Microsoft.CRM.Proxy.CRMAccount ();
   oAccount.Credentials = System.Net.CredentialCache.DefaultCredentials;
   oAccount.Url = strDir + "CRMAccount.srf";

   // Contact proxy object
   Microsoft.CRM.Proxy.CRMContact oContact = new Microsoft.CRM.Proxy.CRMContact ();
   oContact.Credentials = System.Net.CredentialCache.DefaultCredentials;
   oContact.Url = strDir + "CRMContact.srf";

   string strErrorMsg;
   try
   {
      Microsoft.CRM.Proxy.CUserAuth oUserAuth = oBizUser.WhoAmI();
      
      // Create a dummy account and contact that you can then delete
      string strAccount = "<account><name>North Wind Traders</name><ownerid type=\"" 
         + Microsoft.CRM.Flags.ObjectType.otSystemUser.ToString() +"\">"
         + oUserAuth.UserId + "</ownerid></account>";

      string strContact = "<contact><firstname>Angela</firstname>";
      strContact += "<telephone2>206-555-0102</telephone2>";
      strContact += "<lastname>Barbariol</lastname>";
      strContact += "<emailaddress1>angela@northwindtraders.com</emailaddress1>";
      strContact += "<ownerid type=\"";
      strContact += Microsoft.CRM.Flags.ObjectType.otSystemUser.ToString() +"\">";
      strContact += oUserAuth.UserId + "</ownerid></contact>";
      
      // Create the account and contact
      string strAccountId = oAccount.Create(oUserAuth, strAccount);
      string strContactId = oContact.Create(oUserAuth, strContact);

      //Create a hierarchy where the Account is the parent of the Contact
      oAccount.AddSubContact(oUserAuth, strAccountId, strContactId);
      
      // This call will delete both the Account and its contact
      // It will also delete all other objects attached to the account
      oAccount.Delete(oUserAuth, strAccountId);
   }
   catch(System.Web.Services.Protocols.SoapException err)
   {
      // Process the platform error here
      strErrorMsg = ("ErrorMessage: " + err.Message + " " + err.Detail.OuterXml + " Source: " + err.Source );
   }
   catch(Exception err)
   {
      // Process other errors here
      strErrorMsg = ("ErrorMessage: " + err.Message + "Source: " + err.Source );
   }
}

© 2003 Microsoft Corporation. All rights reserved.