使用插件更新状态和属性值

时间:2014-11-05 20:25:42

标签: crm

我正在尝试通过为CRM 2013编写插件来更新实体属性的状态和值。我尝试使用setStateRequest设置活动的状态,但我不确定是否可以将属性值更新为好。我在Merge消息上注册了插件以更改活动的状态。如何更新属性值以及状态更改?这是我目前为状态ch的代码



 protected void ExecutePreCaseMerge(LocalPluginContext localContext)
        {
            if (localContext == null)
            {
                throw new ArgumentNullException("localContext");
            }

            // TODO: Implement your custom Plug-in business logic.

            IPluginExecutionContext context = localContext.PluginExecutionContext;
            IOrganizationService service = localContext.OrganizationService;

            //The InputParameters collection contains all the data passed in the message request.
            if (context.InputParameters.Contains("SubordinateId") &&
                context.InputParameters["SubordinateId"] is Guid)
            {
                try
                {
                    Guid subordinateId = (Guid)context.InputParameters["SubordinateId"];

                    var fetch = @"<fetch version='1.0' output-format='xml-platform' mapping='logical' distinct='false'>
                                  <entity name='task'>
                                    <attribute name='new_issuephase' />
                                    <filter type='and'>
                                      <filter type='and'>
                                        <condition attribute='regardingobjectid' operator='eq' uitype='incident' value='" + subordinateId + @"' />
                                        <condition attribute='statecode' operator='eq' value='0' />
                                      </filter>
                                    </filter>
                                  </entity>
                                </fetch>";

                    EntityCollection ec = service.RetrieveMultiple(new FetchExpression(fetch));
                  

                    if (ec.Entities.Count > 0)
                    {
                       
                        // Create an ExecuteMultipleRequest object.
                        ExecuteMultipleRequest requestWithResults = new ExecuteMultipleRequest()
                        {
                            // Assign settings that define execution behavior: continue on error, return responses. 
                            Settings = new ExecuteMultipleSettings()
                            {
                                ContinueOnError = false,
                                ReturnResponses = true
                            },
                            // Create an empty organization request collection.
                            Requests = new OrganizationRequestCollection()
                        };

                        foreach (var item in ec.Entities)
                        {

                            SetStateRequest setStateRequest = new SetStateRequest();
                            setStateRequest.EntityMoniker = new EntityReference("task", item.Id);
                            setStateRequest.State = new OptionSetValue(2);
                            setStateRequest.Status = new OptionSetValue(6);

                            requestWithResults.Requests.Add(setStateRequest);

                        }

                        ExecuteMultipleResponse responseWithResults =
                            (ExecuteMultipleResponse)service.Execute(requestWithResults);
                    }
                }
&#13;
&#13;
&#13;

安格。

感谢您的帮助!

1 个答案:

答案 0 :(得分:1)

您必须将更新作为单独的请求进行,但您可以与同一ExecuteMultipleRequest中的SetStateRequests一起执行:

foreach (var item in ec.Entities)
{

    SetStateRequest setStateRequest = new SetStateRequest();
    setStateRequest.EntityMoniker = new EntityReference("task", item.Id);
    setStateRequest.State = new OptionSetValue(2);
    setStateRequest.Status = new OptionSetValue(6);

    requestWithResults.Requests.Add(setStateRequest);

    //New Code
    item.Attributes["attributetobeupdated"] = "Updated Value";
    UpdateRequest request = new UpdateRequest() { Target = item };
    requestWithResults.Requests.Add(request);

}

ExecuteMultipleResponse responseWithResults =
    (ExecuteMultipleResponse)service.Execute(requestWithResults);

请注意,如果任何请求失败,ExecuteMultipleRequest将不会抛出错误,但会返回ExecuteMultipleResponse中各个失败的详细信息。似乎此操作失败的常见原因是默认情况下SQL服务器查询配置为在30秒后超时。更多信息:

http://manyrootsofallevilrants.blogspot.in/2012/09/ms-crm-2011-timeout-settings-and-limits.html