Dynamics CRM 2011编程系列(1):系统实体状态更新

   我们在更改系统实体的状态时,不能直接update实体的statecode,需要引用Execute来执行请求,
然后更新实体的状态,在此举例商机的赢单,分别用插件Plguin和SOAP的方式执行。
查询SDK,赢单需要用WinOpportunityRequest请求,插件形式更新代码如下:
Entity target = new Entity() { LogicalName = "opportunityclose" };
target["opportunityid"] = new EntityReference() 
{ LogicalName = "opportunity", Id = entity.Id };
WinOpportunityRequest request = new WinOpportunityRequest();
request.OpportunityClose = target;
request.Status = new OptionSetValue(3);
service.Execute(request);
   更新商机为赢单后,查看商机的状态,导航栏保存等按钮即变为灰色,页面无法进行更改操作,
即显示该商机已结束。
   SOAP方式更新代码,可以采用SDK现有的工具SOAPLogger生成报文,该工具打包在Dynamics 
CRM 2011 SDK中的\samplecode\cs\client(具体报文工具使用情况请看下一篇: Dynamics 
CRM 2011编程系列(2):使用SOAPLogger抓取报文),客户端添加按钮后调用方法更新商机。
SOAP报文如下:
    function ChangeStatus(entityName, entityId, stateCode, statuscode) {
    // create the request
    var request = "http://schemas.xmlsoap.org/soap/envelope/\">";
    request += "";
    request += "http://schemas.microsoft.com/xrm/2011/Contracts/Services\" xmlns:i=\"http://www.w3.org/2001/XMLSchema-instance\">";
    request += "b:WinOpportunityRequest\" xmlns:a=\"http://schemas.microsoft.com/xrm/2011/Contracts\" xmlns:b=\"http://schemas.microsoft.com/crm/2011/Contracts\">";
    request += "http://schemas.datacontract.org/2004/07/System.Collections.Generic\">";
    request += "";
    request += "OpportunityClose";
    request += "a:Entity\">";
    request += "" + entityId + "";
    request += "opportunity";
    request += "true\" />";
    request += "";
    request += "";
    request += "";
    request += "State";
    request += "a:OptionSetValue\">";
    request += "" + stateCode + "";
    request += "";
    request += "";
    request += "";
    request += "Status";
    request += "a:OptionSetValue\">";
    request += "" + statuscode + "";
    request += "";
    request += "";
    request += "";
    request += "true\" />";
    request += "SetState";
    request += "";
    request += "";
    request += "";
    request += "";

    //send set state request
    $.ajax({
        async: false,
        type: "POST",
        contentType: "text/xml; charset=utf-8",
        datatype: "xml",
        url: Xrm.Page.context.getServerUrl() + "/XRMServices/2011/Organization.svc/web",
        data: request,
        beforeSend: function (XMLHttpRequest) {
            XMLHttpRequest.setRequestHeader("Accept", "application/xml, text/xml, */*");
            XMLHttpRequest.setRequestHeader("SOAPAction", "http://schemas.microsoft.com/xrm/2011/Contracts/Services/IOrganizationService/Execute");
        },
        success: function (data, textStatus, XmlHttpRequest) {
            alert("The Entity Status has been changed");
        },
        error: function (XMLHttpRequest, textStatus, errorThrown) {
            alert(errorThrown);
        }
    });

}

你可能感兴趣的:(Dynamics CRM 2011编程系列(1):系统实体状态更新)