Hi all,
In continuation of my previous post here: http://musingsofasoftwareenthusiast.blogspot.in/2016/08/retrieve-data-from-configuration-record.html, here I will discuss how we can retrieve any record with the specified column set and specified search criteria by creating a generic method. In particular, this will be extremely useful to retrieve the values from "Master" entity record. A "Master" entity is a entity whose records are less likely to be created/modified/deleted when the Dynamics CRM system is being used. Also, master records will have a unique value like its name by which we can identify the record.
Here is the generic method:
/// <summary>
/// Retrieves the entity by unique values.
/// </summary>
/// <param name="entityName">Name of the entity.</param>
/// <param name="queryColumns">The query columns.</param>
/// <param name="requiredColumns">The required columns.</param>
/// <param name="service">The service.</param>
/// <returns>The entity as per the provided parameters</returns>
public static Entity RetrieveEntityByUniqueValues(string entityName, Dictionary<string, object> queryColumns, List<string> requiredColumns, IOrganizationService service)
{
if (!string.IsNullOrWhiteSpace(entityName) && queryColumns != null && requiredColumns != null &&
service != null)
{
var query = new QueryExpression(entityName);
query.ColumnSet = new ColumnSet();
foreach (var requiredColumn in requiredColumns)
{
query.ColumnSet.AddColumn(requiredColumn);
}
foreach (var queryColumn in queryColumns)
{
query.Criteria.AddCondition(queryColumn.Key, ConditionOperator.Equal, queryColumn.Value);
}
var recordsCollection = service.RetrieveMultiple(query);
if (recordsCollection != null && recordsCollection.Entities != null &&
recordsCollection.Entities.Count > 0)
{
return recordsCollection.Entities[0];
}
}
return null;
}
Here is how the method is called:
// get the validation activity category record
var validationActivityCategoryRecord =
Common.RetrieveEntityByUniqueValues(
"contoso_activitycategory",
new Dictionary<string, object>()
{
{"contoso_name", "Validation"}
},
new List<string>()
{
"contoso_name"
},
orgService);
Here is a walk through of the parameters that the method expects and how it works:
The first parameter is the name of entity that is expected.
The second parameter is a dictionary of the columns that will be part of the conditional expression in the query expression. As can be seen from the code above it is doing an equality check with the value provided. In the invocation above, I am passing the primary field of this entity (contoso_name) and providing the expected value as "Validation".
The third parameter is the list of columns that are expected from the entity being retrieved. Sometimes, we might not need any value as such, say when we are assigning the retrieved value to an entity reference field, so we can give an empty list (or the field that is part of the condition expression).
The last parameter is the organization service which is required to perform the platform operation.
I hope the above snippets help you to build up your Dynamics CRM Utility library!
Happy Dynamics CRMing!
In continuation of my previous post here: http://musingsofasoftwareenthusiast.blogspot.in/2016/08/retrieve-data-from-configuration-record.html, here I will discuss how we can retrieve any record with the specified column set and specified search criteria by creating a generic method. In particular, this will be extremely useful to retrieve the values from "Master" entity record. A "Master" entity is a entity whose records are less likely to be created/modified/deleted when the Dynamics CRM system is being used. Also, master records will have a unique value like its name by which we can identify the record.
Here is the generic method:
/// <summary>
/// Retrieves the entity by unique values.
/// </summary>
/// <param name="entityName">Name of the entity.</param>
/// <param name="queryColumns">The query columns.</param>
/// <param name="requiredColumns">The required columns.</param>
/// <param name="service">The service.</param>
/// <returns>The entity as per the provided parameters</returns>
public static Entity RetrieveEntityByUniqueValues(string entityName, Dictionary<string, object> queryColumns, List<string> requiredColumns, IOrganizationService service)
{
if (!string.IsNullOrWhiteSpace(entityName) && queryColumns != null && requiredColumns != null &&
service != null)
{
var query = new QueryExpression(entityName);
query.ColumnSet = new ColumnSet();
foreach (var requiredColumn in requiredColumns)
{
query.ColumnSet.AddColumn(requiredColumn);
}
foreach (var queryColumn in queryColumns)
{
query.Criteria.AddCondition(queryColumn.Key, ConditionOperator.Equal, queryColumn.Value);
}
var recordsCollection = service.RetrieveMultiple(query);
if (recordsCollection != null && recordsCollection.Entities != null &&
recordsCollection.Entities.Count > 0)
{
return recordsCollection.Entities[0];
}
}
return null;
}
Here is how the method is called:
// get the validation activity category record
var validationActivityCategoryRecord =
Common.RetrieveEntityByUniqueValues(
"contoso_activitycategory",
new Dictionary<string, object>()
{
{"contoso_name", "Validation"}
},
new List<string>()
{
"contoso_name"
},
orgService);
Here is a walk through of the parameters that the method expects and how it works:
The first parameter is the name of entity that is expected.
The second parameter is a dictionary of the columns that will be part of the conditional expression in the query expression. As can be seen from the code above it is doing an equality check with the value provided. In the invocation above, I am passing the primary field of this entity (contoso_name) and providing the expected value as "Validation".
The third parameter is the list of columns that are expected from the entity being retrieved. Sometimes, we might not need any value as such, say when we are assigning the retrieved value to an entity reference field, so we can give an empty list (or the field that is part of the condition expression).
The last parameter is the organization service which is required to perform the platform operation.
I hope the above snippets help you to build up your Dynamics CRM Utility library!
Happy Dynamics CRMing!
No comments:
Post a Comment