{"id":26718,"date":"2021-02-10T10:52:05","date_gmt":"2021-02-10T10:52:05","guid":{"rendered":"https:\/\/www.inogic.com\/blog\/?p=26718"},"modified":"2021-02-10T10:58:19","modified_gmt":"2021-02-10T10:58:19","slug":"using-environment-variables-in-dynamics-365-crm-part-2","status":"publish","type":"post","link":"https:\/\/www.inogic.com\/blog\/2021\/02\/using-environment-variables-in-dynamics-365-crm-part-2\/","title":{"rendered":"Using Environment Variables in Dynamics 365 CRM \u2013 Part 2"},"content":{"rendered":"<h2><strong>Introduction<\/strong><\/h2>\n<p style=\"text-align: justify;\">In our <a href=\"https:\/\/www.inogic.com\/blog\/2021\/02\/using-environment-variables-in-dynamics-365-crm-part-1\/\" target=\"_blank\" rel=\"noopener noreferrer\">previous blog<\/a>, we understood the background and the purpose of the Environment Variable.<\/p>\n<p>In this blog we will see how to use it in our day to day solutions.<\/p>\n<p style=\"text-align: justify;\">As we make use of Plugins, Workflows, Scripts, Power Automate, etc. in our day-to-day solutions, let\u2019s see how to access and use the environment variables in each one of them.<\/p>\n<h2><strong><u>Plugins\/Workflows<\/u><\/strong><\/h2>\n<p style=\"text-align: justify;\">In our <a href=\"https:\/\/www.inogic.com\/blog\/2021\/02\/using-environment-variables-in-dynamics-365-crm-part-1\/\" target=\"_blank\" rel=\"noopener noreferrer\">previous blog<\/a>, we created an Environment Variable with the schema name &#8216;new_apiurl&#8217;. Now, there could be Plugins and Workflows where we might need to use the value stored. Using the below methods we can get the value from the environment variable:<\/p>\n<p>\/\/\/&lt;summary&gt;<\/p>\n<p>\/\/\/This method will be used to get the details of<\/p>\n<p>\/\/\/ Environment Variable Definition with the schema name new_apiurl<\/p>\n<p>\/\/\/&lt;\/summary&gt;<\/p>\n<p>\/\/\/&lt;param name=&#8221;service&#8221;&gt;&lt;\/param&gt;<\/p>\n<p>\/\/\/&lt;param name=&#8221;context&#8221;&gt;&lt;\/param&gt;<\/p>\n<p>\/\/\/&lt;param name=&#8221;tracing&#8221;&gt;&lt;\/param&gt;<\/p>\n<p>\/\/\/&lt;returns&gt;&lt;\/returns&gt;<\/p>\n<p>privatestringGetEnvironmentVariableDefinition(IOrganizationService service,ITracingService tracing)<\/p>\n<p>{<\/p>\n<p>#region (*Local Variable*)<\/p>\n<p>stringfunctionName = &#8220;GetVariableDefinition&#8221;;<\/p>\n<p>stringfetchXml = string.Empty;<\/p>\n<p>EntityCollection definition = null;<\/p>\n<p>string value = string.Empty;<\/p>\n<p>#endregion<\/p>\n<p>try<\/p>\n<p>{<\/p>\n<p>\/\/Trace<\/p>\n<p>tracing.Trace(&#8220;Inside &#8221; + functionName);<\/p>\n<p>\/\/Create the fetch xml query<\/p>\n<p>fetchXml = &#8220;&lt;fetch version=&#8217;1.0&#8242; output-format=&#8217;xml-platform&#8217; mapping=&#8217;logical&#8217; distinct=&#8217;false&#8217;&gt;&#8221; +<\/p>\n<p>&#8220;&lt;entity name=&#8217;environmentvariabledefinition&#8217;&gt;&#8221; +<\/p>\n<p>&#8220;&lt;attribute name=&#8217;environmentvariabledefinitionid&#8217; \/&gt;&#8221; +<\/p>\n<p>&#8220;&lt;attribute name=&#8217;schemaname&#8217; \/&gt;&#8221; +<\/p>\n<p>&#8220;&lt;attribute name=&#8217;createdon&#8217; \/&gt;&#8221; +<\/p>\n<p>&#8220;&lt;order attribute=&#8217;schemaname&#8217; descending=&#8217;false&#8217; \/&gt;&#8221; +<\/p>\n<p>&#8220;&lt;filter type=&#8217;and&#8217;&gt;&#8221; +<\/p>\n<p>&#8220;&lt;condition attribute=&#8217;schemaname&#8217; operator=&#8217;eq&#8217; value=&#8217;new_apiurl&#8217; \/&gt;&#8221; +<\/p>\n<p>&#8220;&lt;\/filter&gt;&#8221; +<\/p>\n<p>&#8220;&lt;\/entity&gt;&#8221; +<\/p>\n<p>&#8220;&lt;\/fetch&gt;&#8221;;<\/p>\n<p>\/\/Retrieve the Environment Variable Definition with schemaname = new_apiurl<\/p>\n<p>definition = service.RetrieveMultiple(newFetchExpression(fetchXml));<\/p>\n<p>\/\/If the definition is found<\/p>\n<p>if (definiton != null&amp;&amp;definiton.Entities.Count&gt; 0)<\/p>\n<p>{<\/p>\n<p>\/\/Call the method to get the Current Value associated to the Environment Variable Definition<\/p>\n<p>value = GetEnvironmentVariableValue(definiton.Entities[0], service, context, tracing);<\/p>\n<p>}<\/p>\n<p>}<\/p>\n<p>catch (InvalidPluginExecutionExceptionipex)<\/p>\n<p>{<\/p>\n<p>thrownewInvalidPluginExecutionException(functionName + ipex.Message);<\/p>\n<p>}<\/p>\n<p>catch (FaultExceptionfex)<\/p>\n<p>{<\/p>\n<p>thrownewFaultException(functionName + fex.Message);<\/p>\n<p>}<\/p>\n<p>catch (Exception ex)<\/p>\n<p>{<\/p>\n<p>thrownewException(functionName + ex.Message);<\/p>\n<p>}<\/p>\n<p>return value;<\/p>\n<p>}<\/p>\n<p>\/\/\/&lt;summary&gt;<\/p>\n<p>\/\/\/This method will be used to get the Current value<\/p>\n<p>\/\/\/ associated to the environment definition<\/p>\n<p>\/\/\/&lt;\/summary&gt;<\/p>\n<p>\/\/\/&lt;param name=&#8221;entityDefinition&#8221;&gt;&lt;\/param&gt;<\/p>\n<p>\/\/\/&lt;param name=&#8221;service&#8221;&gt;&lt;\/param&gt;<\/p>\n<p>\/\/\/&lt;param name=&#8221;context&#8221;&gt;&lt;\/param&gt;<\/p>\n<p>\/\/\/&lt;param name=&#8221;tracing&#8221;&gt;&lt;\/param&gt;<\/p>\n<p>\/\/\/&lt;returns&gt;&lt;\/returns&gt;<\/p>\n<p>privatestringGetEnvironmentVariableValue(EntityentityDefinition, IOrganizationService service, ITracingService tracing)<\/p>\n<p>{<\/p>\n<p>#region (*Local Variable*)<\/p>\n<p>stringfunctionName = &#8220;GetEvValue&#8221;;<\/p>\n<p>stringfetchXml = string.Empty;<\/p>\n<p>EntityCollection values = null;<\/p>\n<p>stringcurrentValue = string.Empty;<\/p>\n<p>#endregion<\/p>\n<p>try<\/p>\n<p>{<\/p>\n<p>\/\/Create the fetch xml query<\/p>\n<p>fetchXml = &#8220;&lt;fetch version=&#8217;1.0&#8242; output-format=&#8217;xml-platform&#8217; mapping=&#8217;logical&#8217; distinct=&#8217;false&#8217;&gt;&#8221; +<\/p>\n<p>&#8220;&lt;entity name=&#8217;environmentvariablevalue&#8217;&gt;&#8221; +<\/p>\n<p>&#8220;&lt;attribute name=&#8217;environmentvariablevalueid&#8217; \/&gt;&#8221; +<\/p>\n<p>&#8220;&lt;attribute name=&#8217;value&#8217; \/&gt;&#8221; +<\/p>\n<p>&#8220;&lt;attribute name=&#8217;createdon&#8217; \/&gt;&#8221; +<\/p>\n<p>&#8220;&lt;attribute name=&#8217;schemaname&#8217; \/&gt;&#8221; +<\/p>\n<p>&#8220;&lt;attribute name=&#8217;environmentvariabledefinitionid&#8217; \/&gt;&#8221; +<\/p>\n<p>&#8220;&lt;order attribute=&#8217;createdon&#8217; descending=&#8217;true&#8217; \/&gt;&#8221; +<\/p>\n<p>&#8220;&lt;filter type=&#8217;and&#8217;&gt;&#8221; +<\/p>\n<p>&#8220;&lt;condition attribute=&#8217;environmentvariabledefinitionid&#8217; operator=&#8217;eq&#8217; value='&#8221; + entityDefinition.Id + &#8220;&#8216; \/&gt;&#8221; +<\/p>\n<p>&#8220;&lt;\/filter&gt;&#8221; +<\/p>\n<p>&#8220;&lt;\/entity&gt;&#8221; +<\/p>\n<p>&#8220;&lt;\/fetch&gt;&#8221;;<\/p>\n<p>\/\/Retrieve the Environment Variable value associated to the Environment Variable Definition<\/p>\n<p>values = service.RetrieveMultiple(newFetchExpression(fetchXml));<\/p>\n<p>if (values != null&amp;&amp;values.Entities.Count&gt; 0)<\/p>\n<p>{<\/p>\n<p>\/\/Get the Current Value associated to the Environment Variable Definition<\/p>\n<p>currentValue = values.Entities[0].GetAttributeValue&lt;string&gt;(&#8220;value&#8221;);<\/p>\n<p>}<\/p>\n<p>}<\/p>\n<p>catch (InvalidPluginExecutionExceptionipex)<\/p>\n<p>{<\/p>\n<p>thrownewInvalidPluginExecutionException(functionName + ipex.Message);<\/p>\n<p>}<\/p>\n<p>catch (FaultExceptionfex)<\/p>\n<p>{<\/p>\n<p>thrownewFaultException(functionName + fex.Message);<\/p>\n<p>}<\/p>\n<p>catch (Exception ex)<\/p>\n<p>{<\/p>\n<p>thrownewException(functionName + ex.Message);<\/p>\n<p>}<\/p>\n<p>returncurrentValue;<\/p>\n<p>}<\/p>\n<h2><strong><u>Scripts<\/u><\/strong><\/h2>\n<p>Similarly, we can also query the value of environment variable in a script as shown below -here, we have created a method where we are using the OData query to query the Environment Variable records.<\/p>\n<p>getValue = (executionContext: Xrm.Events.EventContext): void =&gt; {<\/p>\n<p>\/\/Local Variables<\/p>\n<p>letfunctionName: string = &#8220;onLoad&#8221;;<\/p>\n<p>let query: string = &#8220;&#8221;;<\/p>\n<p>letenvValue: string = &#8220;&#8221;;<\/p>\n<p>letalertStrings: any = null;<\/p>\n<p>letalertOptions: any = null;<\/p>\n<p>try {<\/p>\n<p>\/\/Create the query to get the Environment Variable Definition with schemaname &#8220;new_apiurl&#8221;<\/p>\n<p>query = &#8220;?$filter=schemanameeq &#8216;new_apiurl'&#8221;;<\/p>\n<p>\/\/Retrieve the Environment Variable Definition with schemaname &#8220;new_apiurl&#8221;<\/p>\n<p>Xrm.WebApi.retrieveMultipleRecords(&#8220;environmentvariabledefinition&#8221;, query).then(<\/p>\n<p>function (definition) {<\/p>\n<p>\/\/Create the query to get the Environment Variable Value associated to the definition with schemaname &#8220;new_apiurl&#8221;<\/p>\n<p>query = &#8220;?$filter=_environmentvariabledefinitionid_valueeq &#8216;&#8221; + definition.entities[0][&#8220;environmentvariabledefinitionid&#8221;] + &#8220;&#8216;&#8221;;<\/p>\n<p>Xrm.WebApi.retrieveMultipleRecords(&#8220;environmentvariablevalue&#8221;, query).then(<\/p>\n<p>function (value) {<\/p>\n<p>envValue = value.entities[0][&#8220;value&#8221;];<\/p>\n<p>},<\/p>\n<p>function (error) {<\/p>\n<p>alertStrings = { text: functionName + &#8221; :: &#8221; + ex.message };<\/p>\n<p>Xrm.Navigation.openAlertDialog(alertStrings);<\/p>\n<p>});<\/p>\n<p>},<\/p>\n<p>function (error) {<\/p>\n<p>alertStrings = { text: functionName + &#8221; :: &#8221; + ex.message };<\/p>\n<p>Xrm.Navigation.openAlertDialog(alertStrings);<\/p>\n<p>});<\/p>\n<p>}<\/p>\n<p>catch (ex) {<\/p>\n<p>alertStrings = { text: functionName + &#8221; :: &#8221; + ex.message };<\/p>\n<p>Xrm.Navigation.openAlertDialog(alertStrings);<\/p>\n<p>}<\/p>\n<p>}<\/p>\n<h2><strong><u>Power Automate<\/u><\/strong><\/h2>\n<p style=\"text-align: justify;\">We can even use the Environment Variables in the Power Automate as below.<\/p>\n<ul>\n<li>We selected the <strong>List records <\/strong>action from\u00a0<strong>Common Data Service <\/strong>connector<a href=\"https:\/\/www.inogic.com\/blog\/wp-content\/uploads\/2021\/02\/1-use-environment-variables.jpeg\" target=\"_blank\" rel=\"noopener noreferrer\"><img decoding=\"async\" class=\"aligncenter wp-image-26729 size-full\" src=\"https:\/\/www.inogic.com\/blog\/wp-content\/uploads\/2021\/02\/1-use-environment-variables.jpeg\" alt=\"Using Environment Variables\" width=\"610\" height=\"532\" srcset=\"https:\/\/www.inogic.com\/blog\/wp-content\/uploads\/2021\/02\/1-use-environment-variables.jpeg 610w, https:\/\/www.inogic.com\/blog\/wp-content\/uploads\/2021\/02\/1-use-environment-variables-300x262.jpeg 300w\" sizes=\"(max-width: 610px) 100vw, 610px\" \/><\/a><\/li>\n<\/ul>\n<p>Here we have used the filter query <strong><em>schemanameeq &#8216;new_apiurl&#8217;<\/em><\/strong>and set the <strong><em>Top Count =1<\/em><\/strong>to get the Environment Variable Definition with schema name as \u201c<strong><em>new_apiurl<\/em><\/strong>\u201d.<\/p>\n<ul>\n<li>Then using the value of the Environment Variable Definition retrieved in the previous step, we retrieved the associated <strong>Environment Variable Value<\/strong> using the filter query as <strong><em>schemanameeq &#8216;new_apiurl'&lt;expression&gt;\u2019<\/em><\/strong> here the <strong><em>&lt;expression&gt;<\/em><\/strong>will be <strong><em>outputs(&#8216;EVDefinitions&#8217;)[&#8216;body\/value&#8217;]?[0]?[&#8216;environmentvariabledefinitionid&#8217;].<\/em><\/strong>Here as well we have kept the Top Count as 1 as there can be only one Environment Variable Value associated to a Definition.<\/li>\n<\/ul>\n<p><a href=\"https:\/\/www.inogic.com\/blog\/wp-content\/uploads\/2021\/02\/2-use-environment-variables.jpeg\" target=\"_blank\" rel=\"noopener noreferrer\"><img decoding=\"async\" class=\"aligncenter wp-image-26728 size-full\" src=\"https:\/\/www.inogic.com\/blog\/wp-content\/uploads\/2021\/02\/2-use-environment-variables.jpeg\" alt=\"Using Environment Variables\" width=\"892\" height=\"537\" srcset=\"https:\/\/www.inogic.com\/blog\/wp-content\/uploads\/2021\/02\/2-use-environment-variables.jpeg 892w, https:\/\/www.inogic.com\/blog\/wp-content\/uploads\/2021\/02\/2-use-environment-variables-300x181.jpeg 300w, https:\/\/www.inogic.com\/blog\/wp-content\/uploads\/2021\/02\/2-use-environment-variables-768x462.jpeg 768w, https:\/\/www.inogic.com\/blog\/wp-content\/uploads\/2021\/02\/2-use-environment-variables-660x397.jpeg 660w\" sizes=\"(max-width: 892px) 100vw, 892px\" \/><\/a><\/p>\n<ul>\n<li>Finally, as can be seen from the screenshot below, we have used the retrieved Environment Variable Value (i.e. the value of the variable \u201c<em>new_apiurl<\/em>\u201d) in the URI of the HTTP request connector as <strong><em>outputs(&#8216;EVValue&#8217;)[&#8216;body\/value&#8217;]?[0]?[&#8216;value&#8217;]<\/em><\/strong> .<\/li>\n<\/ul>\n<p><a href=\"https:\/\/www.inogic.com\/blog\/wp-content\/uploads\/2021\/02\/3-use-environment-variables.jpeg\" target=\"_blank\" rel=\"noopener noreferrer\"><img decoding=\"async\" class=\"aligncenter wp-image-26727 size-full\" src=\"https:\/\/www.inogic.com\/blog\/wp-content\/uploads\/2021\/02\/3-use-environment-variables.jpeg\" alt=\"Using Environment Variables\" width=\"604\" height=\"504\" srcset=\"https:\/\/www.inogic.com\/blog\/wp-content\/uploads\/2021\/02\/3-use-environment-variables.jpeg 604w, https:\/\/www.inogic.com\/blog\/wp-content\/uploads\/2021\/02\/3-use-environment-variables-300x250.jpeg 300w\" sizes=\"(max-width: 604px) 100vw, 604px\" \/><\/a><\/p>\n<h2><strong><u>Security<\/u><\/strong><\/h2>\n<p style=\"text-align: justify;\">One thing to be noted is that to make use of the environment variables in any of the above, the user in context must have the privileges to access both the entities i.e. <strong>Environment Variable Definition<\/strong> and <strong>Environment Variable Value<\/strong>. The privilege assigned to the entity <strong>Environment Variable Definition<\/strong> gets inherited by the entity <strong>Environment Variable Value<\/strong> as well. So you only see the entity <strong>Environment Variable Definition<\/strong> while assigning the privilege from the security role as below.<\/p>\n<p><a href=\"https:\/\/www.inogic.com\/blog\/wp-content\/uploads\/2021\/02\/4-use-environment-variables.jpeg\" target=\"_blank\" rel=\"noopener noreferrer\"><img decoding=\"async\" class=\"aligncenter wp-image-26726 size-full\" src=\"https:\/\/www.inogic.com\/blog\/wp-content\/uploads\/2021\/02\/4-use-environment-variables.jpeg\" alt=\"Using Environment Variables\" width=\"1014\" height=\"373\" srcset=\"https:\/\/www.inogic.com\/blog\/wp-content\/uploads\/2021\/02\/4-use-environment-variables.jpeg 1014w, https:\/\/www.inogic.com\/blog\/wp-content\/uploads\/2021\/02\/4-use-environment-variables-300x110.jpeg 300w, https:\/\/www.inogic.com\/blog\/wp-content\/uploads\/2021\/02\/4-use-environment-variables-768x283.jpeg 768w, https:\/\/www.inogic.com\/blog\/wp-content\/uploads\/2021\/02\/4-use-environment-variables-660x243.jpeg 660w\" sizes=\"(max-width: 1014px) 100vw, 1014px\" \/><\/a><\/p>\n<h2><strong>Conclusion<\/strong><\/h2>\n<p style=\"text-align: justify;\">Thus Environment Variables can be made use of in our day to day scenarios.<\/p>\n<p><a href=\"https:\/\/www.inogic.com\/product\/productivity-apps\/dynamics-365-crm-sharepoint-security-metadata-sync\" target=\"_blank\" rel=\"noopener noreferrer\"><img decoding=\"async\" class=\"aligncenter wp-image-26719 size-full\" src=\"https:\/\/www.inogic.com\/blog\/wp-content\/uploads\/2021\/02\/sharepoint-security-sync.jpg\" alt=\"\" width=\"800\" height=\"200\" srcset=\"https:\/\/www.inogic.com\/blog\/wp-content\/uploads\/2021\/02\/sharepoint-security-sync.jpg 800w, https:\/\/www.inogic.com\/blog\/wp-content\/uploads\/2021\/02\/sharepoint-security-sync-300x75.jpg 300w, https:\/\/www.inogic.com\/blog\/wp-content\/uploads\/2021\/02\/sharepoint-security-sync-768x192.jpg 768w, https:\/\/www.inogic.com\/blog\/wp-content\/uploads\/2021\/02\/sharepoint-security-sync-660x165.jpg 660w\" sizes=\"(max-width: 800px) 100vw, 800px\" \/><\/a><\/p>\n","protected":false},"excerpt":{"rendered":"<p>Introduction In our previous blog, we understood the background and the purpose of the Environment Variable. In this blog we will see how to use it in our day to day solutions. As we make use of Plugins, Workflows, Scripts, Power Automate, etc. in our day-to-day solutions, let\u2019s see how to access and use the\u2026 <span class=\"read-more\"><a href=\"https:\/\/www.inogic.com\/blog\/2021\/02\/using-environment-variables-in-dynamics-365-crm-part-2\/\">Read More &raquo;<\/a><\/span><\/p>\n","protected":false},"author":13,"featured_media":0,"comment_status":"closed","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[16,19],"tags":[545,592,2149],"class_list":["post-26718","post","type-post","status-publish","format-standard","hentry","category-dynamics-365","category-dynamics-crm","tag-dynamics-365-crm","tag-dynamics-crm","tag-environment-variable"],"aioseo_notices":[],"_links":{"self":[{"href":"https:\/\/www.inogic.com\/blog\/wp-json\/wp\/v2\/posts\/26718","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\/13"}],"replies":[{"embeddable":true,"href":"https:\/\/www.inogic.com\/blog\/wp-json\/wp\/v2\/comments?post=26718"}],"version-history":[{"count":0,"href":"https:\/\/www.inogic.com\/blog\/wp-json\/wp\/v2\/posts\/26718\/revisions"}],"wp:attachment":[{"href":"https:\/\/www.inogic.com\/blog\/wp-json\/wp\/v2\/media?parent=26718"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.inogic.com\/blog\/wp-json\/wp\/v2\/categories?post=26718"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.inogic.com\/blog\/wp-json\/wp\/v2\/tags?post=26718"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}