Add Contacts to Segments - with Power Automate!

In a previous post I talked about synchronising an office 365 mailing group to a marketing list in Dynamics [1], but ideally in the world of Dynamics Marketing we should be jumping straight to the Segment as this is what drives our customer journeys. I’m going to keep this post focused on the adding to segment part as there are so many reasons you might want to do this, my previous post is just one example if that.

How not to add a contact to a segment

SegmentMember.png

Lets get this out the way first - its not the same as how you would do for marketing lists. For marketing lists there is a many to many relationship from contact to marketing list so we can use the ‘relate records’ action in Power Automate.

Segments do not have a many to many relationship with Contacts. there is some kind of many to one relationships from Contact to Segment as there is some strange ‘Segment’ lookup field on the contact but that doesn’t many any sense because a Contact can belong to many many segments. Even if you put it on the form, it wont actually show up. So what does it do - I have no idea, but I can reassure you it doesn’t help at all here.

RelateRecords.png

Add a contact

So we have to delve into the world of ‘Perform an unbound action’. Lets be honest here:

  • Do I know what that means? No.

  • Do I know the diferece between unbound and bound? No.

  • Does it work? Yes.

So for now I really don’t need to know the other things, the bits you need to fill in are as follows

Action name: msdyncrm_SegmentMembersUpdate

msdyncrm_segmentid: GUID of the segment e.g. f9e5ba9a-542a-eb11-a813-000d3a8c324e

msdyncrm_operation: addByIds

msdyncrm_memberids: GUID of the contact e.g. 8503596f-8f61-ea11-a811-000d3a33433d

UnboundAction.png

Remove a contact

Exactly the same as above but change msdyncrm_operation: removeByIds

Add many contacts

I should have just stopped there, but curiosity got the better of me. The fact the actions says msdyncrm_memberids suggests to me I can add many contacts at once. I delved into the documentation [2] and it suggested creating something like

 "[\"B5672BDB-8899-43CB-9FA1-0AE4DC61DAD3\", \"694E1C8E-F704-4B23-9B07-E65DB1620E47\", \"A4A31E3D-DFCA-4765-8018-3BA7D5E376C7\"]"
SilentFail.png

So the flow runs successfully (yay) but the contacts don’t get added? Check the outputs of the action - it fails silently and goes down without a fight (doh!)

WhatFlowWants.png

The format power automate actually wants to see is this - now were talking flow language yay! So this is totally fine if I’m hard coding the GUIDs in a compose action. but how the heck do I do that dynamically based on a List Records actions for example?

Action name: msdyncrm_SegmentMembersUpdate

msdyncrm_segmentid: GUID of the segment e.g. f9e5ba9a-542a-eb11-a813-000d3a8c324e

msdyncrm_operation: addByIds

msdyncrm_memberids: GUID of the contacts in the format ["b814000a-6ace-ea11-a812-000d3a58d840","71c56449-2ad1-ea11-a812-000d3a58d840", "0ecd74c8-54cf-ea11-a812-000d3a58d840"]

Convert list records to Power Automate language

If you are smarter than me, you probably worked out a way simpler way to do this. You can stop reading now and go do some segment magic, otherwise stay with me. I went down a rabbit hole but I was kind of proud of what I achieved, I learnt something new and it worked. So I will let one of you geniuses out there tell me how to do it more efficiently but here’s the Amey way featuring explanations of what each crazy compose statement does along the way.

["b814000a-6ace-ea11-a812-000d3a58d840","71c56449-2ad1-ea11-a812-000d3a58d840", "0ecd74c8-54cf-ea11-a812-000d3a58d840"]
1.png

Initiate a string variable

So we need to formulate a string which is surrounded by [] and separated with “, - this is not how it comes out of the list records action. First we initiate a variable called “ContactsList“ and set the value to just [“

2.png

List records

Query to your hearts content here based on what you want to add but for this example I’m going to add all contacts with a first name of Amey to a segment (I made sure there was more than one don’t worry!). I use the Select Query so that it only returns the contactid (GUID), we don’t need anything else.

Add each GUID to the string variable

Next we are going to make a loop to append each of the GUIDS into the string variable “ContactList” - separating each item with “,

3.png

Remove the trailing characters

Currently the string ends with an open bracket and double quote, waiting for another GUID - but we don’t have anymore so now we need to trim the ending ,“

This would be super simple in the world of excel but not so much in Power Automate, we need 2 steps. First we are going to find the location/position of the last pesky ,“ using lastIndexOf which turns out to be 78. Right now what?

lastIndexOf(variables('ContactsList'),',')
4.png

Now we know where our offending characters are, we can use the subString function, to remove it and leave us with a string ready to close. So this formula says go find everything up to location 78 (worked out above) and abandon the rest of it.

substring(variables('ContactsList'),0,add(outputs('LastLocation'),0))
5.png

Close the parentheses [square brackets]

We are so close, the final cherry on the cake is to add the final ] and we do this directly in the unbound action call

6.png

It actually worked

The flow runs successfully and looks quite promising but I always think its too good to be true - its not, it actually worked - all the Amey’s are on the list!

AddManyCOntacts.png

A successful action result

For any of you non believers out there - this is what the actually successful action outputs look like in the flow.

7.png

Well done if you made it this far, like I said there is probably (hopefully) a much more efficient way to create the msdyncrm_memberids string and I’m fully open to suggestions. In the meantime its a success for the power platform seeing Dynamics Marketing and Power Automate work wonderfully together.

[1] Synchronise Outlook mailing lists to Dynamics Marketing segments https://www.ameyholden.com/articles/synchronise-outlook-mailing-lists-to-dynamics-marketing-segments

[2] ‘Basic’ operations on segments using the Segmentation API https://docs.microsoft.com/en-us/dynamics365/marketing/developer/extend-segments#addremove-contacts-to-static-segments?WT.mc_id=BA-MVP-5003692

Previous
Previous

Emails not tracking to Dynamics 365

Next
Next

Synchronise Outlook mailing lists with Dynamics Marketing