Find All of the Incidents for an Account or Contact

This sample retrieves all the incidents (cases) associated with an account or contact.

Class Reference

Schema Reference

  • account.xsd
  • contact.xsd
  • incident.xsd

Example

[C#]

public void RetrieveAssociatedCases()
{
   // 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 + "/";
   string sResult = "";

   // 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";

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

   string strErrorMsg;
   try
   {
      Microsoft.CRM.Proxy.CUserAuth oUserAuth = oBizUser.WhoAmI();

      string strAccount = "<account><name>North Wind Traders</name><ownerid type=\"" 
         + Microsoft.CRM.Flags.ObjectType.otSystemUser.ToString() +"\">"
         + oUserAuth.UserId + "</ownerid></account>";

      string strContact = "<contact><firstname>Rachel</firstname>";
      strContact += "<telephone2>206-555-0103</telephone2><lastname>Valdez</lastname>";
      strContact += "<emailaddress1>rachel@northwindtraders.com</emailaddress1><ownerid type=\"";
      strContact += Microsoft.CRM.Flags.ObjectType.otSystemUser.ToString() +"\">";
      strContact += oUserAuth.UserId + "</ownerid></contact>";

      string strAccountId = oAccount.Create(oUserAuth,strAccount);
      string strContactId = oContact.Create(oUserAuth,strContact);

      // Create an incident associated with the Account
      string strIncident = "<incident><title>Incident A</title>";
      strIncident += "<accountid>"+ strAccountId +"</accountid><ownerid type=\"";
      strIncident += Microsoft.CRM.Flags.ObjectType.otSystemUser.ToString() +"\">";
      strIncident += oUserAuth.UserId + "</ownerid></incident>";

      string strIncidentId = oIncident.Create(oUserAuth, strIncident);

      Microsoft.CRM.Proxy.CObjectName oObj = new Microsoft.CRM.Proxy.CObjectName();
      oObj.Id = strAccountId;
      oObj.Type = Microsoft.CRM.Proxy.ObjectType.otAccount;

      // Retrieve Cases associated with the Account
      sResult = oIncident.RetrieveByObject(oUserAuth, oObj, "<columnset><column>title</column></columnset>");

      // Create an incident associated with the Contact
      string strIncident2 = "<incident><title>Incident B</title>";
      strIncident2 += "<contactid>"+ strContactId + "</contactid><ownerid type=\"";
      strIncident2 += Microsoft.CRM.Flags.ObjectType.otSystemUser.ToString() +"\">";
      strIncident2 += oUserAuth.UserId + "</ownerid></incident>";

      strIncidentId = oIncident.Create(oUserAuth, strIncident2);

      oObj = new Microsoft.CRM.Proxy.CObjectName();
      oObj.Id = strContactId;
      oObj.Type = Microsoft.CRM.Proxy.ObjectType.otContact;

      // Retrieve Cases associated with the Account
      sResult = oIncident.RetrieveByObject(oUserAuth, oObj, "<columnset><column>title</column></columnset>");
   }
   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.