Using JavaScript in Dynamics 365 (Client API Reference)

Today, I will share with you some tips and tricks when adding custom scripts in your Dynamics 365 application. Here, I shared some of the common scenarios that may require you to write codes. The following scenarios are just examples; you might encounter different requirements, but the same solutions can be applied, so keep an open mind. I thought a one-page reference would be of good help, especially for those who are just exploring Dynamics 365 customization.

Scenario 1: Run a script on form load but ONLY when creating a new record.

Solution: Get the form type of the record and use it as your condition to ensure that you don’t load ALL scripts on every form load.

Syntax: if (formContext.ui.getFormType() === 1)

ValueForm type
0Undefined
1Create
2Update
3Read Only
4Disabled
6Bulk Edit
Form Type Return Values

Scenario 2: Every time you open a form, it says unsaved changes. This is because your form is dirty which means there are fields in your form that have changed. To find out what these fields are, you can use the developers’ tool available in your browser.

Solution: Press F12 while running Dynamics 365. Run the following script in the console and it will tell you the fields that you need to deal with.

Syntax: Xrm.Page.data.entity.getDataXml()

Scenario 3: You are required to set a value in your scripts which is only intended to control logic in the form. This becomes a dirty field.

Solution. Set submit mode to control how you want to save the field you changed programmatically. If you are not interested to store the value, you can choose to do so by setting the submit mode it as “never”.

Syntax: formContext.getAttribute(attributeName).setSubmitMode(mode);

alwaysThe data is always sent with a save.
neverThe data is never sent with a save. When this is used, the field(s) in the form for this attribute cannot be edited.
dirtyDefault behavior. The data is sent with the save when it has changed.
Submit Modes

Scenario 4: You are required to manipulate a lookup field value in the client side.

Syntax: (see image below)

Setting a value to Email Regarding field

Scenario 5: There are cases when users want to enable auto-save for most forms but disable it for specific forms.

Solution: Get save mode

Syntax: executionContext.getEventArgs().getSaveMode()

ValueSave modeEntity
1SaveAll
2Save and CloseAll
5DeactivateAll
6ReactivateAll
7SendEmail
15DisqualifyLead
16QualifyLead
47AssignUser or Team owned entities
58Save as CompletedActivities
59Save and NewAll
70Auto SaveAll
Save Mode Values

Scenario 6: Hiding and showing UCI tabs. Depending on the users, they can be very particular on working on form tabs.

Syntax: _formContext.ui.tabs.get(tabName).setVisible(false);

Scenario 7: Manipulate UCI tab label and set it as the active tab (focus) on form load.

Syntax: formContext.ui.tabs.get(tabName).setLabel(“Details”); formContext.ui.tabs.get(tabName).setFocus();

Scenario 8: Need to pass a parameter when a certain field value has been changed.

Solution: Add the script file in the form properties and define the JS function in the onchange event of the field. Enable pass execution context as the first parameter.

OnChange Field Properties

Syntax: functionName = function (executionContext) { var formContext = executionContext.getFormContext(); }

Scenario 9: You are required to display a notification message on the form.

Solution: Use form level notifications.

Syntax:

_formContext.ui.setFormNotification("INFO", "INFO");
_formContext.ui.setFormNotification("WARNING", "WARNING");
_formContext.ui.setFormNotification("ERROR", "ERROR");

Scenario 10: You are required to display the field values from another entity or table.

Solution: Instead of re-creating those fields in the same entity create a Quick View Form. Another way is to create calculated fields to feed the field value of another entity.

I hope this helps.

For more information on Client API, here is complete documentation for your reference.

If you would like to connect and ask some more tips, feel free to connect with me at LinkedIn.

Leveraging the Power of Solution Checker from Power Apps

If you started your implementation with an older version of Dynamics CRM and had converted to the latest version or to using Unified Client Interface, and you want to ensure that your solutions are written based on best practices, Solution Checker will come in handy.

December 2018 when Microsoft started to feature it in public view. Based on the customers that used it, it helps them in resolving lots of issues and have seen 20% performance increases on their form loads.

How to get started? Go to the Power Apps portal and head on to Solutions. Select the solution you would like to check, and you will see the Solution Checker button.

For first time users, it will show you an option to install the solution. Installing it will direct you to the AppSource site. Click on Get it now, and it will redirect you to a page where you need to select the environment you would like to use it for. Click Agree and wait.

AppSource: PowerApps Checker
Note: Every new and existing CDS for Apps environment will now have the solution installed automatically

Before you are able to run it, make sure you have the proper Security Roles in your common data service environment. Two of the required permissions are Export Customizations and Solution Checker.

Required Security Roles to run Solution Checker

You know when it’s done when Power Apps sends you an email with the high level results.

What I really like about Solution Checker is the flexibility to review the results. You can easily view the results in multiple ways: online (make.powerapps.com), during export or in your local drive. Reviewing results online will give you the ability to filter results in different helpful ways.

Viewing Solution Checker results online

The cool thing about this checker is, it can detect missing functions that are set in your form properties’ onload, onsave and on change events. Example error: An event registration entry was detected on the form Contact for the entity Contact for the event onload where the function does not appear to exist in the referring web resource script, [name of the web resource file]. The function name defined is [name of function]. This will likely cause issues when trying to invoke the event.

Example of an actual medium severity result

I hope this helps you improve the quality of your solutions. Let me know in the comments below how this helps you or if you would like to share some tips for everyone to learn.

Create a website or blog at WordPress.com

Up ↑