Using system;
Using Microsoft. xrm. SDK;
Using Microsoft. CRM. SDK. messages;
Public class opportunityhelper
{
Public static readonly string entityname = "opportunity ";
Public guid opportunityid = guid. empty;
/// <Summary>
/// Create a business opportunity
/// </Summary>
/// <Param name = "service"> service </param>
/// <Param name = "accountid"> potential customers </param>
Public void create (iorganizationservice service, guid accountid)
{
Entity en = new entity () {logicalname = entityname };
// Subject
En ["name"] = "test business opportunity ";
// Potential customers
En ["customerid"] = new entityreference () {logicalname = entityname, id = accountid };
Opportunityid = service. Create (en );
}
/// <Summary>
/// Calculate the Business Opportunity value
/// </Summary>
/// <Param name = "service"> service </param>
Public decimal calculateactualvalueopportunity (iorganizationservice Service)
{
Calculateactualvalueopportunityrequest request = new calculateactualvalueopportunityrequest ();
Request. opportunityid = opportunityid;
Calculateactualvalueopportunityresponse response =
(Calculateactualvalueopportunityresponse) service. Execute (request );
Return response. value;
}
/// <Summary>
/// Change the business opportunity to win
/// 1: in progress, 2: waiting, 3: Winning, 4: canceled, 5: sold out
/// </Summary>
Public void winopportunity (iorganizationservice Service)
{
Winopportunityrequest request = new winopportunityrequest ();
Request. opportunityclose = new entity () {logicalname = entityname, id = opportunityid };
Request. Status = new optionsetvalue (3 );
Service. Execute (request );
}
/// <Summary>
/// Change the business opportunity to ticket loss
/// 1: in progress, 2: waiting, 3: Winning, 4: canceled, 5: sold out
/// </Summary>
Public void loseopportunity (iorganizationservice Service)
{
Loseopportunityrequest request = new loseopportunityrequest ();
Request. opportunityclose = new entity () {logicalname = entityname, id = opportunityid };
Request. Status = new optionsetvalue (4 );
Service. Execute (request );
}
/// <Summary>
/// Generate a quote for business opportunities
/// </Summary>
/// <Param name = "service"> service </param>
Public void toquote (iorganizationservice Service)
{
Generatequotefromopportunityrequest request = new generatequotefromopportunityrequest ();
Request. opportunityid = opportunityid;
// These fields will be converted
Request. columnset = new Microsoft. xrm. SDK. query. columnset ("name", "customerid ");
Generatequotefromopportunityresponse response = (generatequotefromopportunityresponse) service. Execute (request );
// Quote entity
Entity quoteen = response. entity;
}
/// <Summary>
/// Generate a sales order
/// </Summary>
/// <Param name = "service"> service </param>
Public void tosaleorder (iorganizationservice Service)
{
Generatesalesorderfromopportunityrequest request = new generatesalesorderfromopportunityrequest ();
Request. opportunityid = opportunityid;
// These fields will be converted
Request. columnset = new Microsoft. xrm. SDK. query. columnset ("name", "customerid ");
Generatesalesorderfromopportunityresponse response = (generatesalesorderfromopportunityresponse) service. Execute (request );
// Sales order
Entity saloorderen = response. entity;
}
/// <Summary>
/// Generate invoice for business opportunity
/// </Summary>
/// <Param name = "service"> service </param>
Public void toinvoicefrom (iorganizationservice Service)
{
Generateinvoicefromopportunityrequest request = new generateinvoicefromopportunityrequest ();
Request. opportunityid = opportunityid;
// These fields will be converted
Request. columnset = new Microsoft. xrm. SDK. query. columnset ("name", "customerid ");
Generateinvoicefromopportunityresponse response = (generateinvoicefromopportunityresponse) service. Execute (request );
// Invoice
Entity invoicefromen = response. entity;
}
/// <Summary>
/// Obtain a small number of product values for the object specified in the target value
/// </Summary>
/// <Param name = "service"> service </param>
/// <Param name = "productid"> product ID </param>
/// <Param name = "uomid"> unit ID </param>
Public int getquantitydecimal (iorganizationservice service, guid productid, guid uomid)
{
Getquantitydecimalrequest request = new getquantitydecimalrequest ();
Request. Target = new entityreference () {logicalname = entityname, id = opportunityid };
Request. productid = productid;
Request. uomid = uomid;
Getquantitydecimalresponse response = (getquantitydecimalresponse) service. Execute (request );
Return response. quantity;
}
/// <Summary>
/// Retrieve the access permissions of the specified security subject (user or Team) on the business opportunity
/// </Summary>
/// <Param name = "service"> service </param>
/// <Param name = "principal"> User or team </param>
Public accessrights retrieveprincipalaccess (iorganizationservice service, entityreference principal)
{
Retrieveprincipalaccessrequest request = new retrieveprincipalaccessrequest ();
Request. Target = new entityreference () {logicalname = entityname, id = opportunityid };
Request. Principal = principal;
Retrieveprincipalaccessresponse response = (retrieveprincipalaccessresponse) service. Execute (request );
Return response. accessrights;
}
/// <Summary>
/// Retrieve the security entity (user or Team) that has the right to access the business opportunity and its access permissions to the business opportunity
/// </Summary>
Public void retrievesharedprincipalsandaccess (iorganizationservice Service)
{
Retrievesharedprincipalsandaccessrequest request = new retrievesharedprincipalsandaccessrequest ();
Request. Target = new entityreference () {logicalname = entityname, id = opportunityid };
Retrievesharedprincipalsandaccessresponse response =
(Retrievesharedprincipalsandaccessresponse) service. Execute (request );
If (response! = NULL & response. principalaccesses! = NULL)
{
Foreach (principalaccess Pa in response. principalaccesses)
{
// Access permission
Accessrights = pa. accessmask;
// User or team reference
Entityreference principal = pa. Principal;
}
}
}
/// <Summary>
/// Remove all access permissions of the specified security entity (user or Team) to the business opportunity.
/// </Summary>
/// <Param name = "service"> service </param>
/// <Param name = "revokee"> User or team </param>
Public void revokeaccess (iorganizationservice service, entityreference revokee)
{
Revokeaccessrequest request = new revokeaccessrequest ();
Request. Target = new entityreference () {logicalname = entityname, id = opportunityid };
Request. revokee = revokee;
Service. Execute (request );
}
/// <Summary>
/// Delete business opportunities
/// </Summary>
Public void Delete (iorganizationservice Service)
{
Service. Delete (entityname, opportunityid );
}
}
CRM operation business opportunity entity