Compartir a través de


Sample: Manage contracts

 

Applies To: Dynamics CRM 2013

This sample code is for Microsoft Dynamics CRM 2013 and Microsoft Dynamics CRM Online. Download the Microsoft Dynamics CRM SDK package. It can be found in the following location in the download package:

SampleCode\CS\BusinessDataModel\Service\WorkingWithContracts.cs

SampleCode\VB\BusinessDataModel\Service\WorkingWithContracts.vb

Requirements

For more information about the requirements for running the sample code provided in this SDK, see Use the sample and helper code.

Demonstrates

This sample shows how to create and manage contracts.

Example


using System;
using System.ServiceModel;

// These namespaces are found in the Microsoft.Xrm.Sdk.dll assembly
// located in the SDK\bin folder of the SDK download.
using Microsoft.Xrm.Sdk;
using Microsoft.Xrm.Sdk.Query;
using Microsoft.Xrm.Sdk.Client;

// This namespace is found in Microsoft.Crm.Sdk.Proxy.dll assembly
// found in the SDK\bin folder.
using Microsoft.Crm.Sdk.Messages;

namespace Microsoft.Crm.Sdk.Samples
{
    /// <summary>
    /// Demonstrates how to do manage contract records programmatically.</summary>
    /// <remarks>
    /// At run-time, you will be given the option to delete all the
    /// database records created by this program.</remarks>
    public class WorkingWithContracts
    {
        #region Class Level Members

        private Guid _accountId;
        private Guid _contractId;
        private Guid _contractTemplateId;
        private Guid _firstCloneId;
        private Guid _secondCloneId;
        private Guid _renewedId;
        private OrganizationServiceProxy _serviceProxy;

        #endregion Class Level Members

        #region How To Sample Code
        /// <summary>
        /// This method first connects to the Organization service. Afterwards,
        /// a Contract Template and several Contracts are created, demonstrating how to
        /// create and work with the Contract entity.
        /// </summary>
        /// <param name="serverConfig">Contains server connection information.</param>
        /// <param name="promptforDelete">When True, the user will be prompted to delete all
        /// created entities.</param>
        public void Run(ServerConnection.Configuration serverConfig, bool promptforDelete)
        {
            try
            {
                // Connect to the Organization service. 
                // The using statement assures that the service proxy will be properly disposed.
                using (_serviceProxy = ServerConnection.GetOrganizationProxy(serverConfig))
                {
                    // This statement is required to enable early-bound type support.
                    _serviceProxy.EnableProxyTypes();

                    CreateRequiredRecords();

                    #region Create Contract Template

                    // First, attempt to retrieve the Contract Template. Otherwise, 
                    // create the template.
                    QueryExpression templateQuery = new QueryExpression()
                    {
                        EntityName = ContractTemplate.EntityLogicalName,
                        ColumnSet = new ColumnSet("contracttemplateid"),
                        Criteria =
                        {
                            Conditions =
                            {
                                new ConditionExpression("abbreviation", ConditionOperator.Equal, "SCT")
                            }
                        }
                    };
                    EntityCollection ec = _serviceProxy.RetrieveMultiple(templateQuery);

                    if (ec.Entities.Count > 0)
                    {
                        _contractTemplateId = ec.Entities[0].Id;
                        Console.Write("Template retrieved, ");
                    }
                    else
                    {
                        ContractTemplate contractTemplate = new ContractTemplate()
                        {
                            Name = "Sample Contract Template",
                            BillingFrequencyCode = new OptionSetValue((int)ContractTemplateBillingFrequencyCode.Monthly),
                            Abbreviation = "SCT",
                            AllotmentTypeCode = new OptionSetValue((int)ContractTemplateAllotmentTypeCode.NumberofCases),
                            EffectivityCalendar =
                                "--------+++++++++---------------+++++++++---------------+++++++++---------------+++++++++---------------+++++++++-------------------------------------------------------"
                        };
                        _contractTemplateId = _serviceProxy.Create(contractTemplate);
                        Console.Write("Template created, ");
                    }

                    #endregion

                    #region Create Contract

                    // Create a Contract from the Contract Template.
                    Contract contract = new Contract()
                    {
                        Title = "Sample Contract",
                        ContractTemplateId = new EntityReference
                        {
                            Id = _contractTemplateId,
                            LogicalName = ContractTemplate.EntityLogicalName
                        },
                        CustomerId = new EntityReference
                        {
                            Id = _accountId,
                            LogicalName = Account.EntityLogicalName
                        },
                        BillingCustomerId = new EntityReference
                        {
                            Id = _accountId,
                            LogicalName = Account.EntityLogicalName
                        },
                        ActiveOn = new DateTime(2015, 1, 1),
                        ExpiresOn = new DateTime(2020, 1, 1),
                        BillingStartOn = new DateTime(2015, 1, 1),
                        BillingEndOn = new DateTime(2020, 1, 1)
                    };
                    _contractId = _serviceProxy.Create(contract);

                    Console.Write("parent contract created, ");

                    // Create a contract line item.
                    ContractDetail contractLineItem = new ContractDetail()
                    {
                        Title = "Sample Contract Line Item",
                        ContractId = new EntityReference
                        {
                            Id = _contractId,
                            LogicalName = Contract.EntityLogicalName
                        },
                        CustomerId = new EntityReference
                        {
                            Id = _accountId,
                            LogicalName = Account.EntityLogicalName
                        },
                        ActiveOn = new DateTime(2015, 1, 1),
                        ExpiresOn = new DateTime(2020, 1, 1),
                        Price = new Money(20.0M),
                        TotalAllotments = 20
                    };
                    _serviceProxy.Create(contractLineItem);

                    Console.Write("contract line attached, ");

                    #endregion

                    #region Clone contract twice

                    // Create the first clone of the contract.
                    CloneContractRequest cloneRequest = new CloneContractRequest()
                    {
                        ContractId = _contractId,
                        IncludeCanceledLines = false
                    };
                    CloneContractResponse cloneResponse =
                        (CloneContractResponse)_serviceProxy.Execute(cloneRequest);
                    _firstCloneId = ((Contract)cloneResponse.Entity).ContractId.Value;

                    Console.Write("first clone created, ");

                    // Create the second clone of the contract.
                    cloneRequest = new CloneContractRequest()
                    {
                        ContractId = _contractId,
                        IncludeCanceledLines = true
                    };
                    cloneResponse =
                        (CloneContractResponse)_serviceProxy.Execute(cloneRequest);
                    _secondCloneId = ((Contract)cloneResponse.Entity).ContractId.Value;

                    Console.Write("second clone created. \n");

                    // Retrieve all Contracts.
                    QueryExpression contractQuery = new QueryExpression()
                    {
                        EntityName = Contract.EntityLogicalName,
                        ColumnSet = new ColumnSet("contractid"),
                        Criteria =
                        {
                            Conditions = 
                            {
                                new ConditionExpression("customerid", ConditionOperator.Equal, _accountId)
                            }
                        }
                    };
                    EntityCollection contracts = _serviceProxy.RetrieveMultiple(contractQuery);

                    // Display the retrieved Contract Ids.
                    for (int i = 0; i < contracts.Entities.Count; i++)
                    {
                        Console.WriteLine("Retrieved contract with Id: {0}",
                            ((Contract)contracts.Entities[i]).ContractId);
                    }

                    #endregion

                    #region Deactivate a cloned contract

                    // In order to deactivate a contract (put it on hold), it is first
                    // necessary to invoice the contract.
                    SetStateRequest setStateRequest = new SetStateRequest()
                    {
                        EntityMoniker = new EntityReference
                        {
                            Id = _firstCloneId,
                            LogicalName = Contract.EntityLogicalName
                        },
                        State = new OptionSetValue((int)ContractState.Invoiced),
                        Status = new OptionSetValue(2)
                    };
                    _serviceProxy.Execute(setStateRequest);

                    Console.Write("Contract invoiced, ");

                    // Now that the contract has been invoiced, it is possible to put
                    // the contract on hold.
                    setStateRequest = new SetStateRequest()
                    {
                        EntityMoniker = new EntityReference
                        {
                            Id = _firstCloneId,
                            LogicalName = Contract.EntityLogicalName
                        },
                        State = new OptionSetValue((int)ContractState.OnHold),
                        Status = new OptionSetValue(4)
                    };
                    _serviceProxy.Execute(setStateRequest);

                    Console.Write("and put on hold.\n");

                    #endregion

                    #region Renew an invoiced contract

                    // In order to renew a contract, it must be invoiced first, and
                    // then canceled.

                    // Invoice the contract.
                    setStateRequest = new SetStateRequest()
                    {
                        EntityMoniker = new EntityReference
                        {
                            Id = _contractId,
                            LogicalName = Contract.EntityLogicalName
                        },
                        State = new OptionSetValue((int)ContractState.Invoiced),
                        Status = new OptionSetValue(3)
                    };
                    _serviceProxy.Execute(setStateRequest);

                    Console.Write("Contract invoiced, ");

                    // Cancel the contract.
                    setStateRequest = new SetStateRequest()
                    {
                        EntityMoniker = new EntityReference
                        {
                            Id = _contractId,
                            LogicalName = Contract.EntityLogicalName
                        },
                        State = new OptionSetValue((int)ContractState.Canceled),
                        Status = new OptionSetValue(5)
                    };
                    _serviceProxy.Execute(setStateRequest);

                    Console.Write("canceled, ");

                    // Renew the canceled contract.
                    RenewContractRequest renewRequest = new RenewContractRequest()
                    {
                        ContractId = _contractId,
                        IncludeCanceledLines = true,
                        Status = 1
                    };
                    RenewContractResponse renewResponse =
                        (RenewContractResponse)_serviceProxy.Execute(renewRequest);

                    // Retrieve Id of renewed contract.
                    _renewedId = ((Contract)renewResponse.Entity).ContractId.Value;

                    // Display the Id of the renewed contract.
                    Console.WriteLine("and renewed.");

                    #endregion

                    DeleteRequiredRecords(promptforDelete);
                }
            }

            // Catch any service fault exceptions that Microsoft Dynamics CRM throws.
            catch (FaultException<Microsoft.Xrm.Sdk.OrganizationServiceFault>)
            {
                // You can handle an exception here or pass it back to the calling method.
                throw;
            }
        }

        /// <summary>
        /// Creates any entity records that this sample requires.
        /// </summary>
        public void CreateRequiredRecords()
        {
            // Instantiate an Account object.
            // See the Entity Metadata topic in the SDK documentation to determine
            // which attributes must be set for each entity.
            Account setupAccount = new Account()
            {
                Name = "Litware, Inc."
            };
            _accountId = _serviceProxy.Create(setupAccount);
        }

        /// <summary>
        /// Deletes any entity records that were created for this sample.
        /// <param name="prompt">Indicates whether to prompt the user 
        /// to delete the records created in this sample.</param>
        /// </summary>
        public void DeleteRequiredRecords(bool prompt)
        {
            // Two of the contracts, their associated account and the contract template
            // records that were created and used in this sample will continue to exist 
            // on your system because contracts that have been invoiced cannot be deleted 
            // in Microsoft Dynamics CRM. They can only be put on hold or canceled.

            bool toBeDeleted = true;

            if (prompt)
            {
                // Ask the user if the created entities should be deleted.
                Console.Write("\nDo you want these entity records deleted? (y/n) [y]: ");
                String answer = Console.ReadLine();
                if (answer.StartsWith("y") ||
                    answer.StartsWith("Y") ||
                    answer == String.Empty)
                {
                    toBeDeleted = true;
                }
                else
                {
                    toBeDeleted = false;
                }
            }

            if (toBeDeleted)
            {                
                _serviceProxy.Delete(Contract.EntityLogicalName, _secondCloneId);
                _serviceProxy.Delete(Contract.EntityLogicalName, _renewedId);
                _serviceProxy.Delete(Contract.EntityLogicalName, _contractId);
                SetStateRequest setStateRequest = new SetStateRequest()
                {
                    EntityMoniker = new EntityReference
                    {
                        Id = _firstCloneId,
                        LogicalName = Contract.EntityLogicalName
                    },
                    State = new OptionSetValue((int)ContractState.Invoiced),
                    Status = new OptionSetValue(3)
                };
                _serviceProxy.Execute(setStateRequest);
                setStateRequest = new SetStateRequest()
                {
                    EntityMoniker = new EntityReference
                    {
                        Id = _firstCloneId,
                        LogicalName = Contract.EntityLogicalName
                    },
                    State = new OptionSetValue((int)ContractState.Canceled),
                    Status = new OptionSetValue(5)
                };
                _serviceProxy.Execute(setStateRequest);
                _serviceProxy.Delete(Contract.EntityLogicalName, _firstCloneId);
                _serviceProxy.Delete(Account.EntityLogicalName, _accountId);
                Console.WriteLine("Entity record(s) have been deleted.");
            }
        }

        #endregion How To Sample Code

        #region Main method

        /// <summary>
        /// Standard Main() method used by most SDK samples.
        /// </summary>
        /// <param name="args"></param>
        static public void Main(string[] args)
        {
            try
            {
                // Obtain the target organization's Web address and client logon 
                // credentials from the user.
                ServerConnection serverConnect = new ServerConnection();
                ServerConnection.Configuration config = serverConnect.GetServerConfiguration();

                WorkingWithContracts app = new WorkingWithContracts();
                app.Run(config, true);
            }
            catch (FaultException<Microsoft.Xrm.Sdk.OrganizationServiceFault> ex)
            {
                Console.WriteLine("The application terminated with an error.");
                Console.WriteLine("Timestamp: {0}", ex.Detail.Timestamp);
                Console.WriteLine("Code: {0}", ex.Detail.ErrorCode);
                Console.WriteLine("Message: {0}", ex.Detail.Message);
                Console.WriteLine("Plugin Trace: {0}", ex.Detail.TraceText);
                Console.WriteLine("Inner Fault: {0}",
                    null == ex.Detail.InnerFault ? "No Inner Fault" : "Has Inner Fault");
            }
            catch (System.TimeoutException ex)
            {
                Console.WriteLine("The application terminated with an error.");
                Console.WriteLine("Message: {0}", ex.Message);
                Console.WriteLine("Stack Trace: {0}", ex.StackTrace);
                Console.WriteLine("Inner Fault: {0}",
                    null == ex.InnerException.Message ? "No Inner Fault" : ex.InnerException.Message);
            }
            catch (System.Exception ex)
            {
                Console.WriteLine("The application terminated with an error.");
                Console.WriteLine(ex.Message);

                // Display the details of the inner exception.
                if (ex.InnerException != null)
                {
                    Console.WriteLine(ex.InnerException.Message);

                    FaultException<Microsoft.Xrm.Sdk.OrganizationServiceFault> fe = ex.InnerException
                        as FaultException<Microsoft.Xrm.Sdk.OrganizationServiceFault>;
                    if (fe != null)
                    {
                        Console.WriteLine("Timestamp: {0}", fe.Detail.Timestamp);
                        Console.WriteLine("Code: {0}", fe.Detail.ErrorCode);
                        Console.WriteLine("Message: {0}", fe.Detail.Message);
                        Console.WriteLine("Plugin Trace: {0}", fe.Detail.TraceText);
                        Console.WriteLine("Inner Fault: {0}",
                            null == fe.Detail.InnerFault ? "No Inner Fault" : "Has Inner Fault");
                    }
                }
            }
            // Additional exceptions to catch: SecurityTokenValidationException, ExpiredSecurityTokenException,
            // SecurityAccessDeniedException, MessageSecurityException, and SecurityNegotiationException.

            finally
            {
                Console.WriteLine("Press <Enter> to exit.");
                Console.ReadLine();
            }
        }
        #endregion Main method
    }
}

Imports System.ServiceModel

' These namespaces are found in the Microsoft.Xrm.Sdk.dll assembly
' located in the SDK\bin folder of the SDK download.
Imports Microsoft.Xrm.Sdk
Imports Microsoft.Xrm.Sdk.Query
Imports Microsoft.Xrm.Sdk.Client

' This namespace is found in Microsoft.Crm.Sdk.Proxy.dll assembly
' found in the SDK\bin folder.
Imports Microsoft.Crm.Sdk.Messages

Namespace Microsoft.Crm.Sdk.Samples
    ''' <summary>
    ''' Demonstrates how to do manage contract records programmatically.</summary>
    ''' <remarks>
    ''' At run-time, you will be given the option to delete all the
    ''' database records created by this program.</remarks>
    Public Class WorkingWithContracts
        #Region "Class Level Members"

        Private _accountId As Guid
        Private _contractId As Guid
        Private _contractTemplateId As Guid
        Private _firstCloneId As Guid
        Private _secondCloneId As Guid
        Private _renewedId As Guid
        Private _serviceProxy As OrganizationServiceProxy

        #End Region ' Class Level Members

        #Region "How To Sample Code"
        ''' <summary>
        ''' This method first connects to the Organization service. Afterwards,
        ''' a Contract Template and several Contracts are created, demonstrating how to
        ''' create and work with the Contract entity.
        ''' </summary>
        ''' <param name="serverConfig">Contains server connection information.</param>
        ''' <param name="promptforDelete">When True, the user will be prompted to delete all
        ''' created entities.</param>
        Public Sub Run(ByVal serverConfig As ServerConnection.Configuration, ByVal promptforDelete As Boolean)
            Try
                ' Connect to the Organization service. 
                ' The using statement assures that the service proxy will be properly disposed.
                _serviceProxy = ServerConnection.GetOrganizationProxy(serverConfig)
                Using _serviceProxy
                    ' This statement is required to enable early-bound type support.
                    _serviceProxy.EnableProxyTypes()

                    CreateRequiredRecords()

'                    #Region "Create Contract Template"

                    ' First, attempt to retrieve the Contract Template. Otherwise, 
                    ' create the template.
                    Dim templateQuery As New QueryExpression() With { _
                        .EntityName = ContractTemplate.EntityLogicalName, .ColumnSet = New ColumnSet("contracttemplateid")}
                    templateQuery.Criteria = New FilterExpression()
                    templateQuery.Criteria.AddCondition("abbreviation", ConditionOperator.Equal, {"SCT"})
                    templateQuery.Criteria.FilterOperator = LogicalOperator.And

                    Dim ec As EntityCollection = _serviceProxy.RetrieveMultiple(templateQuery)

                    If ec.Entities.Count > 0 Then
                        _contractTemplateId = ec.Entities(0).Id
                        Console.Write("Template retrieved, ")
                    Else
                        Dim contractTemplate As New ContractTemplate() With { _
                            .Name = "Sample Contract Template", .BillingFrequencyCode = New OptionSetValue(1), .Abbreviation = "SCT", _
                            .AllotmentTypeCode = New OptionSetValue(1), _
                            .EffectivityCalendar = "--------+++++++++---------------+++++++++---------------+++++++++---------------" &amp; _
                                        "+++++++++---------------+++++++++-------------------------------------------------------"}
                        _contractTemplateId = _serviceProxy.Create(contractTemplate)
                        Console.Write("Template created, ")
                    End If

'                    #End Region

'                    #Region "Create Contract"

                    ' Create a Contract from the Contract Template.
                    Dim contract As New Contract() With {.Title = "Sample Contract", .ContractTemplateId = New EntityReference With { _
                            .Id = _contractTemplateId, .LogicalName = ContractTemplate.EntityLogicalName}, _
                            .CustomerId = New EntityReference With {.Id = _accountId, .LogicalName = Account.EntityLogicalName}, _
                            .BillingCustomerId = New EntityReference With {.Id = _accountId, .LogicalName = Account.EntityLogicalName}, _
                            .ActiveOn = New Date(2015, 1, 1), .ExpiresOn = New Date(2020, 1, 1), .BillingStartOn = New Date(2015, 1, 1), _
                            .BillingEndOn = New Date(2020, 1, 1)}
                    _contractId = _serviceProxy.Create(contract)

                    Console.Write("parent contract created, ")

                    ' Create a contract line item.
                    Dim contractLineItem As New ContractDetail() With {.Title = "Sample Contract Line Item", _
                        .ContractId = New EntityReference With {.Id = _contractId, .LogicalName = contract.EntityLogicalName}, _
                        .CustomerId = New EntityReference With {.Id = _accountId, .LogicalName = Account.EntityLogicalName}, _
                        .ActiveOn = New Date(2015, 1, 1), .ExpiresOn = New Date(2020, 1, 1), .Price = New Money(20D), .TotalAllotments = 20}
                    _serviceProxy.Create(contractLineItem)

                    Console.Write("contract line attached, ")

'                    #End Region

'                    #Region "Clone contract twice"

                    ' Create the first clone of the contract.
                    Dim cloneRequest As New CloneContractRequest() With {.ContractId = _contractId, .IncludeCanceledLines = False}
                    Dim cloneResponse As CloneContractResponse = CType(_serviceProxy.Execute(cloneRequest), CloneContractResponse)
                    _firstCloneId = (CType(cloneResponse.Entity, Contract)).ContractId.Value

                    Console.Write("first clone created, ")

                    ' Create the second clone of the contract.
                    cloneRequest = New CloneContractRequest() With {.ContractId = _contractId, .IncludeCanceledLines = True}
                    cloneResponse = CType(_serviceProxy.Execute(cloneRequest), CloneContractResponse)
                    _secondCloneId = (CType(cloneResponse.Entity, Contract)).ContractId.Value

                    Console.Write("second clone created. " &amp; vbLf)

                    ' Retrieve all Contracts.
                    Dim contractQuery As New QueryExpression() With {.EntityName = contract.EntityLogicalName, _
                                                                     .ColumnSet = New ColumnSet("contractid")}
                    contractQuery.Criteria = New FilterExpression()
                    contractQuery.Criteria.AddCondition("customerid", ConditionOperator.Equal, _accountId)
                    Dim contracts As EntityCollection = _serviceProxy.RetrieveMultiple(contractQuery)

                    ' Display the retrieved Contract Ids.
                    For i As Integer = 0 To contracts.Entities.Count - 1
                        Console.WriteLine("Retrieved contract with Id: {0}", (CType(contracts.Entities(i), Contract)).ContractId)
                    Next i

'                    #End Region

'                    #Region "Deactivate a cloned contract"

                    ' In order to deactivate a contract (put it on hold), it is first
                    ' necessary to invoice the contract.
                    Dim setStateRequest As New SetStateRequest() With {.EntityMoniker = New EntityReference With { _
                            .Id = _firstCloneId, .LogicalName = contract.EntityLogicalName}, _
                            .State = New OptionSetValue(CInt(Fix(ContractState.Invoiced))), .Status = New OptionSetValue(2)}
                    _serviceProxy.Execute(setStateRequest)

                    Console.Write("Contract invoiced, ")

                    ' Now that the contract has been invoiced, it is possible to put
                    ' the contract on hold.
                    setStateRequest = New SetStateRequest() With {.EntityMoniker = New EntityReference With { _
                            .Id = _firstCloneId, .LogicalName = contract.EntityLogicalName}, _
                            .State = New OptionSetValue(CInt(Fix(ContractState.OnHold))), .Status = New OptionSetValue(4)}
                    _serviceProxy.Execute(setStateRequest)

                    Console.Write("and put on hold." &amp; vbLf)

'                    #End Region

'                    #Region "Renew an invoiced contract"

                    ' In order to renew a contract, it must be invoiced first, and
                    ' then canceled.

                    ' Invoice the contract.
                    setStateRequest = New SetStateRequest() With {.EntityMoniker = New EntityReference With { _
                            .Id = _contractId, .LogicalName = contract.EntityLogicalName}, _
                            .State = New OptionSetValue(CInt(Fix(ContractState.Invoiced))), .Status = New OptionSetValue(3)}
                    _serviceProxy.Execute(setStateRequest)

                    Console.Write("Contract invoiced, ")

                    ' Cancel the contract.
                    setStateRequest = New SetStateRequest() With { _
                        .EntityMoniker = New EntityReference With {.Id = _contractId, .LogicalName = contract.EntityLogicalName}, _
                        .State = New OptionSetValue(CInt(Fix(ContractState.Canceled))), .Status = New OptionSetValue(5)}
                    _serviceProxy.Execute(setStateRequest)

                    Console.Write("canceled, ")

                    ' Renew the canceled contract.
                    Dim renewRequest As New RenewContractRequest() With {.ContractId = _contractId, .IncludeCanceledLines = True, .Status = 1}
                    Dim renewResponse As RenewContractResponse = CType(_serviceProxy.Execute(renewRequest), RenewContractResponse)

                    ' Retrieve Id of renewed contract.
                    _renewedId = (CType(renewResponse.Entity, Contract)).ContractId.Value

                    ' Display the Id of the renewed contract.
                    Console.WriteLine("and renewed.")

'                    #End Region

                    DeleteRequiredRecords(promptforDelete)
                End Using

            ' Catch any service fault exceptions that Microsoft Dynamics CRM throws.
            Catch fe As FaultException(Of Microsoft.Xrm.Sdk.OrganizationServiceFault)
                ' You can handle an exception here or pass it back to the calling method.
                Throw
            End Try
        End Sub

        ''' <summary>
        ''' Creates any entity records that this sample requires.
        ''' </summary>
        Public Sub CreateRequiredRecords()
            ' Instantiate an Account object.
            ' See the Entity Metadata topic in the SDK documentation to determine
            ' which attributes must be set for each entity.
            Dim setupAccount As New Account() With {.Name = "Litware, Inc."}
            _accountId = _serviceProxy.Create(setupAccount)
        End Sub

        ''' <summary>
        ''' Deletes any entity records that were created for this sample.
        ''' <param name="prompt">Indicates whether to prompt the user 
        ''' to delete the records created in this sample.</param>
        ''' </summary>
        Public Sub DeleteRequiredRecords(ByVal prompt As Boolean)
            ' Two of the contracts, their associated account and the contract template
            ' records that were created and used in this sample will continue to exist 
            ' on your system because contracts that have been invoiced cannot be deleted 
            ' in Microsoft Dynamics CRM. They can only be put on hold or canceled.

            Dim toBeDeleted As Boolean = True

            If prompt Then
                ' Ask the user if the created entities should be deleted.
                Console.Write(vbLf &amp; "Do you want these entity records deleted? (y/n) [y]: ")
                Dim answer As String = Console.ReadLine()
                If answer.StartsWith("y") OrElse answer.StartsWith("Y") OrElse answer = String.Empty Then
                    toBeDeleted = True
                Else
                    toBeDeleted = False
                End If
            End If

            If toBeDeleted Then
                _serviceProxy.Delete(Contract.EntityLogicalName, _secondCloneId)
                _serviceProxy.Delete(Contract.EntityLogicalName, _renewedId)
                _serviceProxy.Delete(Contract.EntityLogicalName, _contractId)
                Dim setStateRequest As New SetStateRequest() With
                    {
                        .EntityMoniker = New EntityReference With
                                         {
                                             .Id = _firstCloneId,
                                             .LogicalName = Contract.EntityLogicalName
                                         },
                        .State = New OptionSetValue(CInt(Fix(ContractState.Invoiced))),
                        .Status = New OptionSetValue(3)
                    }
                _serviceProxy.Execute(setStateRequest)
                setStateRequest = New SetStateRequest() With
                                  {
                                      .EntityMoniker = New EntityReference With
                                                       {
                                                           .Id = _firstCloneId,
                                                           .LogicalName = Contract.EntityLogicalName
                                                       },
                                      .State = New OptionSetValue(CInt(Fix(ContractState.Canceled))),
                                      .Status = New OptionSetValue(5)
                                  }
                _serviceProxy.Execute(setStateRequest)
                _serviceProxy.Delete(Contract.EntityLogicalName, _firstCloneId)
                _serviceProxy.Delete(Account.EntityLogicalName, _accountId)
                Console.WriteLine("Entity record(s) have been deleted.")
            End If
        End Sub

        #End Region ' How To Sample Code

        #Region "Main method"

        ''' <summary>
        ''' Standard Main() method used by most SDK samples.
        ''' </summary>
        ''' <param name="args"></param>
        Public Shared Sub Main(ByVal args() As String)
            Try
                ' Obtain the target organization's Web address and client logon 
                ' credentials from the user.
                Dim serverConnect As New ServerConnection()
                Dim config As ServerConnection.Configuration = serverConnect.GetServerConfiguration()

                Dim app As New WorkingWithContracts()
                app.Run(config, True)
            Catch ex As FaultException(Of Microsoft.Xrm.Sdk.OrganizationServiceFault)
                Console.WriteLine("The application terminated with an error.")
                Console.WriteLine("Timestamp: {0}", ex.Detail.Timestamp)
                Console.WriteLine("Code: {0}", ex.Detail.ErrorCode)
                Console.WriteLine("Message: {0}", ex.Detail.Message)
                Console.WriteLine("Plugin Trace: {0}", ex.Detail.TraceText)
                Console.WriteLine("Inner Fault: {0}", If(Nothing Is ex.Detail.InnerFault, "No Inner Fault", "Has Inner Fault"))
            Catch ex As TimeoutException
                Console.WriteLine("The application terminated with an error.")
                Console.WriteLine("Message: {0}", ex.Message)
                Console.WriteLine("Stack Trace: {0}", ex.StackTrace)
                Console.WriteLine("Inner Fault: {0}", If(Nothing Is ex.InnerException.Message, "No Inner Fault", ex.InnerException.Message))
            Catch ex As Exception
                Console.WriteLine("The application terminated with an error.")
                Console.WriteLine(ex.Message)

                ' Display the details of the inner exception.
                If ex.InnerException IsNot Nothing Then
                    Console.WriteLine(ex.InnerException.Message)

                    Dim fe As FaultException(Of Microsoft.Xrm.Sdk.OrganizationServiceFault) = _
                        TryCast(ex.InnerException, FaultException(Of Microsoft.Xrm.Sdk.OrganizationServiceFault))
                    If fe IsNot Nothing Then
                        Console.WriteLine("Timestamp: {0}", fe.Detail.Timestamp)
                        Console.WriteLine("Code: {0}", fe.Detail.ErrorCode)
                        Console.WriteLine("Message: {0}", fe.Detail.Message)
                        Console.WriteLine("Plugin Trace: {0}", fe.Detail.TraceText)
                        Console.WriteLine("Inner Fault: {0}", If(Nothing Is fe.Detail.InnerFault, "No Inner Fault", "Has Inner Fault"))
                    End If
                End If
            ' Additional exceptions to catch: SecurityTokenValidationException, ExpiredSecurityTokenException,
            ' SecurityAccessDeniedException, MessageSecurityException, and SecurityNegotiationException.

            Finally
                Console.WriteLine("Press <Enter> to exit.")
                Console.ReadLine()
            End Try
        End Sub
        #End Region ' Main method
    End Class
End Namespace

See Also

Contract entities