{"id":41271,"date":"2025-05-19T15:09:55","date_gmt":"2025-05-19T09:39:55","guid":{"rendered":"https:\/\/www.inogic.com\/blog\/?p=41271"},"modified":"2025-05-19T15:09:55","modified_gmt":"2025-05-19T09:39:55","slug":"how-to-create-and-modify-a-segment-in-customer-insights-journeys-using-the-web-api","status":"publish","type":"post","link":"https:\/\/www.inogic.com\/blog\/2025\/05\/how-to-create-and-modify-a-segment-in-customer-insights-journeys-using-the-web-api\/","title":{"rendered":"How to Create and Modify a Segment in Customer Insights &#8211; Journeys Using the Web API"},"content":{"rendered":"<p><img decoding=\"async\" class=\"alignnone size-full wp-image-41279\" src=\"https:\/\/www.inogic.com\/blog\/wp-content\/uploads\/2025\/05\/Web-API.png\" alt=\"Web API\" width=\"1925\" height=\"1100\" srcset=\"https:\/\/www.inogic.com\/blog\/wp-content\/uploads\/2025\/05\/Web-API.png 1925w, https:\/\/www.inogic.com\/blog\/wp-content\/uploads\/2025\/05\/Web-API-300x171.png 300w, https:\/\/www.inogic.com\/blog\/wp-content\/uploads\/2025\/05\/Web-API-1024x585.png 1024w, https:\/\/www.inogic.com\/blog\/wp-content\/uploads\/2025\/05\/Web-API-768x439.png 768w, https:\/\/www.inogic.com\/blog\/wp-content\/uploads\/2025\/05\/Web-API-1536x878.png 1536w, https:\/\/www.inogic.com\/blog\/wp-content\/uploads\/2025\/05\/Web-API-660x377.png 660w\" sizes=\"(max-width: 1925px) 100vw, 1925px\" \/><\/p>\n<p>In this blog, we will walk through the process of creating a new segment in Real-Time Journeys and updating the segment by adding members using the Microsoft Dynamics 365 Web API. (<a href=\"https:\/\/learn.microsoft.com\/en-us\/dynamics365\/customer-insights\/journeys\/real-time-marketing-segment-based-journey\" target=\"_blank\" rel=\"noopener\">Create a segment-based journey<\/a>)<\/p>\n<p>A <strong>segment<\/strong> is a group of contacts or leads, either dynamically updated or statically defined, that you target in your real-time journey. It helps identify the audience for marketing activities such as emails, SMS, push notifications, and more.<\/p>\n<p>While segments can be created manually through the UI, there are many cases where creating them programmatically using the Web API is far more powerful, especially for developers or integrated systems.<\/p>\n<p>When a user triggers an event, you can instantly add contacts and leads to a specific segment using the Web API\u2014automatically, or you can create a new segment list with no manual intervention. This ensures that the right contacts are included in real-time journeys as soon as the event happens, streamlining the process and keeping everything up-to-date without any extra effort.<\/p>\n<p><strong>Entities Required for Segment Creation<\/strong><\/p>\n<p>To create a static segment in Real-Time Journeys, you need to create records in the following two essential entities:<\/p>\n<ol>\n<li><strong>Segment Definition (msdynmkt_segmentdefinitions)<\/strong>:<\/li>\n<\/ol>\n<ul>\n<li>A segment definition represents a marketing audience or a targeted customer list.<\/li>\n<li>Include metadata like include members, name, type.<\/li>\n<\/ul>\n<ol start=\"2\">\n<li><strong>Segment(msdynmkt_segments)<\/strong>:<\/li>\n<\/ol>\n<ul>\n<li>This table determines the customer group targeted in Customer Journeys and Real-Time Journeys.<\/li>\n<\/ul>\n<p><strong>Please find below the steps to create a real-time segment:<\/strong><\/p>\n<p><strong>1. Call the function:<\/strong> Call the &#8216;createSegment&#8217; function with &#8216;recordIds&#8217; as a parameter, where &#8216;recordIds&#8217; contains the GUIDs of the records you want to add to the segments.<\/p>\n<p>recordIds = &#8220;guid1&#8221;, &#8220;guid2&#8221;;<em>\/\/ Replace with actual GUIDs<\/em><\/p>\n<p><strong>2. Record creation:<\/strong> You have to create the 2 entities records i.e. <strong>Segment Definition,<\/strong> <strong>Segment <\/strong><strong>entity<\/strong><strong>.<\/strong><\/p>\n<pre class=\"lang:css gutter:true start:1\">createSegment(recordIds) {\r\n\r\nvar Seg: any = null\r\n\r\nvar ML: any = null;\r\n\r\ntry {\r\n\r\n\/\/Segments are used to define and organize target customer lists for marketing campaigns.\r\n\r\nML = new Object();\r\n\r\nML = {\r\n\r\n\"msdynmkt_name\": \"New Customers in Last 30 Days\",\r\n\r\n\"msdynmkt_includedmembers\": recordIds, \/\/Guid of the records\r\n\r\n\"statuscode\": 723270001, \/\/72327001-Live\r\n\r\n\"statecode\": 0, \/\/0 = Active, 1 = Inactive\r\n\r\n}\r\n\r\nXrm.WebApi.createRecord (\"msdynmkt_segmentdefinition\", ML).then(\r\n\r\nfunction success(result:any) {\r\n\r\n\/\/Segment definition record has been created successfully. We will now initiate the creation of a segment entity record.\r\n\r\nSeg = new Object();\r\n\r\nSeg = {\r\n\r\n\"msdynmkt_displayname\": \"New Customers in Last 30 Days\",\r\n\r\n\"msdynmkt_sourcesegmentuid\": result.id,\r\n\r\n\"msdynmkt_type\": 11,\/\/10 = Static segment 11 = Dynamic segment\r\n\r\n\"msdynmkt_source\": 12,\/\/12: Customer Insights - Journeys\r\n\r\n\"msdynmkt_membercount\": 3, \/\/length of the contact list\r\n\r\n\"msdynmkt_baseentitylogicalname\":\u201dcontact\u201d, \/\/Pass the entity type\r\n\r\n}\r\n\r\nXrm.WebApi.createRecord('msdynmkt_segment', Seg).then(\r\n\r\nfunction success(segResult:any) {\r\n\r\nif ((segResult.message != undefined)) {\r\n\r\n{\r\n\r\n\/\/Segment record has been created Successfully\r\n\r\nconsole.log(\"Segment Created Successfully\")\r\n\r\n}\r\n\r\n}\r\n\r\n\u00a0\r\n\r\n})\r\n\r\n},\r\n\r\nfunction (error:any) {\r\n\r\nconsole.log(\"Error: \" + error.message);\r\n\r\n}\r\n\r\n}\r\n\r\n);\r\n\r\ncatch (e: any) {\r\n\r\nconsole.log(e.message)\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 }\r\n\r\n}<\/pre>\n<p><strong>3. Call Method:<\/strong> The \u2018WebApi.createRecord\u2019 method in Dynamics 365 is a client-side JavaScript function used to create new records in the Common Data Service (Dataverse). It is especially handy for dynamically generating records within model-driven apps using JavaScript.<\/p>\n<p><strong>4. Final Output:<\/strong> After creation of<strong> the record successfully,<\/strong> the segment record will be created as shown in the screenshot below.<\/p>\n<p><img decoding=\"async\" class=\"alignnone size-full wp-image-41273\" src=\"https:\/\/www.inogic.com\/blog\/wp-content\/uploads\/2025\/05\/2Journeys-Using-the-Web-API-1.png\" alt=\"Journeys Using the Web API\" width=\"1433\" height=\"767\" srcset=\"https:\/\/www.inogic.com\/blog\/wp-content\/uploads\/2025\/05\/2Journeys-Using-the-Web-API-1.png 1433w, https:\/\/www.inogic.com\/blog\/wp-content\/uploads\/2025\/05\/2Journeys-Using-the-Web-API-1-300x161.png 300w, https:\/\/www.inogic.com\/blog\/wp-content\/uploads\/2025\/05\/2Journeys-Using-the-Web-API-1-1024x548.png 1024w, https:\/\/www.inogic.com\/blog\/wp-content\/uploads\/2025\/05\/2Journeys-Using-the-Web-API-1-768x411.png 768w, https:\/\/www.inogic.com\/blog\/wp-content\/uploads\/2025\/05\/2Journeys-Using-the-Web-API-1-660x353.png 660w\" sizes=\"(max-width: 1433px) 100vw, 1433px\" \/> <strong>Note: Currently, you can add only 100 members in the \u201cinclude members\u201d field of the segment.<\/strong> <strong>Update Segment:<\/strong> When working with Real-Time Marketing in Dynamics 365, you may need to update the &#8220;Include Members&#8221; field of a segment dynamically. <strong>Here\u2019s a refined approach to creating a real-time segment.<\/strong><\/p>\n<ul>\n<li><strong>Segment ID: <\/strong>Before updating the &#8216;Include Members&#8217; attribute, obtain the ID<strong> (msdynmkt_segmentdefinitionid) <\/strong>of the static segment you intend to change. Use the GET method to fetch the segment details.<\/li>\n<li><strong>Accounts Data<\/strong>: Have a list of account IDs (GUIDs) to add to the <strong>Include Members<\/strong><\/li>\n<\/ul>\n<p>Example Account IDs: [ &#8221; a0a59f03-1ebd-ef11-b8e8-6045bde7dfac, a0a59f03-1ebd-ef11-b8e8-6045bde7dfac&#8221;]\n<ul>\n<li><strong>Access of the Web API\u2019s:<\/strong> Use the \u201cWebApi.retrieveMultipleRecords\u201d method to fetch the <strong>Segment Definition ID<\/strong> (msdynmkt_segmentdefinitionid) of the target segment.<\/li>\n<\/ul>\n<pre class=\"lang:css gutter:true start:1\">\/\/On update button click you can call this button\r\nasync retrieveSegment( segmentdefinationId:any, selectedRecords:any){\r\n\r\nvar thisObj=this;\r\n\r\n\/\/ segmentdefinationId- Id of segment definition entity\r\n\r\n\/\/ selectedRecords- The record you wish to add in the include member for example a0a59f03-1ebd-ef11-b8e8-6045bde7dfac, a0a59f03-1ebd-ef11-b8e8-6045bde7dfac\r\n\r\nvar tempRecords:any= [];\r\n\r\nvar fetchXml = \"\";\r\n\r\nfetchXml = \"&lt;fetch version='1.0' output-format='xml-platform' mapping='logical' distinct='false'&gt;\" +\r\n\r\n\"&lt;entity name='msdynmkt_segmentdefinition'&gt;\" +\r\n\r\n\"&lt;attribute name='msdynmkt_segmentdefinitionid' \/&gt;\" +\r\n\r\n\"&lt;attribute name='msdynmkt_name' \/&gt;\" +\r\n\r\n\"&lt;attribute name='msdynmkt_includedmembers' \/&gt;\" +\r\n\r\n\"&lt;attribute name='createdon' \/&gt;\" +\r\n\r\n\"&lt;order attribute='msdynmkt_name' descending='false' \/&gt;\" +\r\n\r\n\"&lt;filter type='and'&gt;\" +\r\n\r\n\"&lt;condition attribute='msdynmkt_segmentdefinitionid' operator='eq' uiname='New Segment'\u00a0\u00a0\u00a0 uitype='msdynmkt_segmentdefinition' value='\"+segmentdefinationId+\"'\/&gt;\" +\r\n\r\n\"&lt;\/filter&gt;\" +\r\n\r\n\"&lt;\/entity&gt;\" +\r\n\r\n\"&lt;\/fetch&gt;\" ;\r\n\r\n\u00a0\r\n\r\nlet query: any = '?fetchXml=' + encodeURIComponent(fetchXml)\r\n\r\nawait Xrm.WebApi.retrieveMultipleRecords(\"msdynmkt_segmentdefinition\", query).then(function (results: any) {\r\n\r\n\u00a0\r\n\r\nvar members: any = results.entities[0].msdynmkt_includedmembers + \",\" + tempRecords;\r\n\r\nvar finalmembers = members;\r\n\r\nthisObj.updateSegment(finalmembers, results.entities[0].msdynmkt_segmentdefinitionid)\r\n\r\n},\r\n\r\nfunction(error:any){\r\n\r\nconsole.log(error.message)\r\n\r\n}\r\n\r\n)\r\n\r\n}\r\n\r\n\u00a0\r\n\r\n\/\/Update the segment\r\n\r\nasync updateSegment(response: any, segmentId: any) {\r\n\r\ntry {\r\n\r\nconst memberIds = {\r\n\r\n\"msdynmkt_includedmembers\": response \/\/ pass the Guids\r\n\r\n};\r\n\r\nawait Xrm.WebApi.updateRecord(\"msdynmkt_segmentdefinition\", segmentId, memberIds)\r\n\r\n.then(function (response: any) {\r\n\r\nconsole.log(\"Segment List Updated Successfully\")\r\n\r\n},\r\n\r\nfunction (error: any) {\r\n\r\nconsole.log(\"Error: \" + error.message);\r\n\r\n}\r\n\r\n)\r\n\r\n} catch (e: any) {\r\n\r\nconsole.log(e.message)\r\n\r\n}\r\n\r\n}<\/pre>\n<p><strong>Final Output: <\/strong>After updating the segment, the new members will be added to your existing segment as shown in the screenshot below:<\/p>\n<p><img decoding=\"async\" class=\"alignnone size-full wp-image-41272\" src=\"https:\/\/www.inogic.com\/blog\/wp-content\/uploads\/2025\/05\/2Journeys-Using-the-Web-API.png\" alt=\"Journeys Using the Web API\" width=\"1433\" height=\"767\" srcset=\"https:\/\/www.inogic.com\/blog\/wp-content\/uploads\/2025\/05\/2Journeys-Using-the-Web-API.png 1433w, https:\/\/www.inogic.com\/blog\/wp-content\/uploads\/2025\/05\/2Journeys-Using-the-Web-API-300x161.png 300w, https:\/\/www.inogic.com\/blog\/wp-content\/uploads\/2025\/05\/2Journeys-Using-the-Web-API-1024x548.png 1024w, https:\/\/www.inogic.com\/blog\/wp-content\/uploads\/2025\/05\/2Journeys-Using-the-Web-API-768x411.png 768w, https:\/\/www.inogic.com\/blog\/wp-content\/uploads\/2025\/05\/2Journeys-Using-the-Web-API-660x353.png 660w\" sizes=\"(max-width: 1433px) 100vw, 1433px\" \/><\/p>\n<h3><strong>Conclusion<\/strong><\/h3>\n<p>By following these steps, users can effectively manage and utilize real-time marketing segments to drive targeted engagements and personalized campaigns within Microsoft Dynamics 365 CRM. This approach enhances audience segmentation, ensuring marketing efforts reach the right individuals at the right time.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>In this blog, we will walk through the process of creating a new segment in Real-Time Journeys and updating the segment by adding members using the Microsoft Dynamics 365 Web API. (Create a segment-based journey) A segment is a group of contacts or leads, either dynamically updated or statically defined, that you target in your\u2026 <span class=\"read-more\"><a href=\"https:\/\/www.inogic.com\/blog\/2025\/05\/how-to-create-and-modify-a-segment-in-customer-insights-journeys-using-the-web-api\/\">Read More &raquo;<\/a><\/span><\/p>\n","protected":false},"author":15,"featured_media":0,"comment_status":"closed","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"om_disable_all_campaigns":false,"_monsterinsights_skip_tracking":false,"_monsterinsights_sitenote_active":false,"_monsterinsights_sitenote_note":"","_monsterinsights_sitenote_category":0,"footnotes":""},"categories":[2361,65],"tags":[],"class_list":["post-41271","post","type-post","status-publish","format-standard","hentry","category-technical","category-webapi"],"aioseo_notices":[],"_links":{"self":[{"href":"https:\/\/www.inogic.com\/blog\/wp-json\/wp\/v2\/posts\/41271","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.inogic.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.inogic.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.inogic.com\/blog\/wp-json\/wp\/v2\/users\/15"}],"replies":[{"embeddable":true,"href":"https:\/\/www.inogic.com\/blog\/wp-json\/wp\/v2\/comments?post=41271"}],"version-history":[{"count":0,"href":"https:\/\/www.inogic.com\/blog\/wp-json\/wp\/v2\/posts\/41271\/revisions"}],"wp:attachment":[{"href":"https:\/\/www.inogic.com\/blog\/wp-json\/wp\/v2\/media?parent=41271"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.inogic.com\/blog\/wp-json\/wp\/v2\/categories?post=41271"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.inogic.com\/blog\/wp-json\/wp\/v2\/tags?post=41271"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}