Introduction to using JavaScript in Dynamics 365

2 Comments

Dynamics 365 can be extended using JavaScript, or specifically JScript, Microsoft’s naming of the language.

JavaScript can be used on different events in Dynamics 365, including:

  • Opening of forms, by adding a JScript Web Resource to the form and then having it run OnLoad.
  • Saving of records on forms, by adding a JScript Web Resource to the form and then having it run OnSave.
  • On a field changing, by by adding a JScript Web Resource to the form and then having it run on the field’s OnChange event.

You can add these JavaScript triggers to events through the Dynamics 365 user interface, and also through code dynamically, using:

You can add up to 50 script web resources to a form.

If libraries contain functions with the same name, the function that loads last will be the the one used. To get around this, you can either use unique function names, or use namespaces to differentiate your functions.

jQuery

Microsoft recommends using jQuery for cross-browser manipulation of the DOM. However, they do not recommend it not support within form scripts and ribbon commands.

Xrm.Page and Xrm.Utility

The following are helper libraries and methods that can be called by JavaScript in Dynamics 365.

Xrm.Page is for interacting with form elements. Using Xrm.Page you can get and set field values, show and hide fields, manipulate navigation, interact with a business process flow via Xml.Page.data.process.

Xrm.Page.Context provides ways to retrieve information specific to an organization. This includes what type of device is being used, the client URL, if autosave is enabled for the org, the user id, user name and user roles, query string parameters, and the version.

Xrm.Page.Data provides a way to save and refresh the form.

Xrm.Page.Data.Entity provides ways to retrieve data regarding the form record, such as a collection of the attributes, an XML representation of the saved record, the entity name, the GUID of the record, get the primary attribute, save the record and add/remove to the save event handlers. You can then access Xml.Page.Data.Entity.Attributes to gain access to attribute information such as the name, isdirty, length, control, required level, etc.

Xml.Page.ui gives you access to the control object, including get/set, label, type, and also ability to close the form, tabs on the form, controls on the form, sections, quick forms, navigation.

Xrm.Utility contains functionality such as opening forms and resources, displaying messages to users.

Xrm.Mobile.offline contains methods for is offline enabled, and CRUD mobile operations.

Grid contains methods to change the grid view (setCurrentView), get references to data displayed in the grid, set event handlers for when the grid is loaded (addOnLoad), get selected rows, get the grid entity. For editable grids, there are methods for OnRecordSelect, OnChange and OnSave.

Web Service Calls

You may want to use JavaScript to call Dynamics 365 services, for example to create, retrieve, update or delete data. This can be done many ways, including:

  • XMLHttpRequest
  • Using jQuery’s $.ajax (which is a wrapper for XMLHttpRequest)
  • XrmServiceToolkit – a JavaScript library for 2011/2013/2015 environments (REST and SOAP)
  • SDK.REST – a reusable library that simplifies asynchronous data operations using the REST endpoint
  • SDK.jQuery – a reusable library that performs actions with the REST endpoint using jQuery
  • XrmSvcToolkit – a small JavaScript library that helps access the 2011 web service interfaces (SOAP and REST)

Note the endpoints when accessing Dynamics 365:

  • WebAPI – uses JSON, more lightweight than XML. Has OAuth authentication, and OData (a standard for RESTful web services), making it easy to code across multiple languages. Supports FetchXML queries.
  • 2011 SOAP endpoint – Older endpoint. Uses XML. Developers use the libraries above provided by Microsoft to interact with the endpoint
  • 2011 REST endpoint (deprecated)

 

THANKS FOR READING. BEFORE YOU LEAVE, I NEED YOUR HELP.
 

I AM SPENDING MORE TIME THESE DAYS CREATING YOUTUBE VIDEOS TO HELP PEOPLE LEARN THE MICROSOFT POWER PLATFORM.

IF YOU WOULD LIKE TO SEE HOW I BUILD APPS, OR FIND SOMETHING USEFUL READING MY BLOG, I WOULD REALLY APPRECIATE YOU SUBSCRIBING TO MY YOUTUBE CHANNEL.

THANK YOU, AND LET'S KEEP LEARNING TOGETHER.

CARL

https://www.youtube.com/carldesouza

 

ABOUT CARL DE SOUZA

Carl de Souza is a developer and architect focusing on Microsoft Dynamics 365, Power BI, Azure, and AI.

carldesouza.comLinkedIn Twitter | YouTube

 

2 Responses to Introduction to using JavaScript in Dynamics 365

Leave a Reply

Your email address will not be published. Required fields are marked *