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工作机会实体的更多相关文章

  1. HTPC家庭娱乐和XBOX未来发展畅想<另:创业工作机会>

    微软中国在上海举办新闻发布会,正式宣布Xbox One将于9月23日在中国开始销售,定价3699元起.这款早在2001年就发布的电视游戏机终于在经历了14年的等待后,进军中国大陆市场.此次Xbox O ...

  2. Microsoft Dynamics CRM 2011 新建实体 需要注意的细节

    新建一个实体,需要红色框内的是否勾选的意义,可以进一步加深对CRM的理解.如图: 下面对部分的进行了自我的理解,不对的地方,还请大家指出来.互相学习. 1.CRM2011中,在活动方面加强的新特性包括 ...

  3. Dynamics CRM EntityCollection 根据实体中的某个字段为依据去除重复数据

    CRM中通过QueryExpression查询出了一个EntityCollection集,但有时会存在重复数据,QueryExpression中有个属性distinct,只要设置为true就能过滤 ...

  4. crm操作报价单实体

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

  5. crm操作货币实体

    using System;     using Microsoft.Xrm.Sdk;     using Microsoft.Crm.Sdk.Messages; /// <summary> ...

  6. Dynamics CRM 修改自定义实体名字及属性前缀(架构名称)

    本文章以2013为例(2011同理,4.0比较简单直接在系统设置的自定义中设置即可). 1.设置-->自定义项-->发布者,下图中的红框内的即为需要设置的前缀. 2.设置-->解决方 ...

  7. crm操作产品实体

    using System;     using Microsoft.Xrm.Sdk;     using Microsoft.Crm.Sdk.Messages; /// <summary> ...

  8. crm操作约会实体

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

  9. crm操作发票实体

    using System;     using Microsoft.Xrm.Sdk;     using Microsoft.Xrm.Sdk.Query;     using Microsoft.Cr ...

随机推荐

  1. 让MFC程序隐藏运行界面

    在MFC中隐藏运行界面确实花花点功力. 针对对话框程序,一种不是很好地实现方法是在OnPaint函数中添加如下代码: CWnd::ShowWindow(SW_HIDE); 添加后执行会发现屏幕会闪烁一 ...

  2. HTML5动态分页效果代码

    <!DOCTYPE html> <html> <head> <meta charset="UTF-8"> <title> ...

  3. 漫谈并发编程(六):java中一些经常使用的并发构件的介绍

    CountDownLatch      它被用来同步一个或多个任务,强制它们等待其他任务运行的一组操作完毕.      你能够向CountDownLatch对象设置一个初始计数值,不论什么在这个对象上 ...

  4. 可能性dp+减少国家HDU4336

    Card Collector Time Limit:1000MS     Memory Limit:32768KB     64bit IO Format:%I64d & %I64u Subm ...

  5. LIS小结(O(∩_∩)O~哄哄)

    ~\(≧▽≦)/~啦啦啦,昨天说的是LCS,今天我们要学习的是LIS,什么是LIS呢?  LIS: 最长有序子序列(递增/递减/非递增/非递减)这么说还是有些模糊,举个例子: 在一个无序的序列a1,a ...

  6. Oracle dump 分析secondary key

    验证secondary key 含有主键列数据 SQL> select object_name,object_id,OBJECT_TYPE from user_objects; OBJECT_N ...

  7. spring开发基础

    Spring是一个开源框架,它由Rod Johnson创建.它是为了解决企业应用开发的复杂性而创建的.Spring使用基本的JavaBean来完成以前只可能由EJB完成的事情.然而,Spring的用途 ...

  8. java学习笔记-Thread

    java定义了创建线程的两种方法 1.实现Runnable接口 2.扩展Thread类本身 1.实现Runnable接口: 可以依托任何Runnable接口的对象来创建线程.但是为了实现Runnabl ...

  9. Dnasp计算LD

    Dnasp计算LD Table of Contents 1 Dnasp 计算LD 1 Dnasp 计算LD Dnasp有很多的功能,现在主要来记录其计算LD的功能. 首先File——然后打开data— ...

  10. 使用AjaxFileUpload.js实现文件异步上�

    ajax是无法提交文件的,所以在上传图片并预览的时候,我们常常使用Ifame的方法实现看似异步的效果.可是这样总不是非常方便的,AjaxFilleUpload.js对上面的方法进行了一个包装,使得我们 ...