Dynamics CRM (TCS Tools): Use a workflow to set a lookup

By | April 21, 2017

It happens sometimes that we have a Parent and Child entities in Dynamics, and we need to link a child record to a parent record. Which seems to be a simple task unless, of course, we need to do it automatically. That’s exactly when we’ll know that we have an unexpected problem – if we wanted to use a workflow, there would be no out-of-the-box functionality that would allow us to do that.

Things can quickly get worse if it turns out we need to apply more complex rules when looking for a parent record. And what if there are duplicates? Arghh..

Here are just a few examples of that kind of relationship:

  • Account and Parent Account
  • Account and Primary Contact
  • Case and Customer

Every now and then we would have some unusual situation where we would have to put a few Child records to Dynamics, we would know that there are Parent records, and, yet, the relationship would be more complicated.

For instance, we might have a few parent accounts with the same name, and we would have to choose the most recent one. Out-of-the-box Dynamics workflow are not offering this kind of lookup functionality, but this can be accomplished using a custom workflow activity.

If that’s why you are reading this post, my TCS Tools solution for Dynamics CRM might be of some help. Here is how it works.

1. Import the solution to Dynamics. It is an managed solution, so you will be able to remove it.

2. Use advanced find to create a Lookup Configuration record (see complete walk-through below)

3. Create a worfklow on the Child entity that will be using TCS Tools:Lookup Setter activity to set parent lookup attribute based on the Lookup Configuration you just defined

 

Let’s imagine the following scenario for an Account and Contact entities:

in our Dynamics organization, we have account and contact records. There is “Account Number” field in the Account entity that’s been populated already:

That’s an out-of-the-box “accountnumber” field, although it is not available on the form by default, so I just added it there.

Also, let’s say there is a similar field in the contact Entity, and I actually had to create such a field since it’s not there out-of-the-box:

The schema name of this field is “tcs_accountnumber”. We’ll need it a bit later.

In our scenario the requirement is:

when adding a new contact, we will know account number, but we won’t know account name. What we need is to create a workflow that will correctly populate “Account Name” lookup in this situation.

So, first, let’s create our Lookup Configuration record. In order to do that, we will need to prepare Fetch Xml first. It’s easy to do using Advanced Find – I will open Advanced Find, define the filter, and will use “Download FetchXML” button to get the xml:

Notice how I put tcs_accountnumber in the filter there – you can put anything, it’s just for convenience(you’ll see in a second).

Here is my downloaded FetchXML:

<fetch version=”1.0″ output-format=”xml-platform” mapping=”logical” distinct=”false”>
  <entity name=”account”>
    <attribute name=”name” />
    <attribute name=”primarycontactid” />
    <attribute name=”telephone1″ />
    <attribute name=”accountid” />
    <order attribute=”name” descending=”false” />
    <filter type=”and”>
         <condition attribute=”accountnumber” operator=”eq” value=”tcs_accountnumber” />  
    </filter>
 </entity>
</fetch>

I have higlighted the part I’ll need to modify. “Lookup Setter” custom action will be running for contact record in this scenario. It will go over all the attributes of the current contact record, and it will try to find the following tags in the FetchXML:

#contact attribute schema name#

If it finds a tag, it will replace it with the actual value of the attribute.

Therefore, here is what we should use for the Lookup Configuration:

<fetch version=”1.0″ output-format=”xml-platform” mapping=”logical” distinct=”false”>
  <entity name=”account”>
    <attribute name=”name” />
    <attribute name=”primarycontactid” />
    <attribute name=”telephone1″ />
    <attribute name=”accountid” />
    <order attribute=”name” descending=”false” />
    <filter type=”and”>
         <condition attribute=”accountnumber” operator=”eq” value=”#tcs_accountnumber#” />  
    </filter>
 </entity>
</fetch>

From here, there are only a few steps left.

Let’s create our Lookup Configuration record (I’ll be using Advanced Find for this):

 

 

There are a few things on the last screenshot you need to be aware of:

  • Give that record any name you want, just make sure you know what it means
  • Use FetchXml created earlier fort he Fetch Xml field
  • Fetch Result Attribute is the schema name of the attribute that will be used to populate the target field
  • Target Attribute is self-explanatory. That’s what this whole thing is for, really

Once I have Lookup Configuration record, I can proceed to create a workflow:

This is a real-tie workflow – it does not have to be, but it’s more convenient when parent account is set right away.

It will run when a contact record is created or when Account Number field is modified on the contact.

And it won’t do anything (there is a condition in the first line) if Account Number field is empty.

Finally, if it runs and if it gets through that first condition to the TCS Tools:Lookup Setter custom workflow activity, it will need to know which Lookup Configuration to use, so I’m going to set the properties of that step:

That’s it.. Now I’ll save everything and activate the worfklow.

Let’s see what happens when I create a new contact – remember the very first screenshot in this post? I’ll be using the same 20170101 account number for my contact, but I’ll leave “Parent Account” field empty:

 

And, with that, I’ll just click “Save”.

Here we go.. I knew contact’s account number, and that’s what I put into the contact record – that workflow has done all the rest. It found the account record and populated “Account Name” field on the newly created contact.

From here, I might add more conditions to the Fetch XML if I wanted to define more elaborate rules. I might also use it in combination with the data import jobs or to fine-tune various data migration jobs. All of that without having to write any more code. Mission accomplished!

6 thoughts on “Dynamics CRM (TCS Tools): Use a workflow to set a lookup

  1. Karuna

    How do I download and try your managed solution for the lookup setter

    Reply
  2. Karuna

    Hi Alex,

    I have a requirement and I’d like to know how i can achieve it via your tool. Here it goes..

    First IOT Alert is created > there is no Parent IOT Alert so system creates a Work Order for that Asset.
    Second IOT Alert is created > Now system should check is there any Work Order of Status=Open + Work Order Asset = ERMS123 + Work Order Incident Type = App Hang. If all the 3 conditions are met, then it should get the WO’s related IOT Alert and set that Alert as the parent to the Second IOT Alert. If no record found it will not set any IOT Alert as parent to the second.

    Your solution would be a great help.

    Regards
    Karuna

    Reply
    1. Alex Shlega Post author

      Hi Karuna,

      if there us a relationship between those IOT alerts and work orders, you can probably use this kind of fetch to get the parent alert, and, then, you can run the workflow on the “Create” of the IOT alert record:

      <fetch…
      <entity name=”IOT Alert”>
      <attribute name=”iotalertid”/>
      <link-entity name=”Work Order” from … to…>
      <filter type=”and”>
      <condition attribute=”statecode”… />
      <condition attribute=”OrderAsset”… />
      <condition attribute=”IncidentType”… />
      </filter>
      </link-entity>
      </entity>
      </entity>

      Your fetch result attribute will be “iotalertid” (or whatever the name of the alert id attribute is – must be added to fetchXml then)
      Your target attribute will be “parentiotalertid” (or whatever the name of the parent lookup attribute is)

      Reply
  3. BK

    When You setup quick search view including Account Number field in account entity, and when you go to different entity and write account number of any account, CRM automatically will find this account and put it in Lookup.
    Why you did this development I didn’t understand.

    Reply
    1. Alex Shlega Post author

      The blog post shows you how to use this feature, not necessarily what to use it for. Have a look at what Karuna was trying to do above. That’s a good real-life scenario that requires more complex conditions and workflow automation.

      Reply

Leave a Reply

Your email address will not be published. Required fields are marked *