Wednesday, March 11, 2015

Business Process Flow and Bulk Update of Process and Stage

Let’s imagine following situation – customer decided to use Business Process Flow for one of entities that already existed in CRM. You designed required BPF and everything looks great for new records. But what to do with existing records? Obvious answer is that you need somehow to update existing records. This article will show how to do it in easy and almost no-code way.
In my case customer used picklist with 4 values (Phase 1-4) to control stage of a record. Of course I knew that it would be possible to write code to update processid and stageid fields but we have amazing SSIS Integration Toolkit to do such kind of task. Here are steps:
 Open Visual Studio and Create “Integration Services Project”:
Add “Data Flow Task” to your project:

Open created Data Flow Task and add Dynamics CRM Source to Data Flow Task:

Configure connection to CRM and choose clause that would be used for data fetching (in my case I used Fetch Xml) – select field-pk of records would want to update and fields based on which you will decide with what stage particular record would be updated:

Add “Derived Column” to Data Flow Task to add identifier of BPF:

Add “Script Component” of type Transformation to add identifier of stage of BPF:

 

Use following code in Script Component:
public override void Input0_ProcessInputRow(Input0Buffer Row)
{
    string result = null;

    switch (Row.newopportunityphase)
    {
        case 100000000:
            result = "6234178d-4cad-4aa5-8b10-3b7e84c16829";
            break;
        case 100000001:
            result = "9223b6b6-ad31-a242-9128-19303819c123";
            break;
        case 100000002:
            result = "c0481a30-8500-7230-7632-0d8d17aad035";
            break;
        case 100000003:
            result = "5d6e8351-6456-6c52-3648-2676e760627b";
            break;
    }

    Row.stageid = result;
}


Save and add “Dynamics CRM Destination” component:

Configure connection to CRM, choose action, entity and map fields for update:


So that’s it and now we can update data:

Lets imagine reverse scenario – BPF existed for entity and customer decided to turn it off and remove from a system. After you will deactivate all BPFs and for new records BPF bar would not appear you will see that for existing record it is still available. Solution – clean processid and stageid fields for existing records. Steps would be almost the same as in previous sample with little difference:

“Derived Column”:

No need in script component – just add “Dynamics CRM Destination”:
That's it. Once you've ran DTS identifiers of process and stage would be cleaned.