Thursday, 26 December 2019

Refresh Parent form if a record is added or deleted from the subgrid

Hello Eveyone,

We had a requirement to refresh the main form if a record is added or removed from the subgrid. To achieve this we have written following javascript on load of the main form.
Declared global variable _rowcount to capture grid row count on form load and then comparing the count on load of the grid.

var _rowcount = 0;
function onLoad(executionContext) {
    var formContext = executionContext.getFormContext(); // get the form context
    var gridContext = formContext.getControl("Contacts_grid");// get the grid context
    //stores the row count of subgrid on load event of CRM Form
    _rowcount = gridContext.getGrid().getTotalRecordCount();
    gridContext.addOnLoad(myContactsGridOnloadFunction);

}
function myContactsGridOnloadFunction(executionContext) {
    var currentRowCount = null;
    var formContext = executionContext.getFormContext();
    currentRowCount = formContext.formContext.getControl("Contacts_grid").getGrid().getTotalRecordCount();
    if (currentRowCount != _rowcount) {
        _rowcount = currentRowCount;
        formContext.formContext.data.refresh();
    }
};      
 

Monday, 16 December 2019

Unit test cases for HTTP Requests


Hi Everone,

In recent past times observed external real-time integration unit tests were failing due to some reasons. Like client secret was changed for the token service, external server was down etc., Due to these issues our DevOps build pipeline got effected and VS test task was failing. To overcome these issues we need to fake our HTTP web requests also in unit test code. 
We followed below steps to fake HTTP Requests 
  1. Install HttpWebRequestWrapper from NuGet Packages
  2. Implement below code in your unit test file
var fakeResponseBody = Helper.SampleTokenResponse;
var fakeAzureKeyVaultResponse = "{\"value\":\"secretkey\",\"id\":\"https://azure.vault.azure.net/secrets/"}";
var fakeECSResponse = Helper.SoapSampleResponse;
using (new HttpWebRequestWrapperSession(
                new HttpWebRequestWrapperInterceptorCreator(req =>
               {
                   switch (req.HttpWebRequest.Host)
            {
                       case "login.microsoftonline.com":
                       case "login.windows.net":
                           return req.HttpWebResponseCreator.Create(fakeResponseBody);
                       case "azure.vault.azure.net":
                           return req.HttpWebResponseCreator.Create(fakeAzureKeyVaultResponse);
                       case "devapi.testserver.com":
                           return req.HttpWebResponseCreator.Create(fakeECSResponse);
                       default:
                           return req.HttpWebResponseCreator.Create(fakeECSResponse);
                   }
               })))
            {
                var result = context.ExecuteCodeActivity<GetSMARTActivities>(inputs, null);
                Assert.IsNotNull(result["SMARTActivities"]);
       }
That’s it. Note the above code is written using FakeXrm Easy library for Dynamics CRM custom workflow activity, you need to change it as per your requirements.
Let me know if you have any questions. Thanks!


Tuesday, 3 December 2019

Calculate Age using Microsoft Flow (Power Automate)

Hi Everyone,

We had a requirement to calculate age of the user every day and send birthday wishes to the user.
We achieved this using Microsoft flows with expression formulas.

1. Created two custom fields named as "Age" as whole number and "Upcoming birthday" as date time.

2. Created a flow in https://make.powerapps.com/ scheduled it for every day.
3. Initialize a variable to hold the Age of the current contact, named it as "Diff Years"
4. Initialize variable to hold upcomingbirth day in this year
5. Initialize another variable to hold upcoming birthday in next year.

6. Next connect to CDS and list the contact records. Provide the odata filter to query the contacts where upcoming birthday is less than or equals to today's date.

dk_upcomingbirthdate le @{utcNow('yyyy-MM-dd')}




7. Next click on Plus symbol to add action. Add Apply each action to process each contact record.

8. Set Age and upcoming birthday variables with below expressions.

Difference Years
 sub(int(utcNow('yyyy')),int(formatDateTime(items('Apply_to_each')?['birthdate'],'yyyy')))

       
 

Set current year birthday
       
concat(utcNow('yyyy'),'-',formatDateTime(items('Apply_to_each')?['birthdate'],'MM'),'-',formatDateTime(items('Apply_to_each')?['birthdate'],'dd'))
       
 

Set future year birthday
       
concat(add(int(utcNow('yyyy')), 1),'-',formatDateTime(items('Apply_to_each')?['birthdate'],'MM'),'-',formatDateTime(items('Apply_to_each')?['birthdate'],'dd'))
       
 
9. Next update the contact record. Set the Age and Upcoming birthday fields with below expressions
Age expression
if(or(less(int(utcNow('MM')), int(formatDateTime(items('Apply_to_each')?['birthdate'],'MM'))), 

and(equals(int(utcNow('MM')), int(formatDateTime(items('Apply_to_each')?['birthdate'],'MM'))),

less(int(utcNow('dd')), int(formatDateTime(items('Apply_to_each')?['birthdate'],'dd'))))),sub(variables('Years'),1),variables('Years'))
 
Upcoming birthday expression
       
if(less(int(utcNow('MM')),int(formatDateTime(items('Apply_to_each')?['birthdate'],'MM'))),
formatDateTime(variables('setDate'),'yyyy-MM-dd'),formatDateTime(variables('futurebirthday'),'yyyy-MM-dd'))