JavaScript applied to Dynamics 365

Listed below are examples of JavaScript applied to Dynamics 365

JS ‘Common’ Code Snippets

Remove {} from a GUID

Update a field that occurs multiple times on a D365 form

Reloading a form after a Web API call returns successfully

JS Code Best Practices

Enumerated Types

Using enumerated types to make option sets more readable. For example

rather than

Tools such as XrmDefinitelyTyped and Dataversify can connect to your Dataverse instance and automatically create these types

JS Callback

(Even though the callback design is now considered an old pattern, it’s still worth knowing.) According to w3schools, a callback is a function passed as an argument to another function. Carl has a good explanation of it here https://carldesouza.com/javascript-callback-functions/

The following code is an example of how to wrap a Web API call using a JS callback. This helps if the same Web API call needs to be made in several places within the code.

jXrm JS Library

As described in https://github.com/jasonxi/jXrm, jXrm was inspired by jQuery and it provides a JavaScript wrapping library on top of Microsoft Dynamics 365 (xRM) clientside SDK.

jXrm attribute method .hide()

instead of

jXrm attribute methods .disable() & .optional()

instead of

jXrm method .hideSection()

instead of

Window object

  • window.document – contains the PCF controls
  • window.document.iframe – contains the forms’ js files
  • window.location – contains the server’s url e.g. href, origin
  • window.open – can be used to get around the CORS issue (note: this is a method)
  • window.parent – returns the parent window of the current window
  • window.top – returns the topmost browser window

Further reading

Get and set a dataverse lookup column

References

1.) https://www.w3schools.com/jsref/obj_window.asp