Posted in Dynamics 365 For Marketing, Uncategorized

Enable Custom Entities For Marketing Segment Dynamic Query

A segment in 365 for marketing app is an essential feature which lets you create a list of related contacts based on some criteria( similar to advanced find). Later it is used to target customers in a customer journey.

So when you create a new segment you have an option to create it as static or dynamic. A dynamics segment is what lets you add a query to filter contacts.

picture depicts a dynamic segment query designer

However, there is one thing that will bother you which is, it will not let you add a related custom entity to form a query ( click on add group and select union as relationship)

Solution

There is a settings available in Marketing app which will let you enable this feature for any entity in your system. To check this, go to Marketing App — > Settings Section:


Under Marketing Settings, click on advanced settings.
Now click on Customer Insights Sync under marketing setting section which will give you the list of entities on the right side with check boxes.

Enable for the entities which you need and click on publish from the header.

You might see the box is grayed out and you will not have option to enable. To enable the box you need to enable the change tracking on that entity. to do that you have to go to that entity definition and enable change tracking check box. https://docs.microsoft.com/en-us/power-platform/admin/enable-change-tracking-control-data-synchronization

Once published give it some half an hour.

Navigate to your query now and you will be able to add related entities to your query just like advanced find.

I hope this helps!

Posted in Dynamics 365 For Marketing

Dynamics 365 For Marketing – Retrieve Contacts From A Segment

I had a hard time trying to figure out a way to retrieve members from a marketing segment in my code.

The link-entity between contact and msdyncrm_segment isn’t materialized in CRM. I even raised it here :Basic operations on segments using the Segmentation API

Solution:

Finally fetch xml came to rescue. You can use below xml which can be executed by WebAPI or Organization service and it works on both static and dynamics segments.

<fetch version="1.0" output-format="xml-platform" mapping="logical" returntotalrecordcount="true" page="1" count="5000" no-lock="false">
<entity name="contact">
<attribute name="fullname"/>
<attribute name="contactid"/>
<order attribute="fullname" descending="false"/>
<link-entity name="msdyncrm_segment" from="msdyncrm_segmentid" to="msdyncrm_segmentmemberid" alias="bb">
<filter type="and">
<condition attribute="msdyncrm_segmentid" operator="eq" uitype="msdyncrm_segment" value="bfc9d5d6-d6aa-e911-a859-000d3a3159df"/>
</filter>
</link-entity>
</entity>
</fetch>

Now lets try to execute this first in web api.
Use below format : https://orgurl/api/data/v9.1/contacts?fetchXml=<xml here>
1

Now lets try to execute this using organization service, note that i am using paging here because the number of records can be more than 5000:


var fetch = @"<fetch version='1.0'  page='##pagenumber##' count='5000' no-lock='true'>
<entity name='contact'>
<attribute name='fullname'/>
<attribute name='contactid'/>
<order attribute='fullname' descending='false'/>
<link-entity name='msdyncrm_segment' from='msdyncrm_segmentid' to='msdyncrm_segmentmemberid' alias='bb'>
<filter type='and'>
<condition attribute='msdyncrm_segmentid' operator='eq' uitype='msdyncrm_segment' value='6f5846d5-e5ae-e911-a84b-000d3a802d92'/>
</filter>
</link-entity>
</entity>
</fetch>";



 var contactss = organizationService.RetrieveMultiple(new FetchExpression(fetch));                  
                        foreach(var contact in contactss.Entities)
                        {
                            var id = contact.ToEntityReference();
                            Console.WriteLine(contact.GetAttributeValue<string>("contactid"));
                        }

result:
2

Apart from this fetch xml there is no way to retrieve members from the segment.  There are some issues and limitation i faced during this operation, will try to share them in another post.

 

I hope this helps!