{"id":33574,"date":"2023-01-04T15:45:54","date_gmt":"2023-01-04T10:15:54","guid":{"rendered":"https:\/\/www.inogic.com\/blog\/?p=33574"},"modified":"2023-01-04T15:46:57","modified_gmt":"2023-01-04T10:16:57","slug":"optimum-utilization-of-settings-feature-in-power-apps","status":"publish","type":"post","link":"https:\/\/www.inogic.com\/blog\/2023\/01\/optimum-utilization-of-settings-feature-in-power-apps\/","title":{"rendered":"Optimum utilization of \u2018Settings\u2019 feature in Power Apps"},"content":{"rendered":"<h2><strong>Introduction<\/strong><\/h2>\n<p>While working on a project, there comes a time when you have some value that is unique and needs to be used regularly for your operations. Recently, I too have been through this and found an easy way to optimize the \u2018get and set\u2019 of this value by using App Settings.<\/p>\n<p><a href=\"https:\/\/learn.microsoft.com\/en-us\/power-apps\/maker\/data-platform\/create-edit-configure-settings\" target=\"_blank\" rel=\"noopener\">Settings<\/a> in Power Apps are solution components that allow developers and administrators to quickly configure apps to provide a tailored experience. Settings can be used to enable or disable features, as well as to configure feature behavior for a single app or all apps in an environment.<\/p>\n<h3><strong>Scenario<\/strong><\/h3>\n<p>In my project, I had to store an access token somewhere, so that I could get it instantly and use it further. However, to accomplish this, I would have to create a custom entity and store the value there, which would lead to complexity in our overall flow, and of course, I would have to retrieve it whenever needed.<\/p>\n<p>In such cases, the Settings feature comes in handy. Here, I will need to create a \u2018setting\u2019 definition, which is nothing but the setting that includes the environment variable that stores the value of the token. Also, whenever the token expires, I can replace it with a new one.<\/p>\n<p>Please follow these <a href=\"https:\/\/learn.microsoft.com\/en-us\/power-apps\/maker\/data-platform\/create-edit-configure-settings#adding-a-new-setting-definition\" target=\"_blank\" rel=\"noopener\">steps<\/a> to create the setting definition in your environment.<\/p>\n<blockquote><p><em>Note: I prefer to create the settings in my solution rather than in the default one<br \/>\n<\/em><\/p><\/blockquote>\n<p>So while creating the setting definition, I will add my current token as the default value.<\/p>\n<p><img decoding=\"async\" class=\"alignnone size-full wp-image-33575\" style=\"border: 1px solid #0a0a0a; padding: 1px; margin: 1px;\" src=\"https:\/\/www.inogic.com\/blog\/wp-content\/uploads\/2023\/01\/2App-Settings.png\" alt=\"App Settings\" width=\"1435\" height=\"753\" srcset=\"https:\/\/www.inogic.com\/blog\/wp-content\/uploads\/2023\/01\/2App-Settings.png 1435w, https:\/\/www.inogic.com\/blog\/wp-content\/uploads\/2023\/01\/2App-Settings-300x157.png 300w, https:\/\/www.inogic.com\/blog\/wp-content\/uploads\/2023\/01\/2App-Settings-1024x537.png 1024w, https:\/\/www.inogic.com\/blog\/wp-content\/uploads\/2023\/01\/2App-Settings-768x403.png 768w, https:\/\/www.inogic.com\/blog\/wp-content\/uploads\/2023\/01\/2App-Settings-660x346.png 660w\" sizes=\"(max-width: 1435px) 100vw, 1435px\" \/><\/p>\n<p>Following are the parameter details-<\/p>\n<p><strong>Default Value <\/strong>\u2013 the default value stored in the setting<\/p>\n<p><strong>Data type<\/strong> \u2013 can be String, Number, or Two Options<\/p>\n<p><strong>Values can be changed for<\/strong> \u2013 it defines the default value that can be overridden for the apps\/environment<\/p>\n<p>Further, let us dive into the coding part and see how we can get this setting value.<\/p>\n<pre class=\"lang:css gutter:true start:1\">async getTokenAndCallAPI() {\r\n\r\n\/\/unique name of entity\r\nlet settingName = \"new_accesstokensetting\";\r\n\r\nlet token = \"\";\r\n\r\ntry {\r\n\r\n\/\/works sync to read value from setting\r\n\r\ntoken = Xrm.Utility.getGlobalContext().getCurrentAppSetting(settingName);\r\n\r\n\/\/My function which uses above token\r\n\r\nthis.callingAPIWithToken(token).then((response: any) =&gt; {\r\n\r\nthis.onSuccessOfAPI(response);\r\n\r\n}).catch((error: any) =&gt; {\r\n\r\n})\r\n\r\n} catch (error: any) {\r\n\r\nthrow new Error(error.message);\r\n\r\n}\r\n\r\n}<\/pre>\n<p>Here, to get the setting value, we will use the method <strong>getCurrentAppSetting<\/strong>, which returns the environment variable if present or else returns the default value. The environment variable stores an overridden value that overrides the default value.<\/p>\n<p>Our access token gets expired in an hour, so we will need to override the default value of the setting with the new token. Let us see how we are achieving this, below.<\/p>\n<p>In error call back, if we get the error as \u2018invalid token\u2019 then we will create the token and set the token value in the setting as an override value.<\/p>\n<pre class=\"lang:css gutter:true start:1\">catch((error: any) =&gt; {\r\n\r\nif (error.message == \"Invalid Token\") {\r\n\r\ntoken = this.createToken();\r\n\r\n\/\/1 - add or update setting environment value\r\n\r\nvar appOverrideScope = 1;\r\n\r\nvar saveSettingOptions = { overrideScope: appOverrideScope, solutionUniqueName: \"AccessTokenSettings\" }\r\n\r\n\/\/Set the value in app setting\r\n\r\nXrm.Utility.getGlobalContext().saveSettingValue(settingName, token, saveSettingOptions).then(()=&gt;{\r\n\r\nthis.getTokenAndCallAPI();\r\n\r\n})\r\n\r\n}\r\n\r\n})<\/pre>\n<p>Please find the parameter details below-<\/p>\n<p><strong>settingName<\/strong> &#8211; The name of the setting whose value should be updated<\/p>\n<p><strong>value<\/strong> &#8211; The value to which the setting should be updated<\/p>\n<p><strong>saveSettingOptions<\/strong> &#8211; When updating the value, you get the below parameters<\/p>\n<ul>\n<li><em>overrideScope<\/em>To add or modify an environment setting value, use 1<\/li>\n<\/ul>\n<p>To add or modify a setting app value, use 2<\/p>\n<p>If nothing is entered, the default environment is used<\/p>\n<ul>\n<li><em>solutionUniqueName<\/em>The solution to which the setting app or setting environment value should be added<br \/>\nThe default solution is applied if none is entered<\/li>\n<\/ul>\n<p>Now, if we go into our settings and check, the override value will be shown as the following-<\/p>\n<p><img decoding=\"async\" class=\"alignnone size-full wp-image-33576\" style=\"border: 1px solid #0a0a0a; padding: 1px; margin: 1px;\" src=\"https:\/\/www.inogic.com\/blog\/wp-content\/uploads\/2023\/01\/3App-Settings.png\" alt=\"App Settings\" width=\"1435\" height=\"746\" srcset=\"https:\/\/www.inogic.com\/blog\/wp-content\/uploads\/2023\/01\/3App-Settings.png 1435w, https:\/\/www.inogic.com\/blog\/wp-content\/uploads\/2023\/01\/3App-Settings-300x156.png 300w, https:\/\/www.inogic.com\/blog\/wp-content\/uploads\/2023\/01\/3App-Settings-1024x532.png 1024w, https:\/\/www.inogic.com\/blog\/wp-content\/uploads\/2023\/01\/3App-Settings-768x399.png 768w, https:\/\/www.inogic.com\/blog\/wp-content\/uploads\/2023\/01\/3App-Settings-660x343.png 660w\" sizes=\"(max-width: 1435px) 100vw, 1435px\" \/><\/p>\n<p>The new token value is stored in environment value. After setting the value, we will call our method again to call the API, this way our API will be called even if the token gets expired. Later, when this token gets expired, it will update the highlighted environment value and will use the updated token value.<\/p>\n<h2><strong>Conclusion<\/strong><\/h2>\n<p>Thus, we learned how to \u2018get and set\u2019 the values of app settings<\/p>\n<p><a href=\"https:\/\/www.inogic.com\/product\/productivity-apps\/map-my-relationships-dynamics-365-crm\" target=\"_blank\" rel=\"noopener\"><img decoding=\"async\" class=\"alignnone  wp-image-33577\" src=\"https:\/\/www.inogic.com\/blog\/wp-content\/uploads\/2023\/01\/Map-My-Relationships.png\" alt=\"\" width=\"828\" height=\"207\" srcset=\"https:\/\/www.inogic.com\/blog\/wp-content\/uploads\/2023\/01\/Map-My-Relationships.png 800w, https:\/\/www.inogic.com\/blog\/wp-content\/uploads\/2023\/01\/Map-My-Relationships-300x75.png 300w, https:\/\/www.inogic.com\/blog\/wp-content\/uploads\/2023\/01\/Map-My-Relationships-768x192.png 768w, https:\/\/www.inogic.com\/blog\/wp-content\/uploads\/2023\/01\/Map-My-Relationships-660x165.png 660w\" sizes=\"(max-width: 828px) 100vw, 828px\" \/><\/a><\/p>\n","protected":false},"excerpt":{"rendered":"<p>Introduction While working on a project, there comes a time when you have some value that is unique and needs to be used regularly for your operations. Recently, I too have been through this and found an easy way to optimize the \u2018get and set\u2019 of this value by using App Settings. Settings in Power\u2026 <span class=\"read-more\"><a href=\"https:\/\/www.inogic.com\/blog\/2023\/01\/optimum-utilization-of-settings-feature-in-power-apps\/\">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":{"om_disable_all_campaigns":false,"_monsterinsights_skip_tracking":false,"_monsterinsights_sitenote_active":false,"_monsterinsights_sitenote_note":"","_monsterinsights_sitenote_category":0,"footnotes":""},"categories":[18,38],"tags":[1337],"class_list":["post-33574","post","type-post","status-publish","format-standard","hentry","category-dynamics-365-v9-2","category-microsoft-powerapps","tag-power-apps"],"aioseo_notices":[],"_links":{"self":[{"href":"https:\/\/www.inogic.com\/blog\/wp-json\/wp\/v2\/posts\/33574","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=33574"}],"version-history":[{"count":0,"href":"https:\/\/www.inogic.com\/blog\/wp-json\/wp\/v2\/posts\/33574\/revisions"}],"wp:attachment":[{"href":"https:\/\/www.inogic.com\/blog\/wp-json\/wp\/v2\/media?parent=33574"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.inogic.com\/blog\/wp-json\/wp\/v2\/categories?post=33574"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.inogic.com\/blog\/wp-json\/wp\/v2\/tags?post=33574"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}