crm操作货币实体

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

    /// <summary>
    /// 货币
    /// </summary>
    public class TransactionCurrencyHelper
    {
        public static readonly string entityName = "transactioncurrency";
        public Guid transactionCurrencyId = Guid.Empty;

        /// <summary>
        /// 创建货币
        /// </summary>
        /// <param name="service">服务</param>
        public void Create(IOrganizationService service)
        {
            Entity en = new Entity() { LogicalName = entityName };
            //货币代码
            en["isocurrencycode"] = "CNY";
            //货币名称
            en["currencyname"] = "人命币";
            //货币精度
            en["currencyprecision"] = 2;
            //货币符合
            en["currencysymbol"] = "¥";
            //换算比率
            en["exchangerate"] = 1.0;

            transactionCurrencyId = service.Create(en);
        }

        /// <summary>
        /// 改动货币
        /// </summary>
        /// <param name="service">服务</param>
        public void Update(IOrganizationService service)
        {
            Entity en = new Entity() { LogicalName = entityName,Id = transactionCurrencyId };
            //货币名称
            en["currencyname"] = "人命币-2000";

            service.Update(en);
        }

        /// <summary>
        /// 检索汇率
        /// </summary>
        /// <param name="service">服务</param>
        public decimal SearchRateById(IOrganizationService service)
        {
            decimal value = 0;
            RetrieveExchangeRateRequest request = new RetrieveExchangeRateRequest();
            request.TransactionCurrencyId = transactionCurrencyId;
            RetrieveExchangeRateResponse response = (RetrieveExchangeRateResponse)service.Execute(request);
            value = response.ExchangeRate;
            return value; 
        }

        /// <summary>
        /// 停用和启用货币
        /// </summary>
        public void UpdateTransactionCurrencyState(IOrganizationService service)
        {
            //停用货币
            UpdateState(entityName, transactionCurrencyId, 1, 2, service);
            //启用货币
            UpdateState(entityName, transactionCurrencyId, 0, 1, service);
        }

        private void UpdateState(string enName, Guid id, int state, int status, IOrganizationService service)
        {
            SetStateRequest setState = new SetStateRequest()
            {
                EntityMoniker = new EntityReference()
                {
                    Id = id,
                    LogicalName = enName
                },
                State = new OptionSetValue(state),
                Status = new OptionSetValue(status)
            };
            service.Execute(setState);
        }

        /// <summary>
        /// 删除货币
        /// </summary>
        /// <param name="service">服务</param>
        public void Delete(IOrganizationService service)
        {
            service.Delete(entityName, transactionCurrencyId);
        }
    }

posted @ 2016-04-06 21:14  mfrbuaa  阅读(277)  评论(0编辑  收藏  举报