1. 程式人生 > >crm操作報價單實體

crm操作報價單實體

() log ask public acc lose tracking sof uid

using System;
using Microsoft.Xrm.Sdk;
using Microsoft.Crm.Sdk.Messages;
using Microsoft.Xrm.Sdk.Query;

/// <summary>
/// 報價單
/// </summary>
public class QuoteHelper
{
public static readonly string entityName = "quote";
public Guid quoteId = Guid.Empty;
public IOrganizationService service;

/// <summary>
/// 創建報價單
/// </summary>
public void Create(Guid accountId)
{
Entity en = new Entity() { LogicalName = entityName };
en["name"] = "報價單測試";
en["accountid"] = new EntityReference() { LogicalName = "account",Id = accountId };
quoteId = service.Create(en);
}

/// <summary>
/// 創建報價單及其相關報價單具體信息
/// </summary>
/// <param name="childEn">報價單具體信息</param>
public void CompoundCreate(EntityCollection childEn)
{
CompoundCreateRequest request = new CompoundCreateRequest();
request.Entity = new Entity() { LogicalName = entityName };
request.ChildEntities = childEn;
CompoundCreateResponse response = (CompoundCreateResponse)service.Execute(request);
quoteId = response.Id;
}

/// <summary>
/// 將報價單分派給其它用戶或團隊
/// </summary>
/// <param name="assignee">用戶或團隊引用</param>
public void Assign(EntityReference assignee)
{
AssignRequest request = new AssignRequest();
request.Target = new EntityReference() { LogicalName = entityName,Id = quoteId };
request.Assignee = assignee;
AssignResponse response = (AssignResponse)service.Execute(request);
}

/// <summary>
/// 指定結束報價單
/// </summary>
/// <param name="status">結束的狀態值</param>
public void CloseQuote(int status)
{
CloseQuoteRequest request = new CloseQuoteRequest();
request.QuoteClose = new Entity() { LogicalName = entityName,Id = quoteId };
request.Status = new OptionSetValue(status);
CloseQuoteResponse response = (CloseQuoteResponse)service.Execute(request);
}

/// <summary>
/// 將報價單轉換為銷售訂單
/// </summary>
public Entity ConvertQuoteToSalesOrder()
{
ConvertQuoteToSalesOrderRequest request = new ConvertQuoteToSalesOrderRequest();
request.QuoteId = quoteId;
request.ColumnSet = new Microsoft.Xrm.Sdk.Query.ColumnSet("name","accountid");
ConvertQuoteToSalesOrderResponse response = (ConvertQuoteToSalesOrderResponse)service.Execute(request);
//銷售訂單
Entity solrorderEn = response.Entity;
return solrorderEn;
}

/// <summary>
/// 為目標值中指定的實體獲取產品的數量小數值
/// </summary>
/// <param name="productId">產品id</param>
/// <param name="uoMId">單位id</param>
public int GetQuantityDecimal(Guid productId, Guid uoMId)
{
GetQuantityDecimalRequest request = new GetQuantityDecimalRequest();
request.Target = new EntityReference() { LogicalName = entityName,Id = quoteId };
request.ProductId = productId;
request.UoMId = uoMId;
GetQuantityDecimalResponse response = (GetQuantityDecimalResponse)service.Execute(request);
return response.Quantity;
}

/// <summary>
/// 從商機中檢索產品並將其拷貝到報價單
/// </summary>
/// <param name="opportunityId">商機id</param>
public void GetQuoteProductsFromOpportunity(Guid opportunityId)
{
GetQuoteProductsFromOpportunityRequest request = new GetQuoteProductsFromOpportunityRequest();
request.OpportunityId = opportunityId;
request.QuoteId = quoteId;
GetQuoteProductsFromOpportunityResponse response =
(GetQuoteProductsFromOpportunityResponse)service.Execute(request);
}

/// <summary>
/// 與其它安全主體(用戶或團隊)共享報價單
/// </summary>
/// <param name="accessMask">訪問權限</param>
/// <param name="principal">用戶或團隊引用</param>
public void GrantAccessRequest(AccessRights accessMask,EntityReference principal)
{
GrantAccessRequest request = new GrantAccessRequest();
request.Target = new EntityReference() { LogicalName = entityName,Id = quoteId };
request.PrincipalAccess = new PrincipalAccess() { AccessMask = accessMask,Principal = principal };
GrantAccessResponse response = (GrantAccessResponse)service.Execute(request);
}

/// <summary>
/// 將報價單的狀態設置為“草稿”
/// </summary>
public void ReviseQuote()
{
ReviseQuoteRequest request = new ReviseQuoteRequest();
request.QuoteId = quoteId;
request.ColumnSet = new Microsoft.Xrm.Sdk.Query.ColumnSet("name","accountid");
ReviseQuoteResponse response = (ReviseQuoteResponse)service.Execute(request);
//草稿狀態的報價單
Entity quoteEn = response.Entity;
}

/// <summary>
/// 匯總或檢索與指定的記錄(客戶或聯系人)相關的全部報價單
/// </summary>
/// <param name="target">客戶或聯系人引用</param>
/// <param name="query">查詢條件</param>
/// <param name="rollupType">關聯類型</param>
public void Rollup(EntityReference target, QueryBase query, RollupType rollupType)
{
RollupRequest request = new RollupRequest();
request.Target = target;
request.Query = query;
request.RollupType = rollupType;
RollupResponse response = (RollupResponse)service.Execute(request);
EntityCollection entityCollection = response.EntityCollection;
}

/// <summary>
/// 刪除報價單
/// </summary>
public void Delete()
{
service.Delete(entityName, quoteId);
}

}

crm操作報價單實體