{"id":37496,"date":"2024-05-11T10:44:00","date_gmt":"2024-05-11T05:14:00","guid":{"rendered":"https:\/\/techmasala.addastudents.com\/dev\/?p=37496"},"modified":"2024-07-21T10:52:43","modified_gmt":"2024-07-21T05:22:43","slug":"how-to-retrieve-records-and-related-data-with-microsoft-dynamics-365-crm-plugins","status":"publish","type":"post","link":"https:\/\/techmasala.addastudents.com\/dev\/how-to-retrieve-records-and-related-data-with-microsoft-dynamics-365-crm-plugins\/","title":{"rendered":"How to Retrieve Records and Related Data with Microsoft Dynamics 365 CRM Plugins."},"content":{"rendered":"\n<p class=\"wp-block-paragraph\">Microsoft Dynamics 365 Customer Relationship Management (CRM) provides powerful tools for managing customer data, automating business processes, and building custom solutions. One essential aspect of CRM development is retrieving records and their related data. In this article, we\u2019ll explore how to achieve this using CRM plugins.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">Understanding CRM Plugins<\/h2>\n\n\n\n<p class=\"wp-block-paragraph\">Before diving into record retrieval, let\u2019s briefly discuss CRM plugins. A plugin is a custom code component that runs in response to specific events within the CRM system. These events can be triggered by user actions (such as creating or updating records) or system events (such as record deletion).<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">Plugins allow developers to extend CRM functionality by adding custom business logic. When it comes to retrieving records, plugins are a valuable tool.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">Retrieving Records<\/h2>\n\n\n\n<p class=\"wp-block-paragraph\">To retrieve records from CRM, we\u2019ll focus on using C# code within a plugin. Here are the steps:<\/p>\n\n\n\n<ol class=\"wp-block-list\">\n<li><strong>Create a New Plugin<\/strong>:\n<ul class=\"wp-block-list\">\n<li>In your CRM solution, create a new plugin assembly.<\/li>\n\n\n\n<li>Define the plugin steps (events) that will trigger your code execution (e.g., \u201cPre-Create,\u201d \u201cPost-Update,\u201d etc.).<br><\/li>\n<\/ul>\n<\/li>\n\n\n\n<li><strong>Connect to CRM Service<\/strong>:\n<ul class=\"wp-block-list\">\n<li>Inside your plugin code, establish a connection to the CRM service using the&nbsp;<code>IOrganizationService<\/code>&nbsp;interface.<\/li>\n\n\n\n<li>You can use the&nbsp;<code>ServiceClient<\/code>&nbsp;class to authenticate and connect.<br><\/li>\n<\/ul>\n<\/li>\n\n\n\n<li><strong>Query Records<\/strong>:\n<ul class=\"wp-block-list\">\n<li>Use the&nbsp;<code>QueryExpression<\/code>&nbsp;class to construct your query.<\/li>\n\n\n\n<li>Specify the entity name, columns to retrieve, and any filtering conditions.<\/li>\n\n\n\n<li>An example query to retrieve all active accounts<\/li>\n<\/ul>\n<\/li>\n<\/ol>\n\n\n\n<pre class=\"wp-block-code\"><code>var query = new QueryExpression(\"account\")\n{\n    ColumnSet = new ColumnSet(\"name\", \"accountnumber\"),\n    Criteria = new FilterExpression\n    {\n        Conditions =\n        {\n            new ConditionExpression(\"statecode\", ConditionOperator.Equal, 0) \/\/ Active records\n        }\n    }\n};<\/code><\/pre>\n\n\n\n<p class=\"wp-block-paragraph\">4. <strong>Execute the Query<\/strong>:<\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li>Use the&nbsp;<code>RetrieveMultiple<\/code>&nbsp;method to execute your query and retrieve records.<\/li>\n\n\n\n<li>Process the results as needed (e.g., display, manipulate, or perform additional actions).<\/li>\n<\/ul>\n\n\n\n<script async src=\"https:\/\/pagead2.googlesyndication.com\/pagead\/js\/adsbygoogle.js?client=ca-pub-5374634985378775\"\n     crossorigin=\"anonymous\"><\/script>\n<!-- responsive -->\n<ins class=\"adsbygoogle\"\n     style=\"display:block\"\n     data-ad-client=\"ca-pub-5374634985378775\"\n     data-ad-slot=\"9690889164\"\n     data-ad-format=\"auto\"\n     data-full-width-responsive=\"true\"><\/ins>\n<script>\n     (adsbygoogle = window.adsbygoogle || []).push({});\n<\/script>\n\n\n\n<h2 class=\"wp-block-heading\">Retrieving Related Data<\/h2>\n\n\n\n<p class=\"wp-block-paragraph\">Now let\u2019s explore how to retrieve related data (associated records):<\/p>\n\n\n\n<ol class=\"wp-block-list\">\n<li><strong>Identify the Relationship<\/strong>:\n<ul class=\"wp-block-list\">\n<li>Determine the relationship between the main entity (e.g., account) and the related entity (e.g., contact, opportunity, etc.).<\/li>\n\n\n\n<li>You can find relationship names in the CRM customization area.<br><\/li>\n<\/ul>\n<\/li>\n\n\n\n<li><strong>Query Related Records<\/strong>:\n<ul class=\"wp-block-list\">\n<li>Construct a query similar to the one above but include the relationship information.<\/li>\n\n\n\n<li>Let&#8217;s consider a scenario where we need to retrieve the details of an account and its related contacts whenever a new opportunity record is created. We&#8217;ll implement a plugin that hooks into the <code>Create<\/code> message of the opportunity entity to achieve this functionality.<\/li>\n<\/ul>\n<\/li>\n<\/ol>\n\n\n\n<pre class=\"wp-block-code\"><code>using Microsoft.Xrm.Sdk;\nusing System;\n\nnamespace RetrieveDataPlugin\n{\n    public class RetrieveDataPlugin : IPlugin\n    {\n        public void Execute(IServiceProvider serviceProvider)\n        {\n            \/\/ Retrieve the execution context\n            IPluginExecutionContext context = (IPluginExecutionContext)serviceProvider.GetService(typeof(IPluginExecutionContext));\n\n            \/\/ Check if the plugin is triggered by the Create message of the Opportunity entity\n            if (context.MessageName.ToLower() == \"create\" &amp;&amp; context.PrimaryEntityName.ToLower() == \"opportunity\")\n            {\n                \/\/ Retrieve the newly created opportunity record\n                Entity opportunity = (Entity)context.InputParameters&#91;\"Target\"];\n\n                \/\/ Retrieve the account associated with the opportunity\n                Guid accountId = ((EntityReference)opportunity.Attributes&#91;\"customerid\"]).Id;\n\n                \/\/ Retrieve account details\n                Entity account = RetrieveAccountDetails(serviceProvider, accountId);\n\n                \/\/ Retrieve related contacts\n                EntityCollection contacts = RetrieveRelatedContacts(serviceProvider, accountId);\n\n                \/\/ Process retrieved data (e.g., logging, further operations)\n                ProcessRetrievedData(account, contacts);\n            }\n        }\n\n        private Entity <mark style=\"background-color:rgba(0, 0, 0, 0)\" class=\"has-inline-color has-luminous-vivid-amber-color\">RetrieveAccountDetails<\/mark>(IServiceProvider serviceProvider, Guid accountId)\n        {\n            \/\/ Implement logic to retrieve account details\n            \/\/ Example:\n            \/\/ IOrganizationService service = (IOrganizationService)serviceProvider.GetService(typeof(IOrganizationService));\n            \/\/ return service.Retrieve(\"account\", accountId, new ColumnSet(\"name\", \"email\"));\n\n            throw new NotImplementedException();\n        }\n\n        private EntityCollection <mark style=\"background-color:rgba(0, 0, 0, 0)\" class=\"has-inline-color has-luminous-vivid-amber-color\">RetrieveRelatedContacts<\/mark>(IServiceProvider serviceProvider, Guid accountId)\n        {\n            \/\/ Implement logic to retrieve related contacts\n            \/\/ Example:\n            \/\/ IOrganizationService service = (IOrganizationService)serviceProvider.GetService(typeof(IOrganizationService));\n            \/\/ QueryExpression query = new QueryExpression(\"contact\");\n            \/\/ query.Criteria.AddCondition(\"parentcustomerid\", ConditionOperator.Equal, accountId);\n            \/\/ return service.RetrieveMultiple(query);\n\n            throw new NotImplementedException();\n        }\n\n        private void <mark style=\"background-color:rgba(0, 0, 0, 0)\" class=\"has-inline-color has-luminous-vivid-amber-color\">ProcessRetrievedData<\/mark>(Entity account, EntityCollection contacts)\n        {\n            \/\/ Implement logic to process retrieved data\n            \/\/ Example:\n            \/\/ Log account and contact details, perform additional operations, etc.\n        }\n    }\n}\n<\/code><\/pre>\n\n\n\n<p class=\"wp-block-paragraph\">Retrieving records and related data in Microsoft Dynamics 365 CRM plugins is essential for building robust solutions. By understanding the basics and leveraging the CRM SDK, you can create efficient and effective code to meet your business needs.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>Microsoft Dynamics 365 Customer Relationship Management (CRM) provides powerful tools for managing customer data, automating business processes, and building custom solutions. One essential aspect of CRM development is retrieving records and their related data. In this article, we\u2019ll explore how to achieve this using CRM plugins. Understanding CRM Plugins Before diving into record retrieval, let\u2019s [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":37503,"comment_status":"open","ping_status":"open","sticky":true,"template":"","format":"standard","meta":{"footnotes":""},"categories":[70,17,72],"tags":[912,891,910,909,911],"class_list":["post-37496","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-learn-technology-free","category-learn-microsoft-dynamics-crm","category-tech","tag-crm-2","tag-dynamics-365-crm","tag-plugins","tag-related-data","tag-retrieve-records"],"_links":{"self":[{"href":"https:\/\/techmasala.addastudents.com\/dev\/wp-json\/wp\/v2\/posts\/37496","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/techmasala.addastudents.com\/dev\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/techmasala.addastudents.com\/dev\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/techmasala.addastudents.com\/dev\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/techmasala.addastudents.com\/dev\/wp-json\/wp\/v2\/comments?post=37496"}],"version-history":[{"count":7,"href":"https:\/\/techmasala.addastudents.com\/dev\/wp-json\/wp\/v2\/posts\/37496\/revisions"}],"predecessor-version":[{"id":37568,"href":"https:\/\/techmasala.addastudents.com\/dev\/wp-json\/wp\/v2\/posts\/37496\/revisions\/37568"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/techmasala.addastudents.com\/dev\/wp-json\/wp\/v2\/media\/37503"}],"wp:attachment":[{"href":"https:\/\/techmasala.addastudents.com\/dev\/wp-json\/wp\/v2\/media?parent=37496"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/techmasala.addastudents.com\/dev\/wp-json\/wp\/v2\/categories?post=37496"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/techmasala.addastudents.com\/dev\/wp-json\/wp\/v2\/tags?post=37496"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}