Create a Customer Relationship

banner art

The following sample code demonstrates how to create an instance of a customer relationship between two contacts. Note that before you can assign the values for the customer or lookup, you must first instantiate the correct type.

[C#]
using System;
using System.Collections.Generic;
using System.Text;
using Customerrelationship.CrmSdk;

namespace Customerrelationship
{
    class BuildRelationship
    {
        static void Main(string[] args)
        {

            // Set up the CRM Service.
            CrmService service = new CrmService();
            service.Credentials =
                System.Net.CredentialCache.DefaultCredentials;

            try
            {
                // Instantiate a customerrelationship entity.
                customerrelationship myCR = new customerrelationship();
 
                // Instantiate Lookup.
                myCR.customerroleid = new Lookup();
                // Set the Lookup type to role.
                myCR.customerroleid.type = EntityName.role.ToString();
                // You must replace this with the GUID of a valid 
                // Microsoft CRM role of the primary customer in the relationship.  
                myCR.customerroleid.Value = 
                    new Guid("B6777F84-EA83-DB11-A460-000BDB5C46AB");
  
                // Instantiate customer.   
                myCR.customerid = new Customer();
                // Set the customer type to contact.
                myCR.customerid.type = EntityName.contact.ToString();
                // You must replace this with the GUID of a valid Microsoft 
                // CRM contact, which represents a primary customer
                // in the relationship.
                myCR.customerid.Value = 
                    new Guid("C53E13C5-419F-DB11-86D3-000BDB5C46AB");
                // Instantiate a customer.
                myCR.partnerid = new Customer();
                // Set the customer type to contact.
                myCR.partnerid.type = EntityName.contact.ToString();
                // You must replace this with the GUID of a valid Microsoft 
                // CRM contact, which represents a secondary customer
                // in the relationship.
                myCR.partnerid.Value = 
                    new Guid("D13E13C5-419F-DB11-86D3-000BDB5C46AB");
                // Instantiate Lookup.
                myCR.partnerroleid = new Lookup();
                // Set the Lookup type to role.
                myCR.partnerroleid.type = EntityName.role.ToString();
                // You must replace this with the GUID of a valid 
                // Microsoft CRM role of the secondary customer 
                // in the relationship.  
                myCR.partnerroleid.Value = 
                    new Guid("8C1F4F51-1FF9-41F6-8245-836F2CA6F2F8"); 

                // Create a customerrelationship instance 
                // in Microsoft CRM database. 
                TargetCreateCustomerRelationship cr = 
                    new TargetCreateCustomerRelationship();
                cr.CustomerRelationship = myCR;
                CreateRequest create = new CreateRequest();
                create.Target = cr;
                CreateResponse cresponse = 
                    (CreateResponse)service.Execute(create);
            }
            catch (System.Web.Services.Protocols.SoapException ex)
            {
                Console.WriteLine(ex.Message + "." + ex.Detail.InnerText);
            }      
        }
    }
}

[Visual Basic .NET]
Imports System
Imports System.Collections.Generic
Imports System.Text
Imports Customerrelationship_VB.CrmSdk

Namespace Customerrelationship
    Class BuildRelationship
        Shared Sub Main(ByVal args As String())

            ' Set up the CRM Service.
            Dim service As New CrmService()
            service.Credentials = _
                System.Net.CredentialCache.DefaultCredentials

            Try
                ' Instantiate a customerrelationship entity.
                Dim myCR As New CrmSdk.customerrelationship()

                ' Instantiate Lookup.
                myCR.customerroleid = New Lookup()
                ' Set the Lookup type to role.
                myCR.customerroleid.type = EntityName.role.ToString()
                ' You must replace this with the GUID of a valid 
                ' Microsoft CRM role of the primary customer in the relationship.  
                myCR.customerroleid.Value = _
                    New Guid("B6777F84-EA83-DB11-A460-000BDB5C46AB")

                ' Instantiate customer.   
                myCR.customerid = New Customer()
                ' Set the customer type to contact.
                myCR.customerid.type = EntityName.contact.ToString()
                ' You must replace this with the GUID of a valid Microsoft 
                ' CRM contact, which represents a primary customer
                ' in the relationship.
                myCR.customerid.Value = _
                    New Guid("C53E13C5-419F-DB11-86D3-000BDB5C46AB")
                ' Instantiate a customer.
                myCR.partnerid = New Customer()
                ' Set the customer type to contact.
                myCR.partnerid.type = EntityName.contact.ToString()
                ' You must replace this with the GUID of a valid Microsoft 
                ' CRM contact, which represents a secondary customer
                ' in the relationship.
                myCR.partnerid.Value = _
                    New Guid("D13E13C5-419F-DB11-86D3-000BDB5C46AB")
                ' Instantiate Lookup.
                myCR.partnerroleid = New Lookup()
                ' Set the Lookup type to role.
                myCR.partnerroleid.type = EntityName.role.ToString()
                ' You must replace this with the GUID of a valid 
                ' Microsoft CRM role of the secondary customer 
                ' in the relationship.  
                myCR.partnerroleid.Value = _
                    New Guid("8C1F4F51-1FF9-41F6-8245-836F2CA6F2F8")

                ' Create a customerrelationship instance 
                ' in Microsoft CRM database. 
                Dim cr As New TargetCreateCustomerRelationship()
                cr.CustomerRelationship = myCR
                Dim create As New CreateRequest()
                create.Target = cr
                Dim cresponse As CreateResponse = _
                    DirectCast(service.Execute(create), CreateResponse)
            Catch ex As System.Web.Services.Protocols.SoapException
                Console.WriteLine(ex.Message + "." + ex.Detail.InnerText)
            End Try
        End Sub
    End Class
End Namespace

© 2007 Microsoft Corporation. All rights reserved.