Create an Opportunity and Set a Picklist
The following code example demonstrates how to create an opportunity and set the rating for the opportunity using a picklist value. It also shows how to associate an opportunity with a customer and assign it to a specific user. Note that before you can assign the values for the owner, customer, or picklist, you must first instantiate the correct type.
[C#]
using System;
using System.Collections.Generic;
using System.Text;
using CreaingOpportunity_Picklist.CrmSdk;
namespace CreaingOpportunity_Picklist
{
class Opportunity_Picklist
{
static void Main(string[] args)
{
// Set up the CRM Service.
CrmService service = new CrmService();
service.Credentials =
System.Net.CredentialCache.DefaultCredentials;
try
{
// Instantiate an opportunity entity.
opportunity bikes = new opportunity();
// Set the name property (required).
bikes.name = "New bike store";
// Assign an owner for the opportunity.
bikes.ownerid = new Owner();
bikes.ownerid.type = EntityName.systemuser.ToString();
// You must replace this with the GUID of a valid Microsoft CRM user.
bikes.ownerid.Value =
new Guid("F4D00C90-89C9-DA11-86AF-000BDB5C46AB");
// Associate the opportunity with the potential customer.
bikes.customerid = new Customer();
// You must replace this with the GUID of a valid customer.
bikes.customerid.Value =
new Guid("FD52C7BD-27C1-48A2-8657-0D3FAB00797E");
// Set the type of the customer.
bikes.customerid.type = EntityName.account.ToString();
// Instantiate a picklist.
bikes.opportunityratingcode = new Picklist();
// Set the opportunity rating to warm.
bikes.opportunityratingcode.Value = 2;
// Create the opportunity instance in the Microsoft CRM database.
Guid oppID = service.Create(bikes);
}
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 CreatingOpportunity_Picklist_VB.CrmSdk
Namespace CreatingOpportunity_Picklist
Class Opportunity_Picklist
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 an opportunity entity.
Dim bikes As New opportunity()
' Set the name property (required).
bikes.name = "New bike store"
' Assign an owner for the opportunity.
bikes.ownerid = New Owner()
bikes.ownerid.type = EntityName.systemuser.ToString()
' You must replace this with the GUID of a valid Microsoft CRM user.
bikes.ownerid.Value = New Guid("F4D00C90-89C9-DA11-86AF-000BDB5C46AB")
' Associate the opportunity with the potential customer.
bikes.customerid = New Customer()
' Set the type of the customer.
bikes.customerid.type = EntityName.account.ToString()
' You must replace this with the GUID of a valid customer.
bikes.customerid.Value = New Guid("FD52C7BD-27C1-48A2-8657-0D3FAB00797E")
' Instantiate a picklist.
bikes.opportunityratingcode = New Picklist()
' Set the opportunity rating to warm.
bikes.opportunityratingcode.Value = 2
' Create the opportunity instance in the Microsoft CRM database.
Dim oppID As Guid = service.Create(bikes)
Catch ex As System.Web.Services.Protocols.SoapException
Console.WriteLine(ex.Message + "." + ex.Detail.InnerText)
End Try
End Sub
End Class
End Namespace