Creating web resources

We can use script web resources to store our JavaScript libraries. Once created, they can be reused in multiple entities. Let's say we want to hide the manager lookup on a customer form based on the customer type. We only want to show the manager lookup if the customer type is a customer. Let's follow these steps to create web resources for our script:

  1. Navigate to Settings | Solutions and open the HIMBAPAutoService solution.
  2. Click on Web ResourcesNew and fill in the following details:
  • Name: CustomerLibrary.js
  • Display Name: CustomerLibrary.js
  • Type: Script (Jscript)
  1. Click on Text Editor and use the following script:
var HIMBAP = window.Sdk || {};
(function() {
this.customertypeOnChange = function(executionContext) {
//get formContext
var formContext = executionContext.getFormContext();
//validate if field is available and it's value is nont null
if (formContext.getAttribute("customertypecode") != null &&
formContext.getAttribute("customertypecode").getValue() !== null) {
//get customer type value
var customertype
formContext.getAttribute("customertypecode").getValue();
if (customertype == 3) {
//show manager field
formContext.getControl("him_manager").setVisible(true);
} else {
//hide manager field
formContext.getControl("him_manager").setVisible(false);
}
}
}}).call(HIMBAP);

In the preceding code, we have to define our namespace, HIMBAP, using the following line of code:

var HIMBAP = window.Sdk || {};

Here, we have created our customertypeOnChange function, which takes executionContext as a parameter. When associating our function with an entity form or field, we need to pass executionContext. Furthermore, we have retrieved formContext from executionContext. We can get an entity attribute reference using formContext and then we can get the value of the field using the getValue method. For the customer option, the value is 3, so we are checking whether the customer type is equal to 3. We want to make sure the manager field is visible; otherwise, we will be hiding it. Now that we have our script ready, click on Save to save our script changes.

When a web resource is saved for the first time, we don't need to publish it, but for web resources that have been re-edited after creation, we need to save and publish our changes.

Now, we will call our method on the customer entity form load and customer type field. We have changed the display name of the account entity to customer and the account form display name to customer. Follow these steps to associate our script function with these events:

  1. Navigate to the customer entity and double-click on the customer form to open it. We will associate our method with the OnChange event, as well as on the OnLoad form event.
  2. Double-click on the Relation Type field and click on the Event tab. First, we need to add our customer library. Later, we will call our method when the field changes. While adding the event, we can call our function using the following syntax:
<Namespace>.functionname

  1. We need to make sure that we enable Pass execution context as first parameter so that we can pass the context, like so:

  1. Similarly, we need to call this function when the account entity form loads. Adding our JavaScript library and event association should be completed by following the steps labeled with numbers in the following screenshot:

  1. After this, we will see that if we select any option other than Customer, it will hide the manager lookup:

  1. However, if we select Customer, it will show the manager lookup, as follows:

Now we know how to create a library and associate our function with an entity or field event. While planning to implement form validation, for example, setting a default value for the entity form field, hide/show entity form field, we can also consider using business rules. Business rules can be created using a visual designer, where we can apply different actions based on the condition. The business rule fires on load of the entity form and on change of the field where the business rule is associated. This is a very useful feature for business users or functional consultants as it does not require using any code. By using business rules, we can work with current entity fields, so if we need to apply form validation based on the value of a field from another entity, we need to use JavaScript.

You can find out more about business rules at https://docs.microsoft.com/en-us/dynamics365/customerengagement/on-premises/customize/create-business-rules-recommendations-apply-logic-formwhich should help you understand more about business rules as well as how to create and use them.

In the next section, we are going to discuss using the Web API.

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
3.15.226.173