Create Dynamics 365 Marketing Segments with Power Automate
Now that I have worked out how to convert spreadsheets of Contacts/Leads into old school marketing lists. It felt most appropriate to see how to create and publish Dynamics 365 Real-time Marketing segments within a Power Automate flow.
Official developer docs on working with segments here.
There are two parts to a segment, a ‘Segment Definition’ and a ‘Segment’, when you create one via the UI this is all handled for you, but if you are using flow you need to create the definition first, then the segment. One of the wonderful thing about the real-time marketing segments is that you can combine static and dynamic segment components into a single segment definition, oh and oof course your segment can be based on Leads or Contacts (also known as ‘Members’).
Static - a specific list of members identified by their unique Dynamics 365 Id (GUID) e.g. 3a0e5441-212e-4902-b89c-3d90994e5211. If you look at the URL for a Dynamics 365 record its the string on letters and numbers after the last = sign. It’s unique to that record.
Dynamic - based on a data query such as Lead Source is ‘Landing Page’ and Status is ‘Open’ e.g. PROFILE(lead).FILTER(statecode == 0 && leadsourcecode == 192350100)
Create a Segment Definition (msdynmkt_segmentdefinition)
First, create the Segment Definition (msdynmkt_segmentdefinition), this is where you define who should be on the segment through either static inclusions, static exclusions or dynamics queries. You can use a combination of any of these to get the results needed. Note the items below in red need to be defined when creating the segment definition.
Always included segment members/Always excluded segment members is a comma seperated sting of lead or contact GUIDs
Segment Query is written in a language specific to Dynamics 365 Marketing, written by the segment creator as you build a segment (if I ever work out how to translate FetchXML into this ‘language’ I will let you know…)
WARNING: ‘Always included segment members’/’Always excluded segment members’ is limited to 100 members maximum at the moment (hence my use of Marketing Lists for the CSV conversion monster).
Create a Segment (msdynmkt_segment)
Now, create the Segment (msdynmkt_segment), which is linked to the Segment Definition created above.
Base Entity Logical Name lead or contact
CDM Partition Primary Key Column @{null}
Data Lake Folder Id (Data Lake Folders) @{null}
Last Updated On @{utcNow()}
Source Real-time marketing
Source Segment Uid @{outputs('CreateSegmentDefinition')?['body/msdynmkt_segmentdefinitionid']} (The segment definition GUID from your previous step)
Display Name @{outputs('CreateSegmentDefinition')?['body/msdynmkt_name']} (The name of the segment definiton from the rpevious step)
Source Segment Created By ’Magic’ (the name of the user who created the segment, it is not a proepr user lookup, just free text so you can basically put in here whatever you want)
Source Segment Created On @{utcNow()}
Status Inactive
Status Reason Inactive
Publish the Segment
The segment will now exist but only in draft status, to use it in a journey or anything else to need to mark it as ‘Ready to use’. This time we need to make a HTTP request using the ‘HTTP with Azure AD’ connector.
Method POST
Url of the request https://@{uriHost(outputs('CreateSegmentDefinition')?['body/@odata.id'])}/api/data/v9.0/msdynmkt_PublishSegmentDefinition
Body of the request
{
"SegmentId": "@{outputs('CreateSegment')?['body/msdynmkt_segmentid']}"
}
If you haven’t used this connector before you will need to create the connection, you just need to use your dynamics 365 base URL in both boxes like below
That’s it - done!