Random Musings

June 27, 2009

what is the difference between RetrieveRequest and RetrieveMultipleRequest

Filed under: Dynamics CRM — haditeo @ 10:43 am
Tags: ,

RetrieveRequest is used to retrieve one and only one record based on the unique EntityId while RetrieveMultipleRequest can be used to retrieve one or many records based on the selection criteria.

You can use RetrieveMultipleRequest to retrieve a particular record based on EntityId, usually i create a helper method by using RetrieveMultipleRequest to retrieve particular records based on selection criteria

Here is the example of using RetrieveRequest. Our scenario is retrieving a particular account based on accountid attribute

public DynamicEntity RetrieveByUsingRetrieveRequest(CrmService service, Guid accountId)
{
    TargetRetrieveAccount target = new TargetRetrieveAccount();
    target.EntityId = accountId;

    RetrieveRequest request = new RetrieveRequest();
    request.Target = target;
    
    // retrieve all attributes of the particular account
    request.ColumnSet = new AllColumns();
    
    // tell the CRM SDK to return the result as DynamicEntity type of object
    request.ReturnDynamicEntities = true;
    RetrieveResponse response = (RetrieveResponse) service.Execute(request);
    
    DynamicEntity de = (DynamicEntity)response.BusinessEntity;
    
    return de;
}

and here is the example of using RetrieveMultipleRequest

public DynamicEntity RetrieveByUsingRetrieveMultipleRequest(CrmService service, Guid accountId)
{
    QueryExpression query = new QueryExpression();
    query.EntityName = "account";
    
    ConditionExpression ceAccount = new ConditionExpression();
    ceAccount.AttributeName = "accountid";
    ceAccount.Operator = ConditionOperator.Equal;
    ceAccount.Values = new object[] {accountId};
    
    FilterExpression feAccount = new FilterExpression();
    feAccount.AddCondition(ceAccount);
    feAccount.FilterOperator = LogicalOperator.And;
    
    query.Criteria.AddFilter(feAccount);

    // retrieve all attributes of the particular account
    query.ColumnSet = new AllColumns();

    RetrieveMultipleRequest request = new RetrieveMultipleRequest();
    request.Query = query;
    
    // tell the CRM SDK to return the result as DynamicEntity type of object
    request.ReturnDynamicEntities = true;

    RetrieveMultipleResponse response = (RetrieveMultipleResponse) service.Execute(request);

    DynamicEntity de = (DynamicEntity)response.BusinessEntityCollection.BusinessEntities[0];

    return de;
}

Note :

  • ReturnDynamicEntities property can be used to configure the CRM SDK either to return the result as Dynamic Entity or Proxy Class
  • The disadvantage of using RetrieveRequest is the query can only be implemented by querying the entity-id. Usually our goal is to obtain the particular entity-id by querying certain criteria.

    Let me elaborate further. Suppose you like to know which accounts residing in Singapore, you can query like this SELECT * FROM account where city=’Singapore’ and it will return the list of accounts complete with the account id

    It’s not useful to use RetrieveRequest, if we know ahead all accounts, means SELECT * FROM account where accountid = ‘3F2504E0-4F89-11D3-9A0C-0305E82C3301’

    Usually our targets are the Unique Identifiers.

Advertisements

Leave a Comment »

No comments yet.

RSS feed for comments on this post. TrackBack URI

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

Create a free website or blog at WordPress.com.

%d bloggers like this: