Dataverse Web API | JScript | EDM.Date conversion issue

September 15, 2021 Leave a comment

While triggering ‘Create’ Action from jScript using the Web API we were getting following Edm.date conversion exception:

An error occurred while validating input parameters: Microsoft.OData.ODataException: Cannot convert the literal ‘2021-09-22T18:30:00’ to the expected type ‘Edm.Date’. —> System.FormatException: String ‘2021-09-22T18:30:00’ was not recognized as a valid Edm.Date.

Reason:

  • In the Create Request payload, there was a Date value which was causing Edm.Date conversion issue.
  • Following is the code snippet used to create a record for the custom table ‘raj_jobhistory’:
var entity = {};
entity.raj_companyname = "Microsoft";
entity.raj_startdate = Date.now();
entity["raj_employer@odata.bind"] = "/raj_employers(8D34D0F4-3F11-EC11-B6E6-000D3A3BA21F)";

Xrm.WebApi.online.createRecord("raj_jobhistory", entity).then(
    function success(result) {
        var newEntityId = result.id;
    },
    function(error) {
        Xrm.Utility.alertDialog(error.message);
    }
);
  • In the above script, ‘raj_startdate’ is ‘Date only‘ field which was being set to ‘Date.now()’.
  • For ‘Date only‘ field, Web API accepts the ‘yyyy-mm-dd‘ format. Since the date format was not being sent as yyyy-mm-dd application throwing the exception.

Fix:

  • Use toISOString() function to change the date value format to yyyy-mm-dd.
  • Following is the modified script.
var entity = {};
entity.raj_companyname = "Microsoft";
// Use toISOString() to change the Date format to yyyy-mm-dd
entity.raj_startdate = Date.now().toISOString().split('T')[0];
entity["raj_employer@odata.bind"] = "/raj_employers(8D34D0F4-3F11-EC11-B6E6-000D3A3BA21F)";

Xrm.WebApi.online.createRecord("raj_jobhistory", entity).then(
    function success(result) {
        var newEntityId = result.id;
    },
    function(error) {
        Xrm.Utility.alertDialog(error.message);
    }
);
Notes:
  • EDM stands for Entity Data Model. OData service uses an abstract data model called  (EDM) to describe the exposed data in the service.
  • Refer this article on the usage of Dataverse Web API.

🙂

Categories: PowerApps

[Quick Tip] Short cut to open ‘Command Prompt’ pointing to a folder

September 6, 2021 Leave a comment

In this article, lets learn a shortcut to open Command Prompt pointing to a folder from a Windows OS machine.

Regular Approach:
  • To open a folder from a different drive than C, we use the following approach to point to the folder.
  • Above example, I am pointing the Command Prompt to “D:\E\Practice\S2S\packages\Microsoft.CrmSdk.XrmTooling.PluginRegistrationTool.9.1.0.24\tools” folder using ‘cd’ command.

Shortcut:
  • Go to the folder you want to point the Command Prompt to, and hit cmd.
  • ‘Command Prompt’ window opens up pointing to the folder you hit the cmd from.

🙂

Categories: Misc Tags: ,

Power Platform Tools | Developer Toolkit for Visual Studio 2019

September 1, 2021 3 comments

Power Platform Tools for Visual Studio supports the rapid creation, debugging, and deployment of plug-ins.

You may note that Power Platform Tools for Visual Studio is similar in appearance and function to the Developer Toolkit for Microsoft Dynamics CRM 2013.

While Power Platform Tools for Visual Studio is similar in appearance and function to the Developer Toolkit for Microsoft Dynamics CRM 2013, Power Platform Tools is a new product and completely independent of the Developer Toolkit.

Power Platform Tools is not directly compatible with any templates or projects from the Developer Toolkit and vice versa.

Steps to enable ‘Power Platform Tools’ extension in VS 2019:

  • From the Visual Studio 2019, click on ‘Extensions -> Manage Extensions’.
  • Expand the left navigation panel node Online > Visual Studio Marketplace. Search for “Power Platform Tools”, then click on ‘Download’.
  • Post Download, close all the Visual Studio instances and wait for few seconds, you would get below installer screen.
  • Click ‘Modify’ and complete the installation.
  • Post installation, open the Visual Studio and initiate ‘Create New Project’.
  • You should find ‘New Visual Studio Solution Template for Dynamics 365’ solution template.
  • Provide Project Name and click ‘Create’ which will open up the familiar ‘Developer Toolkit’.
Note:
  • After installing Power Platform Tools, you will not find any Power Platform Tools related menu items or views in the Visual Studio user interface until you create or load a Visual Studio solution that contains at least one project created from a Power Platform Tools template.

Refer Microsoft docs for detailed steps to install Power Platform Tools on Visual Studio.

🙂

Tip | Model Driven Apps | Client API | setSharedVariable and getSharedVariable

As we know Client-side scripting using JavaScript is one of the ways to apply custom business process logic for displaying data on a form in a model-driven app, In this article lets understand how to pass variables between event handlers (i.e., Different jScript functions registered as event handlers).

Lets understand this by first understanding the Form event pipeline.

Form event pipeline:

  • We can define up to 50 event handlers for each event. Each event handler is executed in the order that it is displayed in the Event Handlers section in the Events tab of the Form Properties dialog box.

setSharedVariable:

  • Sets the value of a variable to be used by a handler after the current handler completes.
  • Syntax : ExecutionContextObj.setSharedVariable(key, value);
    • Ex : ExecutionContextObj.setSharedVariable(“sharedAccountName“, formContext.getAttribute(“name”).getValue());

getSharedVariable:

  • Retrieves a variable set using the setSharedVariable method.
  • Syntax: var sharedVariable = ExecutionContextObj.getSharedVariable(key);
    • Xrm.Navigation.openAlertDialog({ text: ExecutionContextObj.getSharedVariable(“sharedAccountName“) });

🙂

Code Snippet | JScript | Capture BPF Stage Parameters

Below is code snippet to capture the BPF stage parameters up on the ‘Stage’ change using jscript.

function onload(executionContext) {
    var formContext = executionContext.getFormContext();
    // Register event handler on Process Stage Change
    formContext.data.process.addOnStageChange(onStageChange);
}

function onStageChange(executionContext) {
    var formContext = executionContext.getFormContext();
    var stage = formContext.data.process.getSelectedStage();
    // Get Stage Name
    var currStageName = stage.getName();
    // Show Stage Name
    Xrm.Navigation.openAlertDialog({ text: "Current StageName - " + currStageName });

    // Get Stage ID
    var stageID = stage.getID();
    // Get Entity Name
    var stageEntityName = stage.getEntityName();
    // Get Stage Status
    var stageStatus = stage.getStatus();
}
  • Save this script as web resource and register both the ‘onload’ and ‘onStageChane’ functions on form’s onload event.
  • Below is the example of script on Opportunity form.

🙂

Categories: PowerApps Tags: , ,

[Step by Step] Dataverse | Connect Cloud flow with Service Principal (Application User)

By default, Cloud flow Dataverse connectors run under the Owner (i.e., User who created the flow) context. When the flows move to different environment via solutions, connectors run under the user account who imported the Solution.

Making the flows run under interactive user accounts is not recommended as they cause confusion when we check the record’s audit for who updated the record. Its recommended to make the flow run under ‘Application User’, if the calling user can be a fixed account.

In this article lets see how to make the flow run under Application User using Connect the flow using Service Principal option.

High level design:

Following are the steps we gonna go through.

  • App registration in Azure Active Directory (AAD)
  • Create an Application User in Environment.
  • Create a Cloud Flow and connect with Application User.

App registration in Azure Active Directory (AAD)

  • Add a Secret and save the Secret.
  • Copy the Application ID and Tenant ID.
  • Refer this article for the detailed ‘App Registration’ steps.

Create an Application User in Environment

  • Click on ‘New app user’ and select ‘Business Unit’ and ‘Security Role(s)’.
  • Click on ‘Add an app’ and select the App registered in previous section.
  • You should see the ‘Application user’ listed as below.

Create a Cloud Flow and connect with Application User:

  • Connect to Maker portal and create a new Solution.
  • Click on New -> Cloud flow.
  • Click on ‘Connect with Service Principle’.
  • Provide the details captured in Azure Active Directory ‘App Registration’ section and click ‘Create’.
  • Now you should see that in ‘Connection references’ as below.
  • If you go back to the ‘Solution’, you would see a new entry ‘Connection Reference (preview)’ along with the flow.
  • With the ‘Connection Reference (preview)’, we can conveniently move flow to different environment using Solution export and import.
  • Lets proceed and complete the flow, which creates a ‘Contact’ record upon the creation of an ‘Account’.
  • Create an ‘Account’ from the ‘Customer Service Hub’ App.
  • A ‘Contact’ gets created triggered from the flow and Owner would the ‘Application User’.

Notes:
  • You can use ‘Run as’ option and make the ‘Dataverse’ run under one of the highlighted User contexts.

🙂

[Preview Feature] Model Driven Apps | Edit Command Bar

In this article lets explore the ‘Edit Command Bar’ preview feature available in Model Driven Apps designer. But first lets understand the following basics of Command Bar.

  • What are Command Bars?
  • Where do we find them in Model Driven App (i.e., Command bar locations)?

What are Command Bars?

  • Command bars are at the core of every model-driven app and appear in many different locations.
  • The group of commands associated with a specific location make up a command bar.

Command bar locations in Model Driven App:

  • Main grid. This command bar is displayed when using the left-hand navigation of an app to view a full page list of records in this table.
  • Main form. This command bar is displayed on the table’s main forms. This appears at the top of the form and is not the same as the associated view or subgrid view that will appear in different areas of the form.
  • Subgrid view. This command bar is displayed on forms of other tables that render this table’s data within a subgrid.
  • Associated view. This command bar is displayed on the form of a parent table when viewing related data in this table.
  • Quick actions. There isn’t a specialized entry point from the command designer because quick actions are edited from the main grid of the table.

Now that we got the basics, lets explore editing the Command bar.

Using ‘Edit command bar (preview)’:

  • To use ‘Edit command bar (preview)’ feature. Connect to Power Apps portal.
  • Create a new ‘Model-driven app from blank’ and select the ‘Modern app designer (preview)’.
  • Add a Page and link one of your Dataverse Table.
    • I’ve added ‘Student’ table as Page.
  • Now click on ‘…’ of ‘Student’ and click on ‘Edit command bar (preview)’.
  • From the options choose one of the ‘Command Bar’ location you would like to edit.
  • In the next screen, you will get all the available of Commands displayed as below.
    • At this point editing ‘Legacy button is not supported’. Refer the limitations here.
  • I am going to add a new ‘Command’ named ‘Open My Blog’ which opens up an URL.
  • Click on ‘+New command’ and provide details on the right pane.
  • Action property specifies what happens on the ‘Select’ of the ‘Command’. Below are 2 available options.
  • As I need to open URL on the select of my ‘Open My Blog’ command. Under Action’ choose ‘Run formula’ and in the formula bar add ‘Launch()’ Power Fx as below.
  • Click ‘Save and Publish’ and ‘Play’.
  • On your App, you should see the command and on select it redirects to the provided URL.

Refer the docs link for more details.

🙂

Categories: PowerApps Tags: ,

Dynamics 30 days trial | Changes in subscription steps

August 3, 2021 2 comments

As we know trials.dynamics.com would allow us to subscribe to Dynamics 30 days trial using our personal email (i.e., Live.com, etc…). With the recent changes, trials.dynamics.com redirecting us to the following portal.

Up on clicking ‘Try for free’ its mandating us to enter “Either Work or school email ID”.

Which means we can’t sign up using personal email. But wait, when you give the personal email and try, you get an error message and a ‘Click here’ link, which allows you to sign up using personal email.

Click on ‘Click here’ and you will be redirected to the classic ‘Sign up’ screen.

Alternately you can directly use this link to go to the above Sign up page directly by passing new portal.

Hope this info saves you sometime 🙂

[Step by Step] Postman tool with Microsoft Dataverse Web API

July 21, 2021 3 comments

Couple of years ago, I’ve blogged Postman tool using Dynamics 365 online. In this article, lets go through the steps to connect Postman tool with Dataverse Web API.

While most of the steps are unchanged, there is a simplified way now to connect to Dataverse Web API with out the need of registering App in Azure AD. Lets get started.

Pre-requisites:

  • Power Apps Environment (Subscribe to 30 days trial, if you have not one).
  • Postman Desktop App. (Download here)
    • Note: These steps will not work with Postman web version.

Get Dataverse Environment URL:

  • Connect to Power Apps portal.
  • Select the ‘Environment’ you wish to connect from Postman tool.
  • Click on ‘Settings -> Advanced Settings’
  • Copy the URL up to the ‘dynamics.com/’ from the new window as shown below.
  • Add ‘.api’ before ‘.crm.dynamics.com’ to the URL.
  • Copy and Save this URL, which would be used in next steps.

Set up Postman Environment:

We would need ‘Workspace’ and ‘Environment’ to get started with Postman.

  • Open the Postman tool and create a new ‘Workspace’ if not having already.
  • Click on create a new ‘Environment’.
  • In the ‘Add Environment’ screen, we need to add Variables related to ‘Dataverse’ which helps Postman to connect Dataverse Web API.
  • Following 6 variables need to be configured.
Variable reference diagram
  • Lets add the first Variable “url“. In the ‘INITIAL VALUE’ paste the Dataverse environment url copied in previous section.
  • Repeat the step for next 5 variables and looks as below. Click ‘Add’ to save the Postman ‘Environment’.

Authorize Postman to Dataverse Environment:

Now that we have Postman Environment ready with required variables. Lets authorize and connect the Postman to Dataverse Web API. For that we need to complete ‘Authorization‘ and generate a new ‘Access Token’.

  • Select to the new Postman ‘Environment’ created in previous section.
  • Create a new GET ‘Request’ and select {{webapiurl}} variable as url.
  • Click on ‘Authorization’ tab and select ‘TYPE’ as ‘OAuth 2.0’ and click on ‘Get New Access Token’.
  • Provide details as below in the ‘Get New Access Token’ screen and click ‘Request Token’.
  • In the next screen, provide the Office 365 credentials.
  • Post successful authentication, a new ‘Access Token’ gets generated as below. Click ‘Use Token’.
  • ‘Access Token’ expires every 1 hour and you would get “401 Unauthorized’ exception upon expiry.
  • Click on ‘Get New Access Token’ if the Token expires to get a new Access Token. From 2nd time, Token generates instantly without any further sign-in.

Connect Postman to Dataverse Web API and query:

As we completed ‘Authorization’ and got the ‘Access Token’, we are good to query Dataverse API.

  • Create a new GET ‘Request’ and select {{webapiurl}} variable as url.
  • In the ‘Authorization’ tab, select the ‘Access Token’ generated and click ‘Send’.
  • You should get the Response as below with 200 Status code.
  • Below is the sample to fetch Top 3 Account records.

🙂

Canvas Apps | Optimization | Quick way to find and clean unused variables

Many a times, in Canvas Apps we would have declared variables but no longer used them. In a complex App with substantial screens, its tedious to identify and clean up the unused variables.

In this article, lets learn how to identify unused variables and clean up. If you are unversed with ‘Variables’, refer this article.

On a high level, Set() is used to define Global variable and UpdateContext() or Navigate() are used to define Context variable (i.e., Scope limited to a screen).

To understand unused variables and clean better, I’ve built a simple Canvas App using below steps.

  • Create a new Canvas App from Power Apps portal.
  • On App ‘OnStart’ event, declared 4 Global variables using Set().
  • Add a new Screen with 3 buttons. As buttons Text, used the one of the global variables(i.e.,btnLabel1) declared in App ‘OnStart’. Repeated the same for other 2 buttons.
  • To understand Context variables, on “btn1’s” OnSelect declared 2 context variables using UpdateContext().
  • Now that we got both Global and Context variables, lets understand about variables in-depth.
  • Go to File -> Variables, you will find 2 tabs ‘Global’ and ‘Screen1’ (this is for Screen1 Context variables).
  • Now click on one of the Variables, and you will find 3 tabs:
    • Definitions : Statement where this variable was declared. In our case its App ‘OnStart’.
    • Uses : Where this Variable used.
    • Indirect uses :  Any control indirectly refers the variable. Lets see this in detail in next step.

  • Click on ‘Uses‘, you will find where ‘btnLabel3’ directly referred (i.e., Text of button 3).
  • To understand ‘Indirect uses‘, lets add a new Label and set the Text as “This is indirect label of a Button : ” & btn3.Text“.
  • In the above statement, ‘Button 3’ text was ‘Directly’ set to ‘btnLabel3’ variable, here new Label’s text was set to ‘Button 3’ text, which indirectly referring ‘btnLabel3’ variable.
  • Now, click on ‘Indirect uses‘ of “btnLabel3” variable and you would see the new Label’s text statement.
  • Coming back to the crux of this article, to identify a Unused variable, click on Variable and go to ‘Uses‘ tab. If its blank, you can navigate to the declaration statement from ‘Uses‘ and remove the statement.

🙂

Categories: CRM, PowerApps Tags: ,