您好,谁能在这里给我解决方案,我在Dynamic Crm中创建自定义操作,添加输入和输出参数,因此,当输入发送时,它将过滤具有该属性的某些实体并在我的输出参数中返回Entity Collection 。但是问题在于,它将始终返回状态为500 Internal Server Error的错误,这是我收到的完整错误
{
"error": {
"code": "0x80040216",
"message": "System.Runtime.Serialization.SerializationException: Element 'schemas.microsoft.com/.../Contracts:Entity' contains data from a type that maps to the name 'CrmEarlyBound:BookableResourceBooking'. The deserializer has no knowledge of any type that maps to this name. Consider changing the implementation of the ResolveName method on your DataContractResolver to return a non-null value for name 'BookableResourceBooking' and namespace 'CrmEarlyBound'.\r\n at System.Runtime.Serialization.XmlObjectSerializerReadContext.InternalDeserialize(XmlReaderDelegator reader, String name, String ns, Type declaredType, DataContract& dataContract)\r\n at System.Runtime.Serialization.XmlObjectSerializerReadContext.InternalDeserialize(XmlReaderDelegator xmlReader, Int32 id, RuntimeTypeHandle declaredTypeHandle, String name, String ns)\r\n at ReadArrayOfEntityFromXml(XmlReaderDelegator , XmlObjectSerializerReadContext , XmlDictionaryString , XmlDictionaryString , CollectionDataContract )\r\n at System.Runtime.Serialization.CollectionDataContract.ReadXmlValue(XmlReaderDelegator xmlReader, XmlObjectSerializerReadContext context)\r\n at System.Runtime.Serialization.XmlObjectSerializerReadContext.InternalDeserialize(XmlReaderDelegator reader, String name, String ns, Type declaredType, DataContract& dataContract)\r\n at System.Runtime.Serialization.XmlObjectSerializerReadContext.InternalDeserialize(XmlReaderDelegator xmlReader, Int32 id, RuntimeTypeHandle declaredTypeHandle, String name, String ns)\r\n at ReadEntityCollectionFromXml(XmlReaderDelegator , XmlObjectSerializerReadContext , XmlDictionaryString[] , XmlDictionaryString[] )\r\n at System.Runtime.Serialization.ClassDataContract.ReadXmlValue(XmlReaderDelegator xmlReader, XmlObjectSerializerReadContext context)\r\n at System.Runtime.Serialization.XmlObjectSerializerReadContext.InternalDeserialize(XmlReaderDelegator reader, String name, String ns, Type declaredType, DataContract& dataContract)\r\n at System.Runtime.Serialization.XmlObjectSerializerReadContext.InternalDeserialize(XmlReaderDelegator xmlReader, Int32 id, RuntimeTypeHandle declaredTypeHandle, String name, String ns)\r\n at ReadKeyValuePairOfstringanyTypeFromXml(XmlReaderDelegator , XmlObjectSerializerReadContext , XmlDictionaryString[] , XmlDictionaryString[] )\r\n at System.Runtime.Serialization.ClassDataContract.ReadXmlValue(XmlReaderDelegator xmlReader, XmlObjectSerializerReadContext context)\r\n at System.Runtime.Serialization.XmlObjectSerializerReadContext.InternalDeserialize(XmlReaderDelegator reader, String name, String ns, Type declaredType, DataContract& dataContract)\r\n at System.Runtime.Serialization.XmlObjectSerializerReadContext.InternalDeserialize(XmlReaderDelegator xmlReader, Int32 id, RuntimeTypeHandle declaredTypeHandle, String name, String ns)\r\n at ReadParameterCollectionFromXml(XmlReaderDelegator , XmlObjectSerializerReadContext , XmlDictionaryString , XmlDictionaryString , CollectionDataContract )\r\n at System.Runtime.Serialization.CollectionDataContract.ReadXmlValue(XmlReaderDelegator xmlReader, XmlObjectSerializerReadContext context)\r\n at System.Runtime.Serialization.XmlObjectSerializerReadContext.InternalDeserialize(XmlReaderDelegator reader, String name, String ns, Type declaredType, DataContract& dataContract)\r\n at System.Runtime.Serialization.XmlObjectSerializerReadContext.InternalDeserialize(XmlReaderDelegator xmlReader, Type declaredType, DataContract dataContract, String name, String ns)\r\n at System.Runtime.Serialization.DataContractSerializer.InternalReadObject(XmlReaderDelegator xmlReader, Boolean verifyObjectName, DataContractResolver dataContractResolver)\r\n at System.Runtime.Serialization.XmlObjectSerializer.ReadObjectHandleExceptions(XmlReaderDelegator reader, Boolean verifyObjectName, DataContractResolver dataContractResolver)\r\n at System.Runtime.Serialization.XmlObjectSerializer.ReadObject(XmlDictionaryReader reader)\r\n at Microsoft.Crm.Sandbox.SandboxUtility.DeserializeDataContract[T](Byte[] serializedDataContract, Assembly proxyTypesAssembly)\r\n at Microsoft.Crm.Sandbox.SandboxExecutionContext.Merge(IExecutionContext originalContext)\r\n at Microsoft.Crm.Sandbox.SandboxCodeUnit.ExecuteInternal(SandboxClient client, IExecutionContext context, SandboxTraceContext sandboxTraceContext, SandboxCallTracker callTracker, Guid parentExecutionId, String assemblyContents, Boolean& isSafeToRetry, Boolean& executeDone)\r\n at Microsoft.Crm.Sandbox.SandboxCodeUnit.<>c__DisplayClass24_0.<Execute>b__0(): Microsoft Dynamics CRM has experienced an error. Reference number for administrators or support: #BBF0AE79",
"innererror": {
"message": "System.Runtime.Serialization.SerializationException: Element 'schemas.microsoft.com/.../Contracts:Entity' contains data from a type that maps to the name 'CrmEarlyBound:BookableResourceBooking'. The deserializer has no knowledge of any type that maps to this name. Consider changing the implementation of the ResolveName method on your DataContractResolver to return a non-null value for name 'BookableResourceBooking' and namespace 'CrmEarlyBound'.\r\n at System.Runtime.Serialization.XmlObjectSerializerReadContext.InternalDeserialize(XmlReaderDelegator reader, String name, String ns, Type declaredType, DataContract& dataContract)\r\n at System.Runtime.Serialization.XmlObjectSerializerReadContext.InternalDeserialize(XmlReaderDelegator xmlReader, Int32 id, RuntimeTypeHandle declaredTypeHandle, String name, String ns)\r\n at ReadArrayOfEntityFromXml(XmlReaderDelegator , XmlObjectSerializerReadContext , XmlDictionaryString , XmlDictionaryString , CollectionDataContract )\r\n at System.Runtime.Serialization.CollectionDataContract.ReadXmlValue(XmlReaderDelegator xmlReader, XmlObjectSerializerReadContext context)\r\n at System.Runtime.Serialization.XmlObjectSerializerReadContext.InternalDeserialize(XmlReaderDelegator reader, String name, String ns, Type declaredType, DataContract& dataContract)\r\n at System.Runtime.Serialization.XmlObjectSerializerReadContext.InternalDeserialize(XmlReaderDelegator xmlReader, Int32 id, RuntimeTypeHandle declaredTypeHandle, String name, String ns)\r\n at ReadEntityCollectionFromXml(XmlReaderDelegator , XmlObjectSerializerReadContext , XmlDictionaryString[] , XmlDictionaryString[] )\r\n at System.Runtime.Serialization.ClassDataContract.ReadXmlValue(XmlReaderDelegator xmlReader, XmlObjectSerializerReadContext context)\r\n at System.Runtime.Serialization.XmlObjectSerializerReadContext.InternalDeserialize(XmlReaderDelegator reader, String name, String ns, Type declaredType, DataContract& dataContract)\r\n at System.Runtime.Serialization.XmlObjectSerializerReadContext.InternalDeserialize(XmlReaderDelegator xmlReader, Int32 id, RuntimeTypeHandle declaredTypeHandle, String name, String ns)\r\n at ReadKeyValuePairOfstringanyTypeFromXml(XmlReaderDelegator , XmlObjectSerializerReadContext , XmlDictionaryString[] , XmlDictionaryString[] )\r\n at System.Runtime.Serialization.ClassDataContract.ReadXmlValue(XmlReaderDelegator xmlReader, XmlObjectSerializerReadContext context)\r\n at System.Runtime.Serialization.XmlObjectSerializerReadContext.InternalDeserialize(XmlReaderDelegator reader, String name, String ns, Type declaredType, DataContract& dataContract)\r\n at System.Runtime.Serialization.XmlObjectSerializerReadContext.InternalDeserialize(XmlReaderDelegator xmlReader, Int32 id, RuntimeTypeHandle declaredTypeHandle, String name, String ns)\r\n at ReadParameterCollectionFromXml(XmlReaderDelegator , XmlObjectSerializerReadContext , XmlDictionaryString , XmlDictionaryString , CollectionDataContract )\r\n at System.Runtime.Serialization.CollectionDataContract.ReadXmlValue(XmlReaderDelegator xmlReader, XmlObjectSerializerReadContext context)\r\n at System.Runtime.Serialization.XmlObjectSerializerReadContext.InternalDeserialize(XmlReaderDelegator reader, String name, String ns, Type declaredType, DataContract& dataContract)\r\n at System.Runtime.Serialization.XmlObjectSerializerReadContext.InternalDeserialize(XmlReaderDelegator xmlReader, Type declaredType, DataContract dataContract, String name, String ns)\r\n at System.Runtime.Serialization.DataContractSerializer.InternalReadObject(XmlReaderDelegator xmlReader, Boolean verifyObjectName, DataContractResolver dataContractResolver)\r\n at System.Runtime.Serialization.XmlObjectSerializer.ReadObjectHandleExceptions(XmlReaderDelegator reader, Boolean verifyObjectName, DataContractResolver dataContractResolver)\r\n at System.Runtime.Serialization.XmlObjectSerializer.ReadObject(XmlDictionaryReader reader)\r\n at Microsoft.Crm.Sandbox.SandboxUtility.DeserializeDataContract[T](Byte[] serializedDataContract, Assembly proxyTypesAssembly)\r\n at Microsoft.Crm.Sandbox.SandboxExecutionContext.Merge(IExecutionContext originalContext)\r\n at Microsoft.Crm.Sandbox.SandboxCodeUnit.ExecuteInternal(SandboxClient client, IExecutionContext context, SandboxTraceContext sandboxTraceContext, SandboxCallTracker callTracker, Guid parentExecutionId, String assemblyContents, Boolean& isSafeToRetry, Boolean& executeDone)\r\n at Microsoft.Crm.Sandbox.SandboxCodeUnit.<>c__DisplayClass24_0.<Execute>b__0(): Microsoft Dynamics CRM has experienced an error. Reference number for administrators or support: #BBF0AE79",
"type": "System.ServiceModel.FaultException`1[[Microsoft.Xrm.Sdk.OrganizationServiceFault, Microsoft.Xrm.Sdk, Version=9.0.0.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35]]",
"stacktrace": " at Microsoft.Crm.Extensibility.OrganizationSdkServiceInternal.Execute(OrganizationRequest request, CorrelationToken correlationToken, CallerOriginToken callerOriginToken, WebServiceType serviceType, Boolean checkAdminMode, ExecutionContext executionContext, Dictionary`2 optionalParameters)\r\n at Microsoft.Crm.Extensibility.OData.CrmODataExecutionContext.Execute(OrganizationRequest request, ExecutionContext executionContext)\r\n at Microsoft.Crm.Extensibility.OData.CrmODataServiceDataProvider.ExecuteOperation(CrmODataExecutionContext context, EdmOperation edmOperation, Dictionary`2 parameters, Dictionary`2 boundParameters)\r\n at Microsoft.Crm.Extensibility.OData.ActionController.ProcessOperationRequest(String operationName, Dictionary`2 operationParameters, EntityReference entityReference, String boundEntityName, String boundEntityType)\r\n at Microsoft.Crm.Extensibility.OData.ActionController.<>c__DisplayClass9_0.<PostUnboundAction>b__0()\r\n at Microsoft.PowerApps.CoreFramework.ActivityLoggerExtensions.Execute[TResult](ILogger logger, EventId eventId, ActivityType activityType, Func`1 func, IEnumerable`1 additionalCustomProperties)\r\n at Microsoft.Xrm.Telemetry.XrmTelemetryExtensions.Execute[TResult](ILogger logger, XrmTelemetryActivityType activityType, Func`1 func)\r\n at lambda_method(Closure , Object , Object[] )\r\n at System.Web.Http.Controllers.ReflectedHttpActionDescriptor.ActionExecutor.<>c__DisplayClass10.<GetExecutor>b__9(Object instance, Object[] methodParameters)\r\n at System.Web.Http.Controllers.ReflectedHttpActionDescriptor.ExecuteAsync(HttpControllerContext controllerContext, IDictionary`2 arguments, CancellationToken cancellationToken)\r\n--- End of stack trace from previous location where exception was thrown ---\r\n at System.Runtime.ExceptionServices.ExceptionDispatchInfo.Throw()\r\n at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task)\r\n at System.Web.Http.Controllers.ApiControllerActionInvoker.<InvokeActionAsyncCore>d__0.MoveNext()\r\n--- End of stack trace from previous location where exception was thrown ---\r\n at System.Runtime.ExceptionServices.ExceptionDispatchInfo.Throw()\r\n at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task)\r\n at System.Web.Http.Controllers.ActionFilterResult.<ExecuteAsync>d__2.MoveNext()\r\n--- End of stack trace from previous location where exception was thrown ---\r\n at System.Runtime.ExceptionServices.ExceptionDispatchInfo.Throw()\r\n at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task)\r\n at System.Web.Http.Dispatcher.HttpControllerDispatcher.<SendAsync>d__1.MoveNext()"
}
}
}
为什么会出现该错误,或者为什么我无法在输出参数上返回Entity Colleciton?因为当我返回字符串时,它工作正常。
预先感谢
编辑
这是我下面的代码
using Microsoft.Xrm.Sdk;
using Microsoft.Xrm.Sdk.Query;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.IO;
using System.Runtime.Serialization;
using System.Xml;
namespace Plugins.GlobalActions
{
public class ACTGetData : IPlugin
{
public void Execute(IServiceProvider serviceProvider)
{
const string PROCESSUNIQUENAME = "MJT_ACTGETDATA";
IPluginExecutionContext context = (IPluginExecutionContext)serviceProvider.GetService(typeof(IPluginExecutionContext));
IOrganizationServiceFactory serviceFactory = (IOrganizationServiceFactory)serviceProvider.GetService(typeof(IOrganizationServiceFactory));
IOrganizationService service = serviceFactory.CreateOrganizationService(context.UserId);
if(context.MessageName.ToUpper() == PROCESSUNIQUENAME)
{
if(context.InputParameters["BookableResource"] == null)
{
throw new InvalidPluginExecutionException("Bookable Resource Can't be null");
}
Guid bookableResource = new Guid(Convert.ToString(context.InputParameters["BookableResource"]));
QueryExpression queryBookingStatus = new QueryExpression("bookingstatus");
queryBookingStatus.Criteria.AddCondition(new ConditionExpression("name", ConditionOperator.Equal, "Scheduled"));
var bookingStatus = service.RetrieveMultiple(queryBookingStatus);
if (bookingStatus != null)
{
QueryExpression queryBookableResourceboking = new QueryExpression("bookableresourcebooking");
queryBookableResourceboking.ColumnSet = new ColumnSet(true);
queryBookableResourceboking.Criteria.AddCondition("resource", ConditionOperator.Equal, bookableResource);
EntityCollection bookableResourceBookingEntity = service.RetrieveMultiple(queryBookableResourceboking);
if (bookableResourceBookingEntity.Entities.Count > 0)
{
context.OutputParameters["BookableResourceBooking"] = new EntityCollection(new List<Entity> { bookableResourceBookingEntity.Entities.FirstOrDefault(), bookableResourceBookingEntity.Entities[0] });
}
}
}
}
}
}
我的邮递员请求
organization.crm5.dynamics.com/.../MyActionName
{
"BookableResource" : "{BookableResourceGuid}"
}
答案 0 :(得分:2)
假设我有一个名为“ Result2”的输出参数。 以下是将输出参数发送到自定义操作的代码-
context.OutputParameters["Result2"] = new EntityCollection(new List<Entity> { entitlements.FirstOrDefault(), order}); //entitlements is List<Entity> object, order is Entity object
更新了帖子
对于EntityCollection,我们不需要指定目标实体,如下图所示。
这使我们可以发送带有单独实体对象的实体集合。从下图可以看到,我已经在一个列表中发送了权利和销售订单实体对象。
调用脚本如下。我已经使用Process.JS来调用动作。这是process.js的URL。请将其添加到您必须在其中调用操作的表单。GitHub link
function CreateRenewals() {
Xrm.Page.ui.setFormNotification("Please wait, this may take some time..", "INFORMATION", "1");
Process.callAction("gc_CreateRenewalOpportunity",
[{
key: "TargetOrder",
type: Process.Type.EntityReference,
value: new Process.EntityReference("salesorder", Xrm.Page.data.entity.getId())
}],
function (params) {
debugger;//Success
Xrm.Page.ui.clearFormNotification("1");
Xrm.Page.ui.setFormNotification(params.Result, "INFORMATION", "2");
},
function (e, t) {
Xrm.Page.ui.clearFormNotification("1");
Xrm.Page.ui.setFormNotification(params.Result, "INFORMATION", "3");
// Error
});
}
public void Execute(IServiceProvider serviceProvider)
{
ITracingService tracer = (ITracingService)serviceProvider.GetService(typeof(ITracingService));
IPluginExecutionContext context = (IPluginExecutionContext)serviceProvider.GetService(typeof(IPluginExecutionContext));
IOrganizationServiceFactory factory = (IOrganizationServiceFactory)serviceProvider.GetService(typeof(IOrganizationServiceFactory));
IOrganizationService service = factory.CreateOrganizationService(context.UserId);
try
{
Entity entity = null;
if (context.InputParameters.Contains("TargetOrder"))
{
if (context.InputParameters["TargetOrder"] is Entity)
entity = (Entity)context.InputParameters["TargetOrder"];
else if (context.InputParameters["TargetOrder"] is EntityReference)
{
EntityReference entityRef = (EntityReference)context.InputParameters["TargetOrder"];
entity = service.Retrieve(entityRef.LogicalName, entityRef.Id, new Microsoft.Xrm.Sdk.Query.ColumnSet(true));
}
}
context.OutputParameters["Result2"] = new EntityCollection(new List<Entity> { entity });
}
希望有帮助。