Posts

Showing posts with the label JavaScript

HOW TO GET MICROSOFT DYNAMICS 365 FORM TYPE IN JAVASCRIPT

Image
When working with forms, you have the ability to create different types of forms based on your needs : Main Form, Quick View Form, Quick Create Form, or Card Form. The most common type is the Main Form which is the form where most of time the users work. The main form itself has different status that represents specific functionality or use, this is known as well as form type . The purpose of this post is to know what are the different types of the Main form and how to get it using JavaScript , so you will have the ability to change its behavior based on its type that will be reflected for end users. Getting the type of form can be done in JavaScript using the function formContext.ui.getFormType() . The following JavaScript function will display a popup message with the form type: function getFormType(context) { var formContext =...

DISABLE ALL FORM FIELDS IN DYNAMICS 365 USING JAVASCRIPT

Image
To make the form read-only in Dynamics 365 you have 3 options: changing record status to Inactive, Business rule, JavaScript. The purpose of this article is to know how to make all form fields disabled with JavaScript. Changing record status to Inactive might not very suitable for some cases where cascading rules might be applied to children records. On the other hand, even though it is a low code solution, using business rule to make all fields read-only will be very time consuming especially if a field is removed from the form or a new field is added to the form. However, using JavaScript will allow you to set all form fields to read-only dynamically with a small piece of code and you can do it based on a certain condition of an attribute value. Make read-only the whole fields of a form based on a certain condition ...

SAVE FORM IN DYNAMICS 365 JAVASCRIPT

Image
In Dynamics 365, you may need to save a CRM form automatically in JavaScript upon a specific action on the form. In order to do that, the function formContext.data.save(saveOptions).then(successCallback, errorCallback); can be used to save the record asynchronously with callback functions to be executed after the save operation is completed. The following example will display a successful message if the save operation is completed, and a failure message if not. formContext.data.save(1).then( function () { alert("Successfully Saved!"); }, function () { alert("Failed while saving!"); }); ...

CHANGE CONTROL LABEL DURING RUNTIME IN DYNAMICS 365 USING JAVASCRIPT

Image
In Dynamics 365, we might have to change CRM control label at runtime for specific needs. This can be easily done through JavaScript code by calling the function setLabel() that sets the label of the control. One function, several ways you can use to change the label based on the control type. Dynamically change a tab label Using the function setLabel , you can change a tab label during runtime as per the below code snippet. var runtimeTabLabel = "Runtime Tab Label"; formContext.ui.tabs.get("<tabName>").setLabel(runtimeTabLabel); Dynamically change section label Using the function setLabel , you can change a section label during r...

CHECKING DIRTY FIELDS IN DYNAMICS 365 USING JAVASCRIPT

Image
In Dynamics 365, there is a common need to identify which fields have been modified on the form during an update. One way is by creating a Plugin that will receive data of the modified fields in the current update event. However, the purpose of this article is to check how to identify the modified fields through JavaScript . It is done by verifying if the form or fields are in a dirty state which indicates if they have been changed. Three methods are available that you can use to get if the form or specific fields are dirty. Check if form is dirty to verify if any field on the form has been changed. Using the function getIsDirty , you can check if any field on the form has been modified. formContext.data.entity.getIsDirty(); ...

SETSUBMITMODE IN DYNAMICS 365 JAVASCRIPT

Image
In Dynamics 365, there are some scenarios where you want to always submit or never submit the value of a column into the database. To do this, you have to use the JavaScript method setSubmitMode that will control whether the data for a column is submitted when a record is created or saved. In case you have a column that you are not interest to capture its data, use the never parameter for the setSubmitMode method. While, if you want to force a column value to be submitted whether it has changed or not, use the always parameter for the setSubmitMode method. Use one of the below three options for the setSubmitMode function as per the below. always : The data is always sent with a create/save event and can be captured within the entity attributes in a plugin function setSubmitModeField(context) { var formCont...

ACCESS FIELDS OF QUICK VIEW FORM THROUGH JAVASCRIPT IN DYNAMICS 365

Image
As known, quick view controls are associated to lookup fields that are included in a form and they are added to display specific information about the selected record in the lookup field. This will increase user experience where the users do not need to navigate to a different record to see the information needed. However, you might have requirements where you want to check a field value of the quick view form control in order to apply specific behavior on the main form. In this post, we will see how to access Quick View Form fields through JavaScript in Dynamics 365 . For the sake of this post, we will work with a contact quick view form control on the account form. Based on the primary contact Relationship Type field, a specific section on the account form will be displayed. Relationship Type field of the contact = Employee => Employee ...

FILTER LOOKUP IN DYNAMICS 365 BASED ON CONDITION IN JAVASCRIPT WITH ADDCUSTOMFILTER AND ADDPRESEARCH

Image
Working with lookups provide you many possibilities regarding filtering the records displayed in the lookup. You can do it by using the out of the box related records filtering in the lookup field properties, however and in some scenarios, this approach does not provide what you need. Therefore, writing JavaScript code is mandatory to achieve what you want, which is the topic of this post In other post, we saw how to filter lookup field in JavaScript using the function addCustomView In this post, we will see how to filter a lookup field in JavaScript using addPreSearch and addCustomFilter functions . For the sake of this post, we will filter the Parent Account lookup in the account form based on the category field ...

SELECT ALL OPTIONS OF MULTISELECT OPTION SET (CHOICES) FIELD IN DYNAMICS 365

Image
Multiselect or Choices field type has some limitations when working with them. One of these limitations is to select all the options by default. Since the multiselect fields cannot be used in business rules, therefore, you have to go with JavaScript to achieve this need. For this post, we will see how to select all options of a Multiselect / Choices field in Dynamics 365 . The below code snippet will get all the available options in the multiselect field and will select them all // Select all the options in the multiselection field on create form function setMultiselectDefaultValues(context) { var formContext = context.getFormContext(); if (formContext.ui.getFormType() === 1) { var options = formContext.getAttribute("cak_multiselectoptionfieldcode").getOptions(); // ...

CALL JAVASCRIPT FUNCTION ON SUBGRID REFRESH IN DYNAMICS 365

Image
In this post, we will see an example on how to call JavaScript function when a subgrid is refreshed in Dynamics 365 . Sometimes, you have a scenario where you need to call a JavaScript function when a sub-grid refresh event has occurred in the form. The subgrid refresh event might happen if a record is added to the subgrid, deleted from the subgrid, or the refresh button of the subgrid is clicked. In order to trigger the function, you have to register a function on the OnLoad event of the form (in the below exemple manageFormBasedOnRecordsInSubgrid ) which in turns, will call the OnLoad event of the subgrid After doing this, each time you add/delete record from sub-grid, the function functionTriggerredgetOnSubgridRefresh() will be triggered. Below, is a sample code for the function that will ...

ERROR DIALOG IN DYNAMICS 365 JAVASCRIPT

Image
In another posts, we saw how to display a Confirm Dialog in Dynamics 365 using JavaScript and how to display an Alert Dialog in Dynamics 365 using JavaScript In this post, we will see how to display an Error Dialog in Dynamics 365 using JavaScript and what are the different properties that can be set. The error dialog will be used to display an error message on the form errorOptions Contains the different dialog display properties: details Contains details about the error. If specified, the user will be able to click the button Download Log File to download a text file that contains the message specified in this op...

ALERT DIALOG IN DYNAMICS 365 JAVASCRIPT

Image
In another post, we saw how to display a Confirm Dialog in Dynamics 365 using JavaScript In this post, we will see how to display an Alert Dialog in Dynamics 365 using JavaScript and what are the different properties that can be set. The alert dialog will be used to display an alert dialog containing a message and a button alertStrings Contains the different dialog display properties: confirmButtonLabel Contains the value that will be displayed for the alert button in the alert dialog. By default it is set to Ok . title Contains the value of the Title that will be displayed in the aler...

CONFIRM DIALOG IN DYNAMICS 365 JAVASCRIPT

Image
In another post, we saw how to display an Alert Dialog in Dynamics 365 using JavaScript In this post, we will see how to display a Confirm Dialog in Dynamics 365 using JavaScript and what are the different properties that can be set. The confirm dialog will be used to display a confirmation dialog containing a message and two button. This dialog will have the below different options confirmStrings Contains the different dialog display properties: confirmButtonLabel Contains the value that will be displayed for the confirm button in the confirmation dialog. By default it is set to Ok . cancelButtonLabel ...

DYNAMICS 365 FORM NOTIFICATION AND FIELD NOTIFICATION

Image
In a previous post, we saw how to display a global notification that lives across the Model driven App . In Dynamics 365 form, you can display two types of notifications: Form Notifications and Field Notifications . In this article, we will see how to display Form Notifications and Field Notifications within an entity form using JavaScript . The form notification will be displayed on top of the form and the setFormNotification function is used to display form level notifications function displayFormNotification(context) { var formContext = context.getFormContext(); formContext.ui.setFormNotification("This is an INFORMATION form notification.", "INFO", "InformationNotificationId"); formCon...

DYNAMICS 365 AUTOMATICALLY EXPAND COLLAPSE BUSINESS PROCESS FLOW IN JAVASCRIPT

Image
As you know, in Dynamics 365, business process flows are used to ensure that the users follow the same steps in order to achieve specific operation. It leads the users through the process by guiding them on what should be entered during each phase of it. In the Unified Interface, the business process flow is by default collapsed and the user must click on it to see what is needed in this phase. However, you can still enhance this user experience, by dynamically showing and hiding the business process flow whenever it is needed. Therefore, the user can know when and what is needed in this phase of the process. In the following points, we will see how to dynamically collapse and expand a business process flow (BPF) in JavaScript . First thing to know is that the BPF can have three display states: Expanded, Collapsed, and Floating ...

DYNAMICS 365 HOW TO EXECUTE FETCHXML QUERIES IN WEB.API JAVASCRIPT

In Dynamics 365, you can use WebAPI calls to retrieve data using OData queries. However, when coming to more complex queries, the OData might become complicated to be built if not impossible. Therefore, using FetchXml queries in the WebAPI call is your best option. Querying using FetchXml have more advantages from the easiness to be generated using the Advanced Find and more readable, to using joins and aggregations. In this quick post, we will see how to execute FetchXml query using Xrm.WebApi in JavaScript . Prepare the FetchXml you want to execute whether from the Advanced Find, or any other FetchXml generator in the community Prepare the JavaScript function you want to call in order to execute WebAPI request The below function is used to retrieve the user roles ...

DYNAMICS 365 HOW TO RETRIEVE BUSINESS UNIT DEFAULT TEAM IN C# AND JAVASCRIPT

In Dynamics 365, each Business Unit can have one or more Teams under it, but has one Default Team that will be automatically created by the system each time a new Business Unit is created, and will have the same name as the Business Unit name. The membership of this team will be systematically managed, where users are automatically added to the team whenever users are added to the business unit and removed from the team whenever users are removed from the business unit. In addition, the default team can be used to simplify the management of the security roles when users have common permissions, where you can associate a security role to the Default Team with a set of permissions that will be given to all users in the business unit. On the other hand, Default Teams cannot be deleted and will have the field Team Type set to Owner ; therefore, they can own records if needed by setting it ...

HOW TO MAP LOOKUP FIELDS INTO CURRENT RECORD

Image
As a Dynamics 365 consultant, you absolutely faced the scenario when changing a lookup field on the form, to map fields of the record chosen in the lookup into fields on the main form you are working on. In this post, I will show you some methods how to map fields from a lookup into the main record fields . Based on the requirements, you can apply the appropriate approach that is suitable for your need. JavaScript The first approach is to use JavaScript that requires a technical consultant to write the needed functions that will be executed on change event of the lookup field. What happens when you apply this approach A retrieve request should be done to get the values from the lookup that will be set on the main...

DYNAMICS 365 HOW TO SET DATE FIELD IN APP FOR OUTLOOK

PROBLEM You might have faced the following script error when setting a Date field in CRM App for Outlook in JavaScript using the method formContext.getAttribute("<fieldName>").setValue(Date.now()); Error: Value should be of type: DateTime at h (https://<crmurl>/uclient/scripts/app.js?v=1.3.163-201103-224104:12:164) at b (https://<crmurl>/uclient/scripts/app.js?v=1.3.163-201103-224104:12:327) at t.prototype.setValue (https://<crmurl>/uclient/scripts/0.js?v =1.3.163-201103-224104:209:21956) at setDateReceptionField (Unknown script code:561:13) at e.prototype.executeFunction (https://<crmurl>/uclient/scripts/app.js?v=1.3.163-201103-224104:1659:50881) at e.prototype.execute (https://<crmurl>/uclient/scripts/app.js?v =1.3.163-201103-224104:1659:50686) at e.prototype._executeIndividualEvent (https://...

DYNAMICS 365 SHOW HIDE TIME PART OF DATETIME IN JAVASCRIPT

Image
Have you faced a situation where you wanted to conditionally show/hide the time part of a date-time field on a form? After upgrading from D365 V8.2 to D365 V9 (9.0.16.7) on-premises, there is abnormal white space around the ribbon buttons . This behavior was everywhere whether in the views, forms or other areas. You can do it using the ClientAPI JavaScript function formContext.getControl("<datetimefieldname>").setShowTime(true); to show the time part Or formContext.getControl("<datetimefieldname>").setShowTime(false); to hide the time part Bonus Tips: In addition to setting the visibility of the time part, you can get whether the time part is ...