What is Difference between Retrieve VS Retrieverequest in SDK - use Retrieverequest to get related records using single server call
Which method will you apply to get related records of an entity record?
Could we get it using service.retrieve? Probably you cannot... It will return single record but you may not get related records.
So how do you get it
You can utilize Retrieverequest for this purpose. You can pass Relatedentity query as part of retrieve request which will return related entities as part of the response.
Let us see some sample
if you want one specific incident record with its notes you can get it in a single server call.
Retrieve Method
Retrieve method used to
get specific record by Id
C#
|
public override Entity Retrieve (
string
entityName,
Guid id,
ColumnSet
columnSet
)
|
Parameters
entityName
Type: String. The logical name of
the entity specified in the entityId parameter.
id
Type: Guid. The ID of the record you want to retrieve.
columnSet
Type: ColumnSet.
A query that specifies the set of columns, or attributes, to retrieve.
Return Value
Type: Entity
The requested record.
The requested record.
Sample Syntax:
Entity entity = crmservice.Retrieve("incident", new Guid("5181F7EA-914F-E411-91C6-0050569A023C"), new ColumnSet(true));
Retrieverequest
Retrieve method used to
get specific record by Id including related entity instance in a single server call
Step 1: Define Related Entity request query. You can create multiple queries if required
QueryExpression OQueryExpression = new QueryExpression();
OQueryExpression.EntityName = "annotation";
OQueryExpression.ColumnSet = new ColumnSet(true);
RelationshipQueryCollection OrelatioshipQueryCollection = new RelationshipQueryCollection();
Relationship Orelationship = new Relationship("Incident_Annotation"); //Schema Name of relationship
OrelatioshipQueryCollection.Add(Orelationship, OQueryExpression);
RetrieveRequest OretrieveRequest = new RetrieveRequest();
OretrieveRequest.Target = new EntityReference("incident", new Guid("5181F7EA-914F-E411-91C6-0050569A023C"));
OretrieveRequest.ColumnSet = new ColumnSet(true);
OretrieveRequest.RelatedEntitiesQuery = OrelatioshipQueryCollection;
RetrieveResponse response=(RetrieveResponse)
service.Execute(OretrieveRequest);
How to read related entity records
foreach (KeyValuePair<Relationship, EntityCollection> relatedEntitiesCollection in
response.Entity.RelatedEntities)
{
Relationship relationship = relatedEntitiesCollection.Key;
EntityCollection entityCollection = relatedEntitiesCollection.Value;
for (int i = 0; i < entityCollection.Entities.Count;i++ )
{
Entity Oentity= entityCollection.Entities[i];
// Do your actions
}
}
Happy Coding!
Comments