首页 > 代码库 > 4、CRM2011编程实战——将窗体中指定控件的值做处理后更新到另一个字段中

4、CRM2011编程实战——将窗体中指定控件的值做处理后更新到另一个字段中

需求:将接报时间加上到期提醒时间后得到的值,更新到字段“到期截止时间”

Js调用:

//设置到期截止时间
    function setDeadLine(){
    var recordId = Xrm.Page.data.entity.getId();
    var entityName = Xrm.Page.data.entity.getEntityName();
    var reportedTime = Xrm.Page.getControl("hxcs_fdatetimeofrequesthelp").getAttribute().getValue().toLocaleString();
    var remindTime =Xrm.Page.getControl("hxcs_fremindtime").getAttribute().getText();
    $.post("/isv/Handlers/SetRemindHandler.ashx", { "recordId":recordId,"entityName":entityName,"reportedTime": reportedTime,"remindTime":remindTime}, function(data){});
}

一般处理程序SetRemindHandler.ashx,代码如下:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using Microsoft.Xrm.Sdk;
using Microsoft.Xrm.Sdk.Client;
using System.Net;
using System.ServiceModel.Description;
using System.Configuration;

namespace IsWaterWeb
{
    /// <summary>
    /// SetRemindHandler 的摘要说明
    /// </summary>
    public class SetRemindHandler : IHttpHandler
    {
        IOrganizationService _service = null;
        public void ProcessRequest(HttpContext context)
        {
            context.Response.ContentType = "text/plain";
            string recordId=context.Request["recordId"];
            string entityName=context.Request["entityName"];
            string reportedTime = context.Request["reportedTime"]; //接报时间
            string remindTime = context.Request["remindTime"]; //到期提醒时间(h)

            if(!string.IsNullOrEmpty(reportedTime)&&!string.IsNullOrEmpty(remindTime))
            {
                DateTime rTime = Convert.ToDateTime(reportedTime);
                double hourCounts = Convert.ToDouble(remindTime);
                DateTime deadline = rTime.AddHours(hourCounts);

                _service = GetOrganization();
                Entity entity = new Entity();
                entity.Id=Guid.Parse(recordId);
                entity.LogicalName=entityName;
                entity.Attributes["hxcs_fdeadline"] = deadline;
                _service.Update(entity);
            }
        }

        public IOrganizationService GetOrganization()
        {
            String UserName = ConfigurationManager.AppSettings["LoginName"];
            String Password = ConfigurationManager.AppSettings["LoginPwd"];
            string url=ConfigurationManager.AppSettings["organizationServiceUrl"];
            Uri uri = new Uri(url);

            var cred = new ClientCredentials();
            //cred.Windows.ClientCredential = new NetworkCredential("Xiaozhou", "Zhou123.");
            cred.Windows.ClientCredential = new NetworkCredential(UserName, Password);

            OrganizationServiceProxy _proxy = new OrganizationServiceProxy(uri, null, cred, null);
            IOrganizationService _service = (IOrganizationService)_proxy;
            return _service;
        }
        public bool IsReusable
        {
            get
            {
                return false;
            }
        }
    }
}

注意:记得将一般处理程序SetRemindHandler.ashx放到服务器目录:C:\Program Files\Microsoft Dynamics CRM\CRMWeb\ISV\Handlers,如果没有Handlers目录,可以新建。