The error you're encountering typically suggests that the function setMandatory is not correctly referenced in your form or that there is an issue with how the script is set up in Dynamics 365. Here are some steps you can try to resolve this issue:
1. Verify Script Registration:
Go to the form editor for the entity where you're using this script.
In the form editor, select Form Properties.
Ensure that the cr65f_JSPraticeScripts web resource is added in the Form Libraries section.
In the Event Handlers section for the field’s OnChange event, make sure that:
The Library is set to cr65f_JSPraticeScripts.
The Function is set to setMandatory (make sure there are no typos).
Pass execution context as first parameter is checked.
2. Check Script Syntax:
Double-check that your JavaScript function syntax is correct, especially in the function name and parameter. The function you've posted looks correct, but any minor typo or syntax error could cause this issue.
3. Clear Browser Cache:
Sometimes, old versions of scripts get cached in the browser, which can cause errors even after publishing the latest version.
Clear your browser cache or try running the script in an incognito window after publishing.
4. Publish All Customizations:
Instead of publishing only the form, try going to Solution and clicking on Publish All Customizations. This ensures all scripts are updated across the environment.
5. Add a Test Alert for Debugging:
Insert an alert statement in the function to confirm if the script is being executed.
function setMandatory(executioncontext) {
alert("Script triggered");
var formContext = executioncontext.getFormContext();
var mandatoryField = formContext.getAttribute("cr65f_setmandatory").getValue();
// rest of your code...
}
This can help confirm if the script is running but encountering an issue elsewhere.
6. Enable Script Debugging:
If you’re still having issues, enable script debugging in your browser (e.g., F12 in Chrome) to see if there are specific errors being logged in the console.
Following these steps should help you troubleshoot the issue. Let me know if the problem persists after these checks.