{"id":4710,"date":"2025-05-27T06:03:47","date_gmt":"2025-05-27T06:03:47","guid":{"rendered":"https:\/\/himbap.com\/blog\/?p=4710"},"modified":"2025-05-27T06:04:17","modified_gmt":"2025-05-27T06:04:17","slug":"updating-record-using-custom-button-power-pages","status":"publish","type":"post","link":"https:\/\/himbap.com\/blog\/?p=4710","title":{"rendered":"Updating record using Custom Button Power Pages"},"content":{"rendered":"\n<p><strong>Requirement<\/strong><\/p>\n\n\n\n<p>Power Pages provides a flexible platform to build low-code, externally facing web applications on top of Dataverse. A common requirement in Power Pages applications is to perform operations on Dataverse records for example update a record using custom button. This is useful when we want to update related table record. In this blog, I\u2019ll walk you through how to <strong>update a Dataverse record using a custom HTML button and JavaScript calling the Web API<\/strong> in Power Pages.<\/p>\n\n\n\n<p><strong>Details<\/strong><\/p>\n\n\n\n<p>To implement above requirement we need to perform following tasks:<\/p>\n\n\n\n<p><strong>Add WebAPI settings under Site Settings<\/strong><\/p>\n\n\n\n<p>While working with WebAPI in power pages we have to add following site settings.<\/p>\n\n\n\n<figure class=\"wp-block-image size-full is-resized\"><a href=\"https:\/\/himbap.com\/blog\/wp-content\/uploads\/2025\/04\/sitesettings.png\"><img decoding=\"async\" loading=\"lazy\" src=\"https:\/\/himbap.com\/blog\/wp-content\/uploads\/2025\/04\/sitesettings.png\" alt=\"\" class=\"wp-image-4712\" width=\"737\" height=\"539\" srcset=\"https:\/\/himbap.com\/blog\/wp-content\/uploads\/2025\/04\/sitesettings.png 737w, https:\/\/himbap.com\/blog\/wp-content\/uploads\/2025\/04\/sitesettings-300x219.png 300w, https:\/\/himbap.com\/blog\/wp-content\/uploads\/2025\/04\/sitesettings-624x456.png 624w\" sizes=\"(max-width: 737px) 100vw, 737px\" \/><\/a><\/figure>\n\n\n\n<p>Using these settings we can add setting for our entity where we want to use WebAPI, for example in our case we want to use for the case (incident)  entity so I have used following settings for the entity and field.<\/p>\n\n\n\n<figure class=\"wp-block-image size-full\"><a href=\"https:\/\/himbap.com\/blog\/wp-content\/uploads\/2025\/04\/sitesetting2.png\"><img decoding=\"async\" loading=\"lazy\" width=\"626\" height=\"360\" src=\"https:\/\/himbap.com\/blog\/wp-content\/uploads\/2025\/04\/sitesetting2.png\" alt=\"\" class=\"wp-image-4713\" srcset=\"https:\/\/himbap.com\/blog\/wp-content\/uploads\/2025\/04\/sitesetting2.png 626w, https:\/\/himbap.com\/blog\/wp-content\/uploads\/2025\/04\/sitesetting2-300x173.png 300w, https:\/\/himbap.com\/blog\/wp-content\/uploads\/2025\/04\/sitesetting2-624x359.png 624w\" sizes=\"(max-width: 626px) 100vw, 626px\" \/><\/a><\/figure>\n\n\n\n<p><strong>Make sure User have required Web Roles with Table permissions<\/strong><\/p>\n\n\n\n<p>When we want to access data in Power Pages we need to make sure, user have required web roles assigned and required table permission is configured.<\/p>\n\n\n\n<p><strong>Write Web API Request<\/strong><\/p>\n\n\n\n<p>Now we can start writing Web API request, first we need to write a wrapper to get authentication token, Microsoft provided us a sample code which we need to use before making Web API call either we can include this before every Web API call or we can keep this in template so it will be available for us:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code> window.onload=function(){\r\n    (function(webapi, $){\r\n            function safeAjax(ajaxOptions) {\r\n                var deferredAjax = $.Deferred();\r\n                        shell.getTokenDeferred().done(function (token) {\r\n                    \/\/ add headers for AJAX\r\n                    if (!ajaxOptions.headers) {\r\n                        $.extend(ajaxOptions, {\r\n                            headers: {\r\n                                \"__RequestVerificationToken\": token\r\n                            }\r\n                        }); \r\n                    } else {\r\n                        ajaxOptions.headers&#91;\"__RequestVerificationToken\"] = token;\r\n                    }\r\n                    $.ajax(ajaxOptions)\r\n                        .done(function(data, textStatus, jqXHR) {\r\n                            validateLoginSession(data, textStatus, jqXHR, deferredAjax.resolve);\r\n                        }).fail(deferredAjax.reject); \/\/AJAX\r\n                }).fail(function () {\r\n                    deferredAjax.rejectWith(this, arguments); \/\/ on token failure pass the token AJAX and args\r\n                });\r\n        \r\n                return deferredAjax.promise();\t\r\n            }\r\n            webapi.safeAjax = safeAjax;\r\n        })(window.webapi = window.webapi || {}, jQuery)\r\n    \r\n    }; <\/code><\/pre>\n\n\n\n<p>I have used this code under footer template:<\/p>\n\n\n\n<figure class=\"wp-block-image size-full\"><a href=\"https:\/\/himbap.com\/blog\/wp-content\/uploads\/2025\/05\/webapi1.png\"><img decoding=\"async\" loading=\"lazy\" width=\"826\" height=\"742\" src=\"https:\/\/himbap.com\/blog\/wp-content\/uploads\/2025\/05\/webapi1.png\" alt=\"\" class=\"wp-image-4716\" srcset=\"https:\/\/himbap.com\/blog\/wp-content\/uploads\/2025\/05\/webapi1.png 826w, https:\/\/himbap.com\/blog\/wp-content\/uploads\/2025\/05\/webapi1-300x269.png 300w, https:\/\/himbap.com\/blog\/wp-content\/uploads\/2025\/05\/webapi1-768x690.png 768w, https:\/\/himbap.com\/blog\/wp-content\/uploads\/2025\/05\/webapi1-624x561.png 624w\" sizes=\"(max-width: 826px) 100vw, 826px\" \/><\/a><\/figure>\n\n\n\n<p>Now let&#8217;s take and example, In case entity I have add a custom text field called comments and I have added this field to edit case page where user can write comment and save record. <\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>  var remark=$(\"#him_comments\");\n  remark.after(\"&lt;button id='submitremarkbutton' type='button' class='btn btn-primary button' style='margin-top:20px;'>&lt;span>&lt;\/span>Submit Remarks&lt;\/button>\");\n\n $(\"#submitremarkbutton\").click(function(){\n     var record = {};\n     record.him_comments =remark.val(); \n\n     webapi.safeAjax({\n         type: \"PATCH\",\n         contentType: \"application\/json\",\n         url: \"\/_api\/incidents(\"+getQueryParameter('id')+\")\",\n         data: JSON.stringify(record),\n         success: function (data, textStatus, xhr) {\n             console.log(\"Record updated\");\n             \/\/clean value\n             remark.val('');\n             location.reload();\n         },\n         error: function (xhr, textStatus, errorThrown) {\n             console.log(xhr);\n         }\n     });\n\n    });\n function getQueryParameter(id) {\r\n        var urlParams = new URLSearchParams(window.location.search);\r\n        return urlParams.get(id);\r\n    }<\/code><\/pre>\n\n\n\n<p>I have used this code under Case edit form like below:<\/p>\n\n\n\n<figure class=\"wp-block-image size-full\"><a href=\"https:\/\/himbap.com\/blog\/wp-content\/uploads\/2025\/05\/webapi3.png\"><img decoding=\"async\" loading=\"lazy\" width=\"681\" height=\"308\" src=\"https:\/\/himbap.com\/blog\/wp-content\/uploads\/2025\/05\/webapi3.png\" alt=\"\" class=\"wp-image-4718\" srcset=\"https:\/\/himbap.com\/blog\/wp-content\/uploads\/2025\/05\/webapi3.png 681w, https:\/\/himbap.com\/blog\/wp-content\/uploads\/2025\/05\/webapi3-300x136.png 300w, https:\/\/himbap.com\/blog\/wp-content\/uploads\/2025\/05\/webapi3-624x282.png 624w\" sizes=\"(max-width: 681px) 100vw, 681px\" \/><\/a><\/figure>\n\n\n\n<p>After save and sync our changes to Portal we should be able to see this button in portal and on click of Submit Remark, our comments should be saved to Case entity.<\/p>\n\n\n\n<p>Hope it will help someone!!<br><strong>Keep learning and Keep Sharing!!<\/strong><\/p>\n","protected":false},"excerpt":{"rendered":"<p>Requirement Power Pages provides a flexible platform to build low-code, externally facing web applications on top of Dataverse. A common requirement in Power Pages applications is to perform operations on Dataverse records for example update a record using custom button. This is useful when we want to update related table record. In this blog, I\u2019ll walk you through how to&#8230; <a href=\"https:\/\/himbap.com\/blog\/?p=4710\">Read more &raquo;<\/a><\/p>\n","protected":false},"author":1,"featured_media":4713,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":[],"categories":[21,1103,296],"tags":[1127,1128,1126,1125],"_links":{"self":[{"href":"https:\/\/himbap.com\/blog\/index.php?rest_route=\/wp\/v2\/posts\/4710"}],"collection":[{"href":"https:\/\/himbap.com\/blog\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/himbap.com\/blog\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/himbap.com\/blog\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/himbap.com\/blog\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=4710"}],"version-history":[{"count":4,"href":"https:\/\/himbap.com\/blog\/index.php?rest_route=\/wp\/v2\/posts\/4710\/revisions"}],"predecessor-version":[{"id":4720,"href":"https:\/\/himbap.com\/blog\/index.php?rest_route=\/wp\/v2\/posts\/4710\/revisions\/4720"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/himbap.com\/blog\/index.php?rest_route=\/wp\/v2\/media\/4713"}],"wp:attachment":[{"href":"https:\/\/himbap.com\/blog\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=4710"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/himbap.com\/blog\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=4710"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/himbap.com\/blog\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=4710"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}