简体   繁体   English

MS CRM 4.0 CrmService - 关闭任务

[英]MS CRM 4.0 CrmService - Close a Task

I'm trying to work out how to use the CrmService to close a Task in MS CRM 4.0我正在尝试解决如何使用 CrmService 关闭 MS CRM 4.0 中的任务

I've tried to use the SetStateTaskRequest to set a Task's state and status to TaskState.Completed and 5. I also tried TaskState.Completed and -1, but no dice there either.我尝试使用SetStateTaskRequest将任务的状态和状态设置为 TaskState.Completed 和 5。我也尝试过 TaskState.Completed 和 -1,但也没有骰子。

Either way, I only receive the ever-helpful "Server was unable to process request" exception on the CrmService.Execute attempt.无论哪种方式,我只在 CrmService.Execute 尝试中收到有用的“服务器无法处理请求”异常。

I can create and update Tasks as freely as I please.我可以随意创建和更新任务。 But I can't seem to set them to completed.但我似乎无法将它们设置为完成。 It's frustrating.这令人沮丧。

I noticed that I can only set the state of a Task to Completed in CRM through the Close Task action.我注意到我只能通过关闭任务操作在 CRM 中将任务的状态设置为已完成。 I was wondering if there is a separate CrmService call that I need to make to perform the Close Task action, rather than going through the CrmService.Execute method.我想知道是否需要单独调用 CrmService 来执行关闭任务操作,而不是通过 CrmService.Execute 方法。

Oh: I'm logging into the CrmService with full permissions.哦:我正在以完全权限登录 CrmService。 So I can't see that it would be a permissions issue on the task item.所以我看不出这是任务项目的权限问题。

I can't think what else could be causing this issue.我想不出还有什么可能导致这个问题。 Any advice or even just a point in the right direction would be very much appreciated.任何建议,甚至只是正确方向的一点,都将不胜感激。

FIRST EDIT:第一次编辑:

Thanks to grega g 's answer for getting me to check the Detail field of the exception.感谢grega g回答让我检查异常的 Detail 字段。

I now have a more detailed exception message.我现在有一个更详细的异常消息。 In XML Form:以 XML 形式:

<error>
    <code>0x80040203</code>
    <description>Invalid format of input XML for request SetStateTask: required field 'EntityId' is missing.</description>
    <type>Platform</type>
</error>

Which is bizarre - consider my code (almost identical to greg g's:这很奇怪 - 考虑我的代码(几乎与 greg g 相同:

SetStateTaskRequest request = new SetStateTaskRequest();
request.EntityID = gTaskId;
request.TaskState = TaskState.Completed;

// ETaskStatusCode is an enumeration of the status codes taken from the StringMap in CRM.
//
// ETaskStatusCode.Completed = 5 - I can confirm this is the accurate status value for a Closed Task.
//
// Also, I have attempted this code with -1, which the documentation claims should cause the status
// to automatically be set to the default status for the supplied state. No change there.
request.TaskStatus = (int)ETaskStatusCode.Completed;            

SetStateTaskResponse response = CRMManager.CrmService.Execute(request) as SetStateTaskResponse;

Also, just to confirm that I have the right status code (and also share something I've found very useful when dealing with MS CRM), here's the SQL I use to determine the values for entity statuses.另外,为了确认我有正确的状态代码(并且还分享了我在处理 MS CRM 时发现非常有用的东西),这是我用来确定实体状态值的 SQL。

SELECT
    MSE.ObjectTypeCode,
    MSE.PhysicalName,
    SM.AttributeName,
    SM.Value,
    SM.AttributeValue
FROM MetadataSchema.Entity MSE
    INNER JOIN StringMap SM on MSE.ObjectTypeCode = SM.ObjectTypeCode
ORDER BY MSE.PhysicalName, SM.AttributeName, SM.AttributeValue  

I can confirm from the MS CRM web interface that the Status value that is associated with a Completed task is also named Completed.我可以从 MS CRM Web 界面确认与已完成任务关联的状态值也被命名为已完成。 I can confirm from the SQL above that the value of this status, for a Task, is 5 - this is the value passed in from my Enum.我可以从上面的 SQL 中确认,对于任务,此状态的值是 5 - 这是从我的 Enum 传入的值。

I can also confirm that gTaskId is being set to a valid Guid that references a Task that actually does exist, and is open at the time the close is attempted.我还可以确认 gTaskId 被设置为引用一个实际存在的任务的有效 Guid,并且在尝试关闭时打开。

Curiouser and curiouser.越来越好奇。 Any thoughts?有什么想法吗?

Use SetStateTaskRequest class.使用SetStateTaskRequest类。

SetStateTaskRequest task2Close = new SetStateTaskRequest();
task2Close.EntityId = <taskGuid>
task2Close.TaskState = TaskState.Completed;
task2Close.TaskStatus = <some value>

try
{
    SetStateTaskResponse r = (SetStateTaskResponse) crmSvc.Execute(task2Close);
}
catch (SoapException e)
{
   //Use e.Details for more info than "server was unable ..."
}

This code should work (or let you see why error occurs)这段代码应该可以工作(或者让你看看为什么会发生错误)

Are you sure that when you are trying to close task you're passing status value which is valid for Completed state ?您确定当您尝试关闭任务时,您正在传递对 Completed状态有效的状态值吗? Different status codes are only valid with their corresponding state codes.不同的状态码只有对应的状态码才有效。 Can you add your source code and a portion of your state entity customization?你能添加你的源代码和你的状态实体定制的一部分吗?

Found it!找到了!

Okay - reviewing my code above and the error message closely, my CrmService contained the property EntityID - but the exception was that the property EntityId was missing.好的 - 仔细查看我上面的代码和错误消息,我的 CrmService 包含属性 EntityID - 但例外是缺少属性 EntityId。

Somehow , my CrmService had its EntityId property renamed to EntityID.不知何故,我的 CrmService 将其 EntityId 属性重命名为 EntityID。

Renaming the property fixed the problem.重命名属性解决了这个问题。 I still have no idea how that happened in the first place.我仍然不知道这最初是怎么发生的。

To be safe, I'll regenerate a new Service proxy to make sure that my properties are correctly named.为安全起见,我将重新生成一个新的服务代理以确保我的属性正确命名。

Looking through the code, it seems that someone did a find-and-replace on 'Id' and turned it into 'ID' - which incidentally is the naming convention in my workplace for Property fields that represent primary keys.查看代码,似乎有人对“Id”进行了查找和替换并将其转换为“ID”——顺便说一下,这是我工作场所中代表主键的 Property 字段的命名约定。

Thanks again to grega g for pointing out that the Detail property had the extra information I needed.再次感谢 grega g 指出 Detail 属性包含我需要的额外信息。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM