Tag Archives: javascript
Trigger Power Automate Flow using JavaScript – Bi-Directional
Hi All, This blog will be a continuation of my previous blog – Trigger Power Automate Flow using JavaScript – Uni Directional Now, feedback is essential when sending a request to determine whether it was successfully performed or failed somewhere. You can accomplish this by forwarding a response back from where the flow was invoked. I’ll use the same situation as in my previous blog, where I send a notification by greeting a person’s name if exists, or else I will greet a friend. Check out my previous blog to learn how to build your Flow and JavaScript. Steps to pass the response back within Flow Step 1: Add a response that will be sent back from where the Flow was invoked. Quick Tip: I am checking if ‘Name’ is present in my dynamic content. If yes, then greet the person else greet a Friend Formula: if(contains(triggerBody()?[‘DynamicData’], ‘Name’), triggerBody()?[‘DynamicData’][‘Name’], ‘Friend’) Steps to add into the JavaScript Step 1: Initially we created JS to trigger the flow, now we will add the code snippet to accept the response from Flow. Add the following Code: Step 2: Trigger the JS and watch the output I get as Alert (I have used the console page to trigger my JS for example purposes) Hope this helps in achieving a response from the Power Automate Flow!
Dynamically filter required Fields/Columns from a Record’s BPF and Form and apply Requirement to those fields
Hi Everyone, Let me explain this topic with an example of a scenario that you might encounter. I’ll walk you through how to complete this scenario in a concise manner below. Use Case Let’s say you have a Table (Entity) with or without multiple Business Process Flows (BPFs) that include some required Fields and even on the Form. Even if the required fields are empty and you want to change a field or flag within the form and save the record. We must disable all required Fields on the Form and then re-enable the requirements for those fields. We can do this with some simple JavaScript code. Step 1: Find a trigger point for your JS function to be called.It can be done with a Ribbon Button or by manually changing a Field. Find the code you’ll need below. I’ll be using Ribbon Button to trigger my JS Function. Quick Tip: You cannot get any Attribute Value of a Field residing in BPF directly. You need to get the entire Control of the Field and then call its attribute values. Step 2: Register your JS function onto your Ribbon Workbench or OnChange of any Field on Form. Since I called my function using Ribbon Button, I used “OpportunityForm.executeMain” with Parameters; “CRM Parameter -> Primary Control“ If you’re calling the JS using Field OnChange, then register the function as “OpportunityForm.executeMain” and do pass ‘executionContext‘. In this case, your part of the script will change as below (use this if you use JS on your Form only) OUTPUT This is how all fields will have no requirement on the Form. I took this output before re-enabling the requirement level for the fields. That’s all, I hope this helped you
Make form read-only using JavaScript
One way to lock down a form is through JavaScript. Let’s go through how to do this by looking at the code. var formCustomizations = { disableForm: function (executionContext) { let formContext = executionContext.getFormContext(); let formControls = formContext.ui.controls; formControls.forEach(element => { if (element.getName() != “” && element.getName() != null) { element.setDisabled(true); } }); } } Here when the JavaScript Function Is called and the execution parameter is passed, the user can see the desired output. formContext.ui.controls; Provides the properties and methods to retrieve information about the user interface (UI) controls for several sub-components of the form. formControls.forEach(element => { if (element.getName() != “” && element.getName() != null) { element.setDisabled(true);} Here the forEach loop will traverse and see if there are any fields that are not blank and the field value is not null, then set all that fields to read only on that form. Hope this helps!
Cancel the running flows with JavaScript code
We will look at how we can use JavaScript to cancel flows that are running without canceling each flow manually. To do this, follow the steps below. Let’s look at a scenario where we are seeing the flows that are in a running state and are not terminating. Click on All runs, this navigates you to all flow runs view. When we are in the All runs view, we need to open the developer console in the browser. The developer console can be opened by clicking F12 (or holding fn and F12) on Windows. Or by right-clicking on the screen and clicking Inspect. In the developer console, navigate to the Console pane. To clear the console window, click CTRL + L. The console pane is where we will add the JavaScript code that will cancel the running flows. As can be seen in the next step. // Copy this first and run it in the console var jq = document.createElement(‘script’); jq.src=”https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js”; document.getElementsByTagName(‘head’)[0].appendChild(jq); // … give time for script to load, then type (or see below for non wait option) jQuery.noConflict() //Paste the same again var jq = document.createElement(‘script’); jq.src=”https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js”; document.getElementsByTagName(‘head’)[0].appendChild(jq); // … give time for script to load, then type (or see below for non wait option) jQuery.noConflict() // Then copy this in the console to start the cancelation of the flows. confirm = function () {return true;}; setInterval(function () { $(“.fl-StatusInCell:contains(‘Running’)”).parent().parent().find(‘.ms-DetailsRow-cell’).last().click(); $(‘button[name=”Cancel”]’).click(); },5000); Click the running code after the code is implemented in the console, and the output will be seen below Hope this Helps!
Switching of Forms using Option Set field in Javascript
Use Case – To Switch form based on the value selected from option set field. Steps – 1. We have a Option Set field – “Lead Type“ Based on the Option selected – the form will switch For example – “PMO Member” – On change of the option, it will switch to PMO Member Form “HR” – On change of the option, it will switch to HR Form 2. To get the current form Guid use the following in the console- 3. Below JavaScript Code- Hope this helps!!
How to Dynamically Filter Multi-Select Option set/Picklist in D365 CRM using JavaScript
In this blog, I am going to show how we can filter a field (multi-select option set) item dynamically based on another field (single-select option set) item. Let’s consider a use case, We have a field named ‘Application Area’ which is a (single-select option set) field and ‘Application Type’ which is a (multi-select option set) field. Let’s have a look at JavaScript where the magic is making this possible. Step 1: Below is the entire code of JS For Table/Entity Main Form customization, select ‘Form Properties’ to include JavaScript function as below, On Load: oApplicationAreaType.Main On Change of field ‘Application Area’: oApplicationAreaType.getDetails Note: Pass the execution context for calling the JS function. That’s all, we have created a dynamic filtering of multi-select option set using JavaScript. Hope this helps!!
To show validation on a field using Regex Expression
In this blog, I am going to show how we can display a validation on the field if the entered text is not in the required format Let’s consider a use case, We have a field name ‘DMV Initial License’ which is a single line of text field. When text is entered the Date format for this field should be MM/YYYY Let’s have a look at JavaScript where the magic is making this possible. Step 1: Regex Expression for Date in the required format MM/YYYY – date_regex = /^(0[1-9]|1[0-2])\/[/]?([0-9]{4})$/ Step 2: Below is the entire code of JS For Table/Entity Main Form customization, select ‘Form Properties’ to include JavaScript function as below, On Change of field ‘ DMV Initial License ’: oCustomerFormCustomization.checkDateFormat Note: Pass the execution context for calling the JS function. Hope this helps!!
How to throw validation notification on Fields residing in Form Header
Hi All, Have you tried setting Field level notification on the Form Header of the Record? Let’s consider an example, We have an Estimated Revenue field which should never be set a $0.00. Therefore, we must throw a notification error based on this validation. The formula for setting field level notification is generalized as follows, Now let’s see how this general formula changes based on the field’s location set on the Form. If Field is located on the Form Formula will be: If Field is located on the Form Header Formula will be: If Field is located on the Form Business Process Flow (BPF) Formula will be: Hope this helps!!
Disable field on change of tab in D365 CE
Use case – Our requirement is to enable field description field on invoice line form on clicking of tab General. Let’s see how we can achieve this Solution – Step 1 – Create web resource with below function- var invoiceLineCustomization = { unlockField : function(executionContext) { var formContext = executionContext.getFormContext(); formContext.getControl(“description”).setDisabled(false); }, } Step 2: Add this web resource on tab property event TabStateChange and try. (path to go to event tab – Click on tab -> change properties -> event) Output – Hope this helps !
Disable field in Business Process Flow in D365 CE using JavaScript
Use case – Our requirement is to mark field Purchase time Frame disabled in Business process flow on change of disable field(custom field) on opportunity form. Let’s see how we can achieve this Solution – Step 1 – Create web resource with below function- Add header_process before schema name of field. Here schema name of purchase time frame field is purchasetimeframe. And after adding header_process_purchasetimeframe var opportunityCustomization = { //opportunityCustomization.disableField disableField : function(executionContext) { var formContext = executionContext.getFormContext(); var disable = formContext.getAttribute(“cf_disable”).getValue(); if (disable == true) { formContext.getControl(“header_process_purchasetimeframe”).setDisabled(true); } } } Step 2: Add this web resource on form load, on Save and on change of the field. And try Output – Hope this helps !