|
| 1 | +--- |
| 2 | +title: Missing Form Context for Internal Handlers on Customized Bookable Resource Forms |
| 3 | +description: Addresses issues with customized forms that are based on the default bookable resource form for Dynamics 365 Field Service. |
| 4 | +author: m-hartmann |
| 5 | +ms.author: mhart |
| 6 | +ms.reviewer: mhart |
| 7 | +ms.date: 04/08/2025 |
| 8 | +ms.custom: sap:Schedule Board |
| 9 | +--- |
| 10 | +# Form context for internal handlers is missing on customized bookable resource forms |
| 11 | + |
| 12 | +This article helps resolve an issue caused by missing form context for internal handlers on bookable resource forms in Microsoft Dynamics 365 Field Service. |
| 13 | + |
| 14 | +## Symptoms |
| 15 | + |
| 16 | +While [creating a bookable resource](/dynamics365/field-service/set-up-bookable-resources) in Dynamics 365 Field Service, you might receive the following error message when you select a user from a field on the form: |
| 17 | + |
| 18 | +> Cannot read properties of undefined (reading 'getFormContext') |
| 19 | +
|
| 20 | +## Cause |
| 21 | + |
| 22 | +The issue occurs because the system uses a customized form that is based on an outdated version of the bookable resource form. A change to the internal handlers for `onchange` events now requires the execution context to be passed in from the form. |
| 23 | + |
| 24 | +## Resolution |
| 25 | + |
| 26 | +Use one of the listed resolutions to ensure that the execution context is passed as the first parameter. |
| 27 | + |
| 28 | +> [!IMPORTANT] |
| 29 | +> The following resolutions assume that the script error references the `Mscrm.userid_onchange` function. If the error refers to other fields or functions, such as `Mscrm.accountid_onchange` or `Mscrm.contactid_onchange`, adjust the steps accordingly. |
| 30 | +
|
| 31 | +### Resolution 1: Update the form in Power Apps |
| 32 | + |
| 33 | +1. Sign in to Power Apps and open the solution that contains the form. |
| 34 | + |
| 35 | +1. Select the **User** field and go to the event settings. |
| 36 | + |
| 37 | +1. Look for the `Mscrm.userid_onchange` event handler. |
| 38 | + |
| 39 | + If the handler doesn't exist: |
| 40 | + |
| 41 | + 1. Add a new event of type **On Change**. |
| 42 | + 1. Select the **Scheduling/BookableResource/BookableResource_main_system_library.js** library. |
| 43 | + 1. Enter **Mscrm.userid_onchange** in the **Function** field. |
| 44 | + 1. Ensure the **Enabled** and **Pass execution context as first parameter** checkboxes are selected. |
| 45 | + |
| 46 | + If the handler exists: |
| 47 | + |
| 48 | + 1. Edit the handler and ensure the **Pass execution context as first parameter** checkbox is selected. |
| 49 | + |
| 50 | +1. Save and publish the updated form. |
| 51 | + |
| 52 | +### Resolution 2: Validate the customizations.xml file |
| 53 | + |
| 54 | +1. Open the **customizations.xml** file from the solution associated with the customized form that shows the error in an editor. |
| 55 | + |
| 56 | +1. In the `Handler` element of the `Mscrm.userid_onchange` function, ensure the `passExecutionContext` attribute is set to **true**. |
| 57 | + |
| 58 | +1. Republish the solution. |
| 59 | + |
| 60 | +### Resolution 3: Run a script in the browser console |
| 61 | + |
| 62 | +To ensure this script has permission to find and update the required information, you need to run it in a browser tab that has an active session with your environment. Additionally, your user account needs permisssion to update the XML of the customized bookable resource form. |
| 63 | + |
| 64 | +1. Open the environment in your browser. The following instructions use Microsoft Edge as an example. |
| 65 | + |
| 66 | +1. Open **DevTools** by pressing <kbd>F12</kbd> or navigating to ellipsis (…) > **More tools** > **Developer tools**. |
| 67 | + |
| 68 | +1. Select the **Console** tab in DevTools and select **Clear Console**. |
| 69 | + |
| 70 | +1. Copy and paste the following JavaScript code into the console. |
| 71 | + |
| 72 | + ```JavaScript |
| 73 | + const ORG = "<YOUR-ENVIRONMENT-URL>"; // for example "contoso.crm.dynamics.com" |
| 74 | + |
| 75 | + async function fixBookableResourceForms() { |
| 76 | + |
| 77 | + console.log("Starting Bookable Resource Form fetch process..."); |
| 78 | + |
| 79 | + let publishXML = false; |
| 80 | + |
| 81 | + try { |
| 82 | + |
| 83 | + const response = await fetch(`https://${ORG}/api/data/v9.2/systemforms?$filter=objecttypecode eq 'bookableresource'`); |
| 84 | + |
| 85 | + if (!response.ok) { |
| 86 | + |
| 87 | + throw new Error(`Error fetching resources: ${response.statusText}`); |
| 88 | + |
| 89 | + } |
| 90 | + |
| 91 | + const data = await response.json(); |
| 92 | + |
| 93 | + const forms = data.value; |
| 94 | + |
| 95 | + if(forms.length !== 0) { |
| 96 | + |
| 97 | + for (const form of forms) { |
| 98 | + |
| 99 | + console.log(` Checking form ${form.name} (${form.formid})...`); |
| 100 | + |
| 101 | + const formXML = form.formxml; |
| 102 | + |
| 103 | + const parser = new DOMParser(); |
| 104 | + |
| 105 | + const xmlDoc = parser.parseFromString(formXML, "text/xml"); |
| 106 | + |
| 107 | + const handlers = xmlDoc.getElementsByTagName("Handler"); |
| 108 | + |
| 109 | + const userIdHandlers = [] |
| 110 | + |
| 111 | + for (let i = 0; i < handlers.length; i++) { |
| 112 | + |
| 113 | + const handler = handlers[i]; |
| 114 | + |
| 115 | + if ( |
| 116 | + |
| 117 | + handler.getAttribute("functionName") === "Mscrm.userid_onchange" && |
| 118 | + |
| 119 | + handler.getAttribute("libraryName") === "Scheduling/BookableResource/BookableResource_main_system_library.js" |
| 120 | + |
| 121 | + ) { |
| 122 | + |
| 123 | + userIdHandlers.push(handler); |
| 124 | + |
| 125 | + } |
| 126 | + |
| 127 | + } |
| 128 | + |
| 129 | + if (userIdHandlers.length > 1) { |
| 130 | + |
| 131 | + console.warn(`Form ${form.name} (${form.formid}) has more than 1 Mscrm.userid_onchange event handlers (has ${userIdHandlers.length}).`); |
| 132 | + |
| 133 | + } |
| 134 | + |
| 135 | + else if (userIdHandlers.length === 0) { |
| 136 | + |
| 137 | + console.warn(`Form ${form.name} (${form.formid}) has 0 Mscrm.userid_onchange event handlers.`); |
| 138 | + |
| 139 | + continue; |
| 140 | + |
| 141 | + } |
| 142 | + |
| 143 | + await Promise.all(userIdHandlers.map(async handler => { |
| 144 | + |
| 145 | + if ( |
| 146 | + |
| 147 | + handler.getAttribute("functionName") === "Mscrm.userid_onchange" && |
| 148 | + |
| 149 | + handler.getAttribute("libraryName") === "Scheduling/BookableResource/BookableResource_main_system_library.js" && |
| 150 | + |
| 151 | + handler.getAttribute("passExecutionContext") === "true" |
| 152 | + |
| 153 | + ) { |
| 154 | + |
| 155 | + console.log(` Form ${form.name} (${form.formid}) has the correct Mscrm.userid_onchange event handler.`); |
| 156 | + |
| 157 | + } |
| 158 | + |
| 159 | + else if ( |
| 160 | + |
| 161 | + handler.getAttribute("functionName") === "Mscrm.userid_onchange" && |
| 162 | + |
| 163 | + handler.getAttribute("libraryName") === "Scheduling/BookableResource/BookableResource_main_system_library.js" && |
| 164 | + |
| 165 | + (handler.getAttribute("passExecutionContext") === "false" || handler.getAttribute("passExecutionContext") === null) |
| 166 | + |
| 167 | + ) { |
| 168 | + |
| 169 | + console.log(` Form ${form.name} (${form.formid}) has the Mscrm.userid_onchange event handler but the passExecutionContext attribute is not set to true. Setting it to true...`); |
| 170 | + |
| 171 | + handler.setAttribute("passExecutionContext", "true"); |
| 172 | + |
| 173 | + const serializer = new XMLSerializer(); |
| 174 | + |
| 175 | + const updatedXml = serializer.serializeToString(xmlDoc); |
| 176 | + |
| 177 | + await updateBookableResourceForms(form, updatedXml); |
| 178 | + |
| 179 | + publishXML = true; |
| 180 | + |
| 181 | + } |
| 182 | + |
| 183 | + return Promise.resolve(); |
| 184 | + |
| 185 | + })); |
| 186 | + |
| 187 | + } |
| 188 | + |
| 189 | + } else { |
| 190 | + |
| 191 | + console.log("No Bookable Resource forms found. Nothing was done"); |
| 192 | + |
| 193 | + } |
| 194 | + |
| 195 | + } catch (error) { |
| 196 | + |
| 197 | + console.error(error.message); |
| 198 | + |
| 199 | + } |
| 200 | + |
| 201 | + if(publishXML) { |
| 202 | + |
| 203 | + console.log("Publishing changes..."); |
| 204 | + |
| 205 | + await publishChanges(); |
| 206 | + |
| 207 | + publishXML = false; |
| 208 | + |
| 209 | + } |
| 210 | + |
| 211 | + console.log("Finished"); |
| 212 | + |
| 213 | + } |
| 214 | + |
| 215 | + async function updateBookableResourceForms(form, formxml) { |
| 216 | + |
| 217 | + console.log(` Updating Bookable Resource Form ${form.name} (${form.formid}) ...`); |
| 218 | + |
| 219 | + try { |
| 220 | + |
| 221 | + const reqBody = JSON.stringify({formxml: formxml}); |
| 222 | + |
| 223 | + const updateResponse = await fetch(`https://${ORG}/api/data/v9.2/systemforms(${form.formid})`, { |
| 224 | + |
| 225 | + method: 'PATCH', |
| 226 | + |
| 227 | + headers: { |
| 228 | + |
| 229 | + 'Content-Type': 'application/json' |
| 230 | + |
| 231 | + }, |
| 232 | + |
| 233 | + body: reqBody |
| 234 | + |
| 235 | + }); |
| 236 | + |
| 237 | + if (!updateResponse.ok) { |
| 238 | + |
| 239 | + throw new Error(`Error updating form ${formid}: ${updateResponse.statusText}`); |
| 240 | + |
| 241 | + } |
| 242 | + |
| 243 | + console.log(` Form ${form.name} (${form.formid}) updated successfully.`); |
| 244 | + |
| 245 | + } catch (error) { |
| 246 | + |
| 247 | + console.error(error.message); |
| 248 | + |
| 249 | + } |
| 250 | + |
| 251 | + console.log(` Done updating Bookable Resource Form ${form.name} (${form.formid}).`); |
| 252 | + |
| 253 | + } |
| 254 | + |
| 255 | + async function publishChanges() { |
| 256 | + |
| 257 | + console.log(" Starting publish XML process..."); |
| 258 | + |
| 259 | + const publishResponse = await fetch(`https://${ORG}/api/data/v9.2/PublishXml`, { |
| 260 | + |
| 261 | + method: 'POST', |
| 262 | + |
| 263 | + headers: { |
| 264 | + |
| 265 | + 'Content-Type': 'application/json' |
| 266 | + |
| 267 | + }, |
| 268 | + |
| 269 | + body: '{"ParameterXml": "<importexportxml><entities><entity>bookableresource</entity></entities></importexportxml>"}' |
| 270 | + |
| 271 | + }); |
| 272 | + |
| 273 | + if (!publishResponse.ok) { |
| 274 | + |
| 275 | + throw new Error(`Error publishing XML: ${publishResponse.statusText}`); |
| 276 | + |
| 277 | + } |
| 278 | + |
| 279 | + console.log(" Done publish XML process."); |
| 280 | + |
| 281 | + } |
| 282 | + |
| 283 | + // Call the function |
| 284 | + |
| 285 | + fixBookableResourceForms() |
| 286 | + ``` |
| 287 | + |
| 288 | +1. Update the `ORG` constant in the script with your environment URL, for example, `contoso.crm.dynamics.com`. |
| 289 | + |
| 290 | +1. Run the script and review the output to confirm the updates. |
0 commit comments