Opportunity Helper
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); } }