locked
Convert BusinessEntity to Account? RRS feed

  • Question

  • Thanks to the folks here, I am finally getting somewhere but still a long way to go.

    I am trying to retrieve an account entity with a condition. That works fine and  I am using the RetrieveMultipleResponse web method to retrieve a BusinessEntity Collection.

     

    What I want now is to strongly type/convert the BusinessEntity into an account entity. How can I do this?

     

    I have followed the code snippets in the SDK and implemented them into my solution however, I always get the "Object cannot be converted to target type" exception.

     

    I can see the values and names of the entity, thats good and what I expect but would like to convert that entity into a strongly typed Account entity. Does anyone know how?

     



    BusinessEntity busEntity = (BusinessEntity)Convert(currentBusinessEntity); //looping through the collection
         

     

     

     

    The Convert class is in the SDK in the howto folder in the class called ConvertDynamicToCore. That is the same code I am using.

     

    What am I doing wrong?

     

    in addition, how can I check which type of entity has returned to me and convert it into the correct type? So I could be retrieving say a task collection or something, I then want to be able to strongly type convert that to a task entity. Does the BusinessEntity act like a "core" entity where I can then cast it into the target entity I like?

     

    What about say, retrieving a custom entity and getting its values/properties?

     

    Thanks!

    Wednesday, June 20, 2007 9:36 AM

Answers

  • You don't need the Convert class (that is only relevant for converting dynamic entities, and you won't be returning dynamic entities). All you need is

     

    account myAccount = (account) currentBusinessEntity;

     

    RetrieveMultiple will only return entities of one type, as determined by the QueryExpression you pass it.

     

    You use exactly the same approach for custom entities.

     

    The CRM SDK has enough examples to show you how to do this

     

     

    Wednesday, June 20, 2007 2:30 PM
    Moderator
  •  

    Thanks David, I almost eventually got it done, thankfully :-), pretty much the same as your response.

     



    TargetRetrieveAccount target = new TargetRetrieveAccount();
     

        target.EntityId = new Guid(accountID);
     
        // Create the request object.
        RetrieveRequest retrieve = new RetrieveRequest();
     
        // Set the properties of the request object.
        retrieve.Target = target;
        retrieve.ColumnSet = new AllColumns();
     
        // Execute the request.
        RetrieveResponse retrieved = (RetrieveResponse)service.Execute(retrieve);
        
        account accountEntity = new account();
        accountEntity = (account)retrieved.BusinessEntity;

     

     

    Wednesday, June 20, 2007 3:09 PM

All replies

  • You don't need the Convert class (that is only relevant for converting dynamic entities, and you won't be returning dynamic entities). All you need is

     

    account myAccount = (account) currentBusinessEntity;

     

    RetrieveMultiple will only return entities of one type, as determined by the QueryExpression you pass it.

     

    You use exactly the same approach for custom entities.

     

    The CRM SDK has enough examples to show you how to do this

     

     

    Wednesday, June 20, 2007 2:30 PM
    Moderator
  •  

    Thanks David, I almost eventually got it done, thankfully :-), pretty much the same as your response.

     



    TargetRetrieveAccount target = new TargetRetrieveAccount();
     

        target.EntityId = new Guid(accountID);
     
        // Create the request object.
        RetrieveRequest retrieve = new RetrieveRequest();
     
        // Set the properties of the request object.
        retrieve.Target = target;
        retrieve.ColumnSet = new AllColumns();
     
        // Execute the request.
        RetrieveResponse retrieved = (RetrieveResponse)service.Execute(retrieve);
        
        account accountEntity = new account();
        accountEntity = (account)retrieved.BusinessEntity;

     

     

    Wednesday, June 20, 2007 3:09 PM