CRMContext is a special object that is available to you in the code expressions, and that provides some core properties / methods you can use to work with Dynamics.
Here is an example of a code expression that’s using CRMContext:
CRMContext.Target[“name”] = “First Account”;
This expression is going to set “name” parameter of the target record to “First Account”.
Let’s see what’s available there:
1. CRMContext.Target
For the Create/Update/Delete/Retrieve expressions, this is the actual entity record for which your expression is running. If you are familiar with plugins development, it’s what you would get in the plugin if you accessed context.InputParameters[“Target”]
CRMContext.Target[“name”] = “Test Account”;
For the RetrieveMultiple expressions, this is, actually, an EntityCollection. Which is the same as context.InputParameters[“BusinessEntityCollection”] in the plugins.
foreach(a in CRMContext.Target.Entities)
{
a[“name”] = “Test”;
}
2. CRMContext.PreImage
For the Update/Delete expressions, this is how the entity looked like before the operation started.
if(CRMContext.PreImage[“name”] == CRMContext.Target[“name”]){
…
}
3. CRMContext.UserId
This is GUID of the user performing the operation
4. CRMContext.CreateRecord(string entityName)
You can use this function to create a new entity record:
newRecord = CRMContext.CreateRecord(“account”);
5. CRMContext.SaveRecord(Entity entity)
You can use this function to update an entity record. If you used CreateRecord to create that record in the first place, this call will be translated into a “Create” call to the organization service. Otherwise, it’ll be transalted into an “Update” call:
CRMContext.SaveRecord(updatedAccount);
6. CRMContext.DeleteRecord(Entity entity)
You can use this function to delete an entity record:
CRMContext.DeleteRecord(account);
7. CRMContext.DeleteRecord(Entity entity)
You can use this function to delete an entity record:
CRMContext.DeleteRecord(account);
8. CRMContext.LookupRecord(entityName, attributeName, attributeValue)
You can use this function to lookup a record:
contact = CRMContext.LookupRecord(“contact”, “fullname”, “CRM Admin”);
9. CRMContext.QueryFetchXml(fetchXml)
You can use QueryFetchXml to run a fetchXml query:
contacts = CRMContext.QueryFetchXml(<YOUR FETCH XML>);
foreach(c in contacts)
{
…
}
12. CRMContext.IsTeamMember(TeamId, UserId)
You can use IsTeamMember function to verify user’s membership in a team:
if(CRMContext.IsTeamMember(teamId, userId))
{
…
}
13. There is, also, a Sequence(sequenceName, minimumCharacters, leadingCharacters) function that you can use to generate sequential id-s. This function is not in the CRMContext, though:
CRMContext.Target[“name”] = Sequence(“Account”, 5, “*”);
In the example above, “name” attribute of the current entity record will be populated with a new sequential id. That id will be generated for the “Account” sequence (it will be created if it does not exist), it will have at least 5 characters, and, if the number is not big enough, it will use “*” for the leading characters (so, for example, the first time you call it you’ll get *****1 as a result)