Create an Account and Related Contacts
This sample creates an account and some related contacts.
Class Reference
Schema Reference
- account.xsd
- contact.xsd
Example
[C#]
public void CreateAccountAndRelatedContacts()
{
// 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");
String strErrorMsg;
try
{
Microsoft.Crm.Platform.Proxy.CUserAuth userAuth = bizUser.WhoAmI();
StringBuilder accountXml
= new StringBuilder("<account><name>Account One</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><firstname>Dave</firstname>");
contactXml.Append("<lastname>Natsuhara</lastname>");
contactXml.Append("<telephone2>206-555-0100</telephone2>");
contactXml.Append
("<emailaddress1>dave@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>");
String accountId = account.Create(userAuth, accountXml.ToString());
String contactId = contact.Create(userAuth, contactXml.ToString());
// There are two relationships between accounts and contacts:
// Hierarchical and Primary Contact
// Create a hierarcy where the account is the parent of the contact
account.AddSubContact(userAuth, accountId, contactId);
// Make the the contact a primary contact of the account
account.SetPrimaryContact(userAuth, accountId, contactId);
}
catch (System.Web.Services.Protocols.SoapException err)
{
// Process the platform error here
strErrorMsg = String.Concat("ErrorMessage: ", err.Message, " ",
err.Detail.OuterXml, " Source: ", err.Source);
}
}