Dynamics CRM 2011 Dialog Process Dynamically Query CRM Data

Paul Nieuwelaar, 06 January 2012

In my previous post on Dialogs I looked at some of the new features included in dialogs from the rollup 5 update, including the new response types of Date and Lookup available.  I will be showing how to query data dynamically, so you can perform filters based on the context of the current record, for example showing a list of all the contacts associated with an Account.

 Dynamics CRM 2011 Dialog Process Dynamically Query CRM Data

You can also query any other data in CRM that an advanced find would allow you to do. This means you can return results from records that are not only related to the entity you’re running the dialog on. For example, you could create a query for all Accounts with a relationship of Customer in your system, and then display those within the Dialog for a user to select from.

To create a query in your Dialog, click on the Add Step menu within your Dialog, and select Query CRM Data. It’s usually a good idea to declare your Queries at the beginning of your dialog, so that any branch of your dialog can use them, but otherwise it doesn’t matter where the Query is declared, so long as it is before the point in which you need to use it.

 Dynamics CRM 2011 Dialog Process Dynamically Query CRM Data

When you Set Properties for the Query, you will be able to use the advanced find style of filtering, where you can select your entity, build your query, and edit the columns. In the example of returning the Contacts related to an Account, we can build our query on Contacts, where the Parent Customer equals a specific Account. For now, we can set the account to a placeholder record (in this case Frosty’s Ice Cream Store). This is just temporary so we can insert the placeholder later.

 Dynamics CRM 2011 Dialog Process Dynamically Query CRM Data

At the bottom of your query you can ‘Find’ records based on your filter just to test the query is returning the correct records. You may also want to think carefully about the sort order set on the columns, as this will determine the order in which the records are displayed.

Once you have finished designing your query, and adding the columns you want available in your results, you can move over to the ‘Modify Query Variables’ tab within the query, where we will enter the dynamic variable for Parent Customer.

If you have only one filter, then there will be one variable. Otherwise you will need to match the variable with the conditions in the query. To set a dynamic value, click into the XML Values field for the variable you are changing, and use the Form Assistant to select the dynamic value. In this case I have selected the Parent Customer of the Contact we are running the Dialog on, so that all the Contacts within their Company will be returned.

 Dynamics CRM 2011 Dialog Process Dynamically Query CRM Data

Once you have modified the query variables, you will not be able to go back and modify the query design without discarding your changes, so make sure the query’s right before you assign the variables. Finally set a label for this query, for example ‘Related Contacts’, so we will know which query we’re using later on.

Next add your Page, and Prompt and Response to your Dialog. You can then set the properties of the prompt and response, and select either Option Set (radio buttons) or Option Set (picklist), which basically means you can display the results in a standard option set list, or in a list of radio buttons. Once you select the Option Set Response Type, the fields below will become active, where you can ‘Provide Values’ from the option ‘Query CRM Data’. This will allow you to select the query you just created.

 Dynamics CRM 2011 Dialog Process Dynamically Query CRM Data

You can then select the Columns to pull through into the list. If you added multiple columns to the query, you can add these in using a separator, for example I have chosen to display the Name of the Contact, followed by their E-mail. You can also provide a separator for these values, such as a colon.

Once you have set a Label and some Prompt text, you can Save and Close this Prompt and Response. At this point you can continue building your Process or you can Activate your Dialog, and test that it is working.

 Dynamics CRM 2011 Dialog Process Dynamically Query CRM Data

Now that we’ve captured this information, we can go and update the Parent Customer with the newly found Primary Contact. To do this we simply add an Update Record step after our Page, and select the Parent Customer (Account) record to update, as we are running this Dialog on a Contact.

We can then set the properties of the Update Record, and assign the dynamic value from the question response.

 Dynamics CRM 2011 Dialog Process Dynamically Query CRM Data

Because we are retrieving Contact records, we can assign the response directly into the Primary Contact field. We can also use the other fields returned in our query, for example the email address field that was added to the columns. We can pull these individual fields from our selected Contact record, such as the full name or email, and assign the values into other fields, or even into our other prompt and responses!

That’s all there is to it, we can reuse these queries in other prompts, or create additional queries to extend your dialogs. Using queries in your Dialogs will make selecting records a lot easier, and more relevant for users that need to select and enter the data into the Dialogs.

There’s a lot more that can be done with Dialogs than what I have just shown. They are quite powerful once you start using them to their full potential.