Amey Holden

View Original

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

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

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

See this content in the original post

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.

See this content in the original post

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 “,

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?

See this content in the original post

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.

See this content in the original post

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

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!

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.

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