What is the differences between RetrieveRequest and IOrganizationService.Retrieve in CRM?

百般思念 提交于 2019-12-06 06:33:10

In most cases the Retrieve method suffices.

The RetrieveRequest however adds an interesting feature: it provides the option to query data associated with the retrieved object in one go.

Imagine you need invoice data along with its Invoice Product records. One option would be to create a QueryExpression and join the results of the invoice entity and the invoicedetail entity. This would result in one, potentially large table.

With the RetrieveRequest you can query records associated with the retrieved entity in one request.

An example:

private readonly IOrganizationService _service;

public Entity GetFullInvoice(Guid invoiceId)
{
    var request = new RetrieveRequest
    {
        ColumnSet = new ColumnSet(allColumns: true),
        Target = new EntityReference("invoice", invoiceId),
        RelatedEntitiesQuery = new RelationshipQueryCollection()
    };

    var relation = new Relationship("invoice_details");
    relation.PrimaryEntityRole = EntityRole.Referenced;

    var invoiceDetailQuery = new QueryExpression("invoicedetail");
    invoiceDetailQuery.ColumnSet = new ColumnSet(allColumns: true);
    invoiceDetailQuery.Criteria.AddCondition("invoiceid", ConditionOperator.Equal, invoiceId);

    var result = (RetrieveResponse)_service.Execute(request);

    return result.Entity;
}

The Entity object returned by GetFullInvoice has a RelatedEntities property holding the entity collections related to the invoice.

Multiple queries can be added to the RetrieveRequest, so it would also be possible to retrieve associated activities etc. in one go as well.

A RetrieveRequest and the Retrieve method of IOrganizationService do exactly the same thing: retrieve a specific record.

Eventually RetrieveRequest can be batched (using the ExecuteMultipleRequest message) but personally I never see a RetrieveRequest batched, because normally UpdateRequest or DeleteRequest are the one batched in order to improve performance.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!