Looking for PowerObjects? Don’t worry, you’re in the right place! We’ve been part of HCL for several years, and we’ve now taken the final step in our acquisition journey: moving our website to the HCL domain. Nothing else is changing – we are still fanatically focused on Microsoft Business Applications!

PowerObjects Blog 

for Microsoft Business Applications


Using Fetch XML in a Workflow in Microsoft Dynamics CRM 2011

Post Author: Joe D365 |

Users of Microsoft Dynamics CRM 2011 have many features they can draw on to improve their CRM experience and improve business processes. One great feature to explore is using Fetch XML in a workflow.

Let's dig in using a real business situation.

Scenario - How to determine if a Contract's Contract Lines have a particular Product.

Solution - Luckily Microsoft CRM 2011 provides the use of executing Fetch XML in a custom workflow. Using Fetch XML (or Query Expression) in a custom workflow allows you to set up your query to use as many relationship levels as required. The added benefit of using Fetch XML is that you can use Advance Find to set up your query and download the Fetch Statement without using any other tools.

1. Start by using the Advance Find view to set up your fetch statement.

Microsoft Dynamics CRM Fetch XML

2. Next click on the Download Fetch XML button from the ribbon and open the downloaded file to select the fetch statement.

3. Next set up your Custom Workflow using Visual Studio 2010. For brevity you can reference http://msdn.microsoft.com/en-us/library/gg328515.aspx (Create a Custom Workflow Activity)

4. In the Execute method set a string variable with your Fetch XML statement and use the Services' RetrieveMultiple method with a FetchExpression as its parameter.

Microsoft Dynamics CRM Workflows

5. Register your Custom Workflow using the Plugin Registration tool.

Now you have the ability to run your Custom Workflow and validate if a Contract has Product X.

Snazzy, eh? Let us know if this tip comes in handy or need a Microsoft CRM Partner to work assist in your project.

Happy CRM'ing.

Joe CRM
By Joe D365
Joe D365 is a Microsoft Dynamics 365 superhero who runs on pure Dynamics adrenaline. As the face of PowerObjects, Joe D365’s mission is to reveal innovative ways to use Dynamics 365 and bring the application to more businesses and organizations around the world.

One comment on “Using Fetch XML in a Workflow in Microsoft Dynamics CRM 2011”

  1. i try to put into crm, but its not working for me..
    I cant see it in my workflow.
    any step I did wrong ??

    The following is my code: after that I registered it into crm.
    ---------------------------------------------------------------------------------
    using System;
    using System.Activities;
    using System.Linq;
    using System.ServiceModel;
    using System.Text;
    using Microsoft.Xrm.Sdk;
    using Microsoft.Xrm.Sdk.Client;
    using Microsoft.Xrm.Sdk.Messages;
    using Microsoft.Xrm.Sdk.Metadata;
    using Microsoft.Xrm.Sdk.Query;
    using Microsoft.Xrm.Sdk.Workflow;

    namespace FetchWFExample
    {
    public class FetchWf : CodeActivity
    {
    protected override void Execute(CodeActivityContext executionContext)
    {
    var context = executionContext.GetExtension();
    var serviceFatory = executionContext.GetExtension();
    var service = serviceFatory.CreateOrganizationService(context.UserId);

    Opportunity.Set(executionContext, false);

    const string fetch = "" +
    "" +
    "" +
    "" +
    "" +
    "" +
    "" +
    "" +
    "" +
    "" +
    "" +
    "" +
    "" +
    "" +
    "" +
    "";

    if (service.RetrieveMultiple(new FetchExpression(fetch)).Entities.Count > 0)
    Opportunity.Set(executionContext, true);
    }
    [Output("Opportunity")]
    public OutArgument Opportunity { get; set; }
    }
    }

PowerObjects Recommends