2011-03-17 7 views
9

Questa domanda è in relazione ad un plugin che ora sto creando per Dynamics CRM 2011.obtaing il GUID per un EntityReference utilizzando plugin in DynamicsCRM

Ho un ente che ha 5 attributi. 1 di questi attributi è una ricerca di un'altra entità.

Quello che sto cercando di fare con il mio Plugin è quello di recuperare il Guid dell'entità Ricerca. Semplice, vero?

Ma no, mi sta dando problemi, ho provato dozzine di modi usando EntityReference per ottenere il guid ma non ci riesco. Ho provato a creare una preImage che ha fallito troppo.

Ecco uno snippet del CODICE.

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; 

    } 
    } 

anche si avvicinò con questo

 if (entity.Attributes.Contains("new_producttaxrateid")) 
       { 
        Guid myGuid = (Guid)entity.Attributes["new_producttaxrateid"]; 
        EntityReference ownerLookup = new EntityReference("new_product", myGuid); 
        pid = ownerLookup.Id; 
       } 

Continuo a ricevere questo errore

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> 
+0

si avvicinò con questo pensare im più stretti – Calibre2010

+0

if (entity.Attributes.Contains (" new_producttaxrateid ")) { Guid myGuid = (Guid) entity.Attributes [" new_produc ttaxrateid "]; EntityReference ownerLookup = new EntityReference ("new_product", myGuid); // EntityReference ownerLookup = (EntityReference) entity.Attributes ["new_producttaxrateid"]; pid = ownerLookup.Id; } – Calibre2010

risposta

13
 if (entity.Attributes.Contains("new_product")) 
        { 

         productGUID = ((EntityReference)entity["new_product"]).Id; 
         Entity member = service.Retrieve("product", ((EntityReference)entity["new_product"]).Id, new ColumnSet(true)); 
         if (member.Attributes.Contains("name")) 
         { 
          productName = member.Attributes["name"].ToString(); 
         } 
        }