Automatically add Resource to Resource Group in Dynamics CRM

Paul Nieuwelaar, 18 May 2015

One of the key pieces of Service Scheduling is resources and resource groups. If your resources are not in any resource groups, they won't be able to be scheduled for any services. To make sure this doesn't happen, we can automatically add resources into a default resource group when they are created.

For this example, I will be adding new Facility/Equipment into an 'All Resources' resource group when they are created.

At first glance this seems like it will be pretty easy. Plugin on create of equipment, retrieve the resource group, perform an associate request to link them together. The problem is… there is no "relationship" between resources and resource groups, or more specifically, between facility/equipment and resource groups, or between resource groups and anything really!

This is because CRM doesn't actually use a traditional relationship to manage the resources. One can only imagine this is because resources need to link to users, facility equipment, teams, as well as other resource groups; so it's not really a relationship like we know them.

The resources for each resource group are actually saved in the 'Constraints' field on the resource group, as XML. Seems simple once you know where it is.

So here's the somewhat tricky part. Assuming the resource group already has some resources, we need to add our new resource into the mix, without affecting the existing resources.

For starters, here's what the XML in that field needs to look like (back slashes are escaped for C# string):

No resources:

<Constraints><Constraint><Expression><Body>false</Body><Parameters><Parameter name=\"resource\" /></Parameters></Expression></Constraint></Constraints>

One resource:

<Constraints><Constraint><Expression><Body>resource[\"Id\"] == {299ce055-d6a0-4430-888e-961b87e89f07}</Body><Parameters><Parameter name=\"resource\" /></Parameters></Expression></Constraint></Constraints>

Two or more resources:

<Constraints><Constraint><Expression><Body>resource[\"Id\"] == {299ce055-d6a0-4430-888e-961b87e89f07} || resource[\"Id\"] == {c4224c78-b6ae-4c88-b2fc-0bd92edd1c31}</Body><Parameters><Parameter name=\"resource\" /></Parameters></Expression></Constraint></Constraints>

The important things to note are:

  1. The Id's are the unique identifiers of the facility/equipment or system users. From what I can tell CRM will figure out whether the Id matches to a user or an equipment automatically.
  2. Make sure that each resource is separated with || .
  3. If there are no resources, the body will be 'false', so this needs to be removed from the body when you add the first resource.

Once you've got the existing Constraints, and modified the XML to include your new resource, you just need to simply update the resource group with the new Constraints XML and the resource will automatically start to show in the resource group and vice versa.