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 = String.Concat("https://", 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");
// Incident proxy object
Microsoft.Crm.Platform.Proxy.CRMIncident incident
= new Microsoft.Crm.Platform.Proxy.CRMIncident ();
incident.Credentials = System.Net.CredentialCache.DefaultCredentials;
incident.Url = String.Concat(strDir, "CRMIncident.srf");
String strErrorMsg;
try
{
Microsoft.Crm.Platform.Proxy.CUserAuth userAuth = bizUser.WhoAmI();
// Create an account to use later
StringBuilder accountXml = new StringBuilder("<account>");
accountXml.Append("<name>North Wind Traders</name>");
accountXml.AppendFormat("<ownerid type=\"{0}\">",
Microsoft.Crm.Platform.Types.ObjectType.otSystemUser.ToString());
accountXml.AppendFormat("{0}</ownerid></account>", userAuth.UserId);
// Create a contact to use later
StringBuilder contactXml = new StringBuilder("<contact>");
contactXml.Append("<firstname>Rachel</firstname>");
contactXml.Append("<telephone2>506-555-0123</telephone2>");
contactXml.Append("<lastname>Valdez</lastname>");
contactXml.Append
("<emailaddress1>rachel@northwindtraders.com</emailaddress1>");
contactXml.AppendFormat("<ownerid type=\"{0}\">",
Microsoft.Crm.Platform.Types.ObjectType.otSystemUser.ToString());
contactXml.AppendFormat("{0}</ownerid></contact>", userAuth.UserId);
String accountId = account.Create(userAuth, accountXml.ToString());
String contactId = contact.Create(userAuth, contactXml.ToString());
// Create an incident associated with the account
StringBuilder incidentXml = new StringBuilder("<incident>");
incidentXml.Append("<title>Incident A</title>");
incidentXml.AppendFormat("<accountid>{0}</accountid>", accountId);
incidentXml.AppendFormat("<ownerid type=\"{0}\">",
Microsoft.Crm.Platform.Types.ObjectType.otSystemUser.ToString());
incidentXml.AppendFormat("{0}</ownerid></incident>",
userAuth.UserId);
String incidentId = incident.Create(userAuth,
incidentXml.ToString());
Microsoft.Crm.Platform.Proxy.CObjectName obj
= new Microsoft.Crm.Platform.Proxy.CObjectName();
obj.Id = accountId;
obj.Type = Microsoft.Crm.Platform.Proxy.ObjectType.otAccount;
// Retrieve incidents associated with the account
String result = incident.RetrieveByObject(userAuth, obj,
"<columnset><column>title</column></columnset>");
// Create an incident associated with the Contact
StringBuilder incidentXml2 = new StringBuilder("<incident>");
incidentXml2.Append("<title>Incident B</title>");
incidentXml2.AppendFormat("<contactid>{0}</contactid>", contactId);
incidentXml2.AppendFormat("<ownerid type=\"{0}\">",
Microsoft.Crm.Platform.Types.ObjectType.otSystemUser.ToString());
incidentXml2.AppendFormat("{0}</ownerid></incident>",
userAuth.UserId);
incidentId = incident.Create(userAuth, incidentXml2.ToString());
obj = new Microsoft.Crm.Platform.Proxy.CObjectName();
obj.Id = strContactId;
obj.Type = Microsoft.Crm.Platform.Proxy.ObjectType.otContact;
// Retrieve incidents associated with the account
result = incident.RetrieveByObject(userAuth, obj,
"<columnset><column>title</column></columnset>");
}
catch (System.Web.Services.Protocols.SoapException err)
{
// Process the platform error here
strErrorMsg = String.Concat("ErrorMessage: ", err.Message, " ",
err.Detail.OuterXml, " Source: ", err.Source);
}
}