使用 DynamicsCRM 中的插件获取 EntityReference 的 GUID
这个问题与我现在为 Dynamics CRM 2011 创建的插件有关。
我有一个具有 5 个属性的实体。其中一个属性是对另一个实体的查找。
我试图用我的插件做的是检索查找实体的 Guid。很简单,对吧?
但不,这给我带来了麻烦,我尝试了数十种使用 EntityReference 来获取 guid 的方法,但都失败了。我尝试创建一个 preImage,但也失败了。
这是代码的片段。
Entity entity = (Entity)context.InputParameters["Target"];
if (entity.LogicalName == "new_producttaxrate")
{
if (entity.Attributes.Contains("new_product"))
{
EntityReference ownerLookup = (EntityReference)entity.Attributes["new_product"];
productName = ownerLookup.Name;
Guid productId = ownerLookup.Id;
}
}
也想出了这个
if (entity.Attributes.Contains("new_producttaxrateid"))
{
Guid myGuid = (Guid)entity.Attributes["new_producttaxrateid"];
EntityReference ownerLookup = new EntityReference("new_product", myGuid);
pid = ownerLookup.Id;
}
我不断收到这个错误
Unhandled Exception: System.ServiceModel.FaultException`1[[Microsoft.Xrm.Sdk.OrganizationServiceFault, Microsoft.Xrm.Sdk, Version=5.0.0.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35]]: Condition for attribute 'new_producttaxrate.new_product': expected argument(s) of type 'System.Guid' but received 'System.DBNull'.Detail:
<OrganizationServiceFault xmlns:i="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://schemas.microsoft.com/xrm/2011/Contracts">
<ErrorCode>-2147220989</ErrorCode>
<ErrorDetails xmlns:d2p1="http://schemas.datacontract.org/2004/07/System.Collections.Generic">
<KeyValuePairOfstringanyType>
<d2p1:key>CallStack</d2p1:key>
<d2p1:value xmlns:d4p1="http://www.w3.org/2001/XMLSchema" i:type="d4p1:string"> at Microsoft.Crm.Extensibility.VersionedPluginProxyStepBase.Execute(PipelineExecutionContext context)
at Microsoft.Crm.Extensibility.Pipeline.Execute(PipelineExecutionContext context)
at Microsoft.Crm.Extensibility.MessageProcessor.Execute(PipelineExecutionContext context)
at Microsoft.Crm.Extensibility.InternalMessageDispatcher.Execute(PipelineExecutionContext context)
at Microsoft.Crm.Extensibility.ExternalMessageDispatcher.ExecuteInternal(IInProcessOrganizationServiceFactory serviceFactory, IPlatformMessageDispatcherFactory dispatcherFactory, String messageName, String requestName, Int32 primaryObjectTypeCode, Int32 secondaryObjectTypeCode, ParameterCollection fields, CorrelationToken correlationToken, CallerOriginToken originToken, UserAuth userAuth, Guid callerId, Guid transactionContextId, Int32 invocationSource, Nullable`1 requestId, Version endpointVersion)
at Microsoft.Crm.Extensibility.OrganizationSdkServiceInternal.ExecuteRequest(OrganizationRequest request, CorrelationToken correlationToken, CallerOriginToken callerOriginToken, WebServiceType serviceType)
at Microsoft.Crm.Extensibility.OrganizationSdkServiceInternal.Execute(OrganizationRequest request, CorrelationToken correlationToken, CallerOriginToken callerOriginToken, WebServiceType serviceType)</d2p1:value>
</KeyValuePairOfstringanyType>
</ErrorDetails>
<Message>Condition for attribute 'new_producttaxrate.new_product': expected argument(s) of type 'System.Guid' but received 'System.DBNull'.</Message>
<Timestamp>2011-03-17T13:21:04.6999035Z</Timestamp>
<InnerFault i:nil="true" />
<TraceText>
[VATPlugin2: VATPlugin2.TaxRateValidation]
[4b0b7f5c-9950-e011-849d-000c292be099: VATPlugin2.TaxRateValidation: Create of new_producttaxrate]
</TraceText>
</OrganizationServiceFault>
This question is in relation to a Plugin which I am now creating for Dynamics CRM 2011.
I have an entity which has 5 attributes. 1 of these attributes is a Lookup to another entity.
What I am trying to do with my Plugin is to retreive the Guid of the Lookup entity. Simple, right?
But no, It is giving me trouble, I have tried dozens of ways using EntityReference to obtain the guid but cannot. I have tried creating a preImage which failed too.
Here is a snippet of the CODE.
Entity entity = (Entity)context.InputParameters["Target"];
if (entity.LogicalName == "new_producttaxrate")
{
if (entity.Attributes.Contains("new_product"))
{
EntityReference ownerLookup = (EntityReference)entity.Attributes["new_product"];
productName = ownerLookup.Name;
Guid productId = ownerLookup.Id;
}
}
also came up with this
if (entity.Attributes.Contains("new_producttaxrateid"))
{
Guid myGuid = (Guid)entity.Attributes["new_producttaxrateid"];
EntityReference ownerLookup = new EntityReference("new_product", myGuid);
pid = ownerLookup.Id;
}
I keep getting this error
Unhandled Exception: System.ServiceModel.FaultException`1[[Microsoft.Xrm.Sdk.OrganizationServiceFault, Microsoft.Xrm.Sdk, Version=5.0.0.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35]]: Condition for attribute 'new_producttaxrate.new_product': expected argument(s) of type 'System.Guid' but received 'System.DBNull'.Detail:
<OrganizationServiceFault xmlns:i="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://schemas.microsoft.com/xrm/2011/Contracts">
<ErrorCode>-2147220989</ErrorCode>
<ErrorDetails xmlns:d2p1="http://schemas.datacontract.org/2004/07/System.Collections.Generic">
<KeyValuePairOfstringanyType>
<d2p1:key>CallStack</d2p1:key>
<d2p1:value xmlns:d4p1="http://www.w3.org/2001/XMLSchema" i:type="d4p1:string"> at Microsoft.Crm.Extensibility.VersionedPluginProxyStepBase.Execute(PipelineExecutionContext context)
at Microsoft.Crm.Extensibility.Pipeline.Execute(PipelineExecutionContext context)
at Microsoft.Crm.Extensibility.MessageProcessor.Execute(PipelineExecutionContext context)
at Microsoft.Crm.Extensibility.InternalMessageDispatcher.Execute(PipelineExecutionContext context)
at Microsoft.Crm.Extensibility.ExternalMessageDispatcher.ExecuteInternal(IInProcessOrganizationServiceFactory serviceFactory, IPlatformMessageDispatcherFactory dispatcherFactory, String messageName, String requestName, Int32 primaryObjectTypeCode, Int32 secondaryObjectTypeCode, ParameterCollection fields, CorrelationToken correlationToken, CallerOriginToken originToken, UserAuth userAuth, Guid callerId, Guid transactionContextId, Int32 invocationSource, Nullable`1 requestId, Version endpointVersion)
at Microsoft.Crm.Extensibility.OrganizationSdkServiceInternal.ExecuteRequest(OrganizationRequest request, CorrelationToken correlationToken, CallerOriginToken callerOriginToken, WebServiceType serviceType)
at Microsoft.Crm.Extensibility.OrganizationSdkServiceInternal.Execute(OrganizationRequest request, CorrelationToken correlationToken, CallerOriginToken callerOriginToken, WebServiceType serviceType)</d2p1:value>
</KeyValuePairOfstringanyType>
</ErrorDetails>
<Message>Condition for attribute 'new_producttaxrate.new_product': expected argument(s) of type 'System.Guid' but received 'System.DBNull'.</Message>
<Timestamp>2011-03-17T13:21:04.6999035Z</Timestamp>
<InnerFault i:nil="true" />
<TraceText>
[VATPlugin2: VATPlugin2.TaxRateValidation]
[4b0b7f5c-9950-e011-849d-000c292be099: VATPlugin2.TaxRateValidation: Create of new_producttaxrate]
</TraceText>
</OrganizationServiceFault>
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
“new columnSet(true)”是不好的做法。请仅传递您想要的字段。
"new columnSet(true)" is bad practice. Please only pass fields you want to have.