標籤:crm建立商機 crm計算商機的值 crm更改商機為贏單 crm更改商機為丟單 crm刪除商機
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>
/// 建立商機
/// </summary>
/// <param name="service">服務</param>
/// <param name="accountId">潛在客戶</param>
public void Create(IOrganizationService service,Guid accountId)
{
Entity en = new Entity() { LogicalName = entityName };
//主題
en["name"] = "測試商機";
//潛在客戶
en["customerid"] = new EntityReference() { LogicalName = entityName,Id = accountId };
opportunityId = service.Create(en);
}
/// <summary>
/// 計算商機的值
/// </summary>
/// <param name="service">服務</param>
public decimal CalculateActualValueOpportunity(IOrganizationService service)
{
CalculateActualValueOpportunityRequest request = new CalculateActualValueOpportunityRequest();
request.OpportunityId = opportunityId;
CalculateActualValueOpportunityResponse response =
(CalculateActualValueOpportunityResponse)service.Execute(request);
return response.Value;
}
/// <summary>
/// 更改商機為贏單
/// 1: 進行中,2: 暫候,3: 贏單,4: 已取消,5: 售完
/// </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>
/// 更改商機為丟單
/// 1: 進行中,2: 暫候,3: 贏單,4: 已取消,5: 售完
/// </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>
/// 商機產生報價單
/// </summary>
/// <param name="service">服務</param>
public void ToQuote(IOrganizationService service)
{
GenerateQuoteFromOpportunityRequest request = new GenerateQuoteFromOpportunityRequest();
request.OpportunityId = opportunityId;
//這些欄位將會轉換
request.ColumnSet = new Microsoft.Xrm.Sdk.Query.ColumnSet("name", "customerid");
GenerateQuoteFromOpportunityResponse response = (GenerateQuoteFromOpportunityResponse)service.Execute(request);
//報價單實體
Entity quoteEn = response.Entity;
}
/// <summary>
/// 商機產生銷售訂單
/// </summary>
/// <param name="service">服務</param>
public void ToSaleOrder(IOrganizationService service)
{
GenerateSalesOrderFromOpportunityRequest request = new GenerateSalesOrderFromOpportunityRequest();
request.OpportunityId = opportunityId;
//這些欄位將會轉換
request.ColumnSet = new Microsoft.Xrm.Sdk.Query.ColumnSet("name", "customerid");
GenerateSalesOrderFromOpportunityResponse response = (GenerateSalesOrderFromOpportunityResponse)service.Execute(request);
//銷售訂單
Entity saloorderEn = response.Entity;
}
/// <summary>
/// 商機產生發票
/// </summary>
/// <param name="service">服務</param>
public void ToInvoiceFrom(IOrganizationService service)
{
GenerateInvoiceFromOpportunityRequest request = new GenerateInvoiceFromOpportunityRequest();
request.OpportunityId = opportunityId;
//這些欄位將會轉換
request.ColumnSet = new Microsoft.Xrm.Sdk.Query.ColumnSet("name", "customerid");
GenerateInvoiceFromOpportunityResponse response = (GenerateInvoiceFromOpportunityResponse)service.Execute(request);
//發票
Entity invoiceFromEn = response.Entity;
}
/// <summary>
/// 為目標值中指定的實體擷取產品的數量小數值
/// </summary>
/// <param name="service">服務</param>
/// <param name="productId">產品id</param>
/// <param name="uoMId">單位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>
/// 檢索指定安全性主體(使用者或團隊)對商機擁有的存取權限
/// </summary>
/// <param name="service">服務</param>
/// <param name="principal">使用者或團隊</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>
/// 檢索有權訪問商機的安全性主體(使用者或團隊)及其對商機所擁有的存取權限
/// </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)
{
//存取權限
AccessRights accessRights = pa.AccessMask;
//使用者或者團隊的引用
EntityReference Principal = pa.Principal;
}
}
}
/// <summary>
/// 移除指定安全性主體(使用者或團隊)對商機擁有的所有存取權限。
/// </summary>
/// <param name="service">服務</param>
/// <param name="revokee">使用者或者團隊</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>
/// 刪除商機
/// </summary>
public void Delete(IOrganizationService service)
{
service.Delete(entityName, opportunityId);
}
}
crm操作商機實體