Few points on UTCNow and FormatDateTime – Power Automate / Dataverse


Recently we wrote a flow that will run daily once and will pick all the tasks due in the last 24 hours i.e. schedule end date less than equal to utcNow() and greater than equal to addDays(UTCNow(),-1)

Interestingly we observed one of the task records not picked.

The scheduled end date on the task record was – 2024-07-22T20:00:00Z

And for the flow the filter condition was –

scheduledend le 2024-07-23T20:00:35.5173871Z and scheduledend ge 2024-07-22T20:00:35.5173943Z

If we look at the date for the greater than equal condition, we can see that the seconds part is 35, the exact time when the List rows step would have run, and in case of that particular task record is 00, so it was not picked.

Then we applied the below formatDateTime function, excluding the time part.

(scheduledend le ‘@{formatDateTime(utcNow(),’yyyy-MM-dd’)}’ and scheduledend ge ‘@{formatDateTime(addDays(utcNow(),-1),’yyyy-MM-dd’)}’ and _regardingobjectid_value ne null and statecode eq 0)

Again we saw few tasks not picked,

The records that were not picked had scheduledenddate as

  • 2024-07-25 18:00:00.000
  • 2024-07-25 19:00:00.000

And as per new condition

scheduledend = ‘2024-07-24’ which essentially was

scheduledend = ‘2024-07-24 00:00:00.0000’

Eventually we updated the flow’s Filter Rows condition to include only the hour and minutes, ignoring the seconds/milliseconds because of which we got the issue in the first place.

(scheduledend le ‘@{formatDateTime(utcNow(),’yyyy-MM-dd HH:mm’)}’ and scheduledend ge ‘@{formatDateTime(addDays(utcNow(),-1),’yyyy-MM-dd HH:mm’)}’ and _regardingobjectid_value ne null and statecode eq 0)

One more example for more clarity –

Below we are creating a contact record and setting values for 3 date time fields, UTC1, UTC2, UTC3.

  • UTC1 = utcNow()
  • UTC2 = formatDateTime(utcNow(),’yyyy-MM-dd’)
  • UTC3 = formatDateTime(utcNow(),’yyyy-MM-dd HH:mm’)

The values for those fields inside CRM’s form –

A screenshot of a computer

Description automatically generated

The corresponding values within the Dataverse/ CRM’s database (UTC) –

A screenshot of a computer

Description automatically generated

Hope it helps..

Fixed – Action Failed: A record with matching key values already exists (DuplicateRecord)– Power Automate / Dataverse


Recently while testing one of our old existing flows, we got the below error

Action ‘Create_Child_Log_Record’ failed: A record with matching key values already exists.

{

“error”: {

“code”: “0x80040237”,

“message”: “A record with matching key values already exists.”,

“@Microsoft.PowerApps.CDS.ErrorDetails.ApiExceptionSourceKey”: “Plugin/Microsoft.Crm.ObjectModel.CustomBusinessEntityService”,

“@Microsoft.PowerApps.CDS.ErrorDetails.ApiStepKey”: “38200c13-a28e-ee11-be36-002248933483”,

“@Microsoft.PowerApps.CDS.ErrorDetails.ApiDepthKey”: “1”,

“@Microsoft.PowerApps.CDS.ErrorDetails.ApiExceptionMessageName”: “DuplicateRecord”,

“@Microsoft.PowerApps.CDS.ErrorDetails.ApiExceptionHttpStatusCode”: “412”,

“@Microsoft.PowerApps.CDS.ErrorDetails.SqlExceptionMessage”: “Violation of PRIMARY KEY constraint ‘PK_childBase’. Cannot insert duplicate key in object ‘dbo.bew_logBase’. The duplicate key value is .”,

“@Microsoft.PowerApps.CDS.HelpLink”: “http://go.microsoft.com/fwlink/?LinkID=398563&error=Microsoft.Crm.CrmException%3a80040237&client=platform”,

“@Microsoft.PowerApps.CDS.InnerError.Message”: “Cannot insert duplicate key.”

}

}

Basically on Create or Update of the Parent Record it was creating a corresponding child log record, recording changes in the status field of the parent record.

The issue was in the Create Child Log record step, here it was setting the Primary Key Field / GUID field of the Child Log record being created with the GUID of the Parent record.

This worked for the 1st record, but when trying to create any new record with the same parent’s GUID, it was throwing the duplicate exception as it would be the same parent GUID getting specified.

On clearing that field, and letting the system generate the GUID, the flow ran successfully.

Hope it helps..

Advertisements

Enable / Disable (turn on / turn off) multiple cloud flows using Code – Power Automate


Sharing a sample code through which we can Enable / Disable (turn on / turn off) multiple cloud flows using code.

E.g. here we want to turn on the below Cloud Flows owned by a specific user.

A screenshot of a computer

Description automatically generated

Table name – Workflow and Category – Modern Flow.

Below is the sample code, code is straightforward, we are updating the status of the record.

const string connectionString = "AuthType = ClientSecret; " +
                                         "Url = https://[org].crm.dynamics.com/;" +
                                         "ClientId=[GUID];" +
                                         "ClientSecret=[Secret]";

        var myServiceClient = new CrmServiceClient(connectionString);
        if (myServiceClient.IsReady)
        {
            var query = new QueryExpression("workflow");
            query.ColumnSet.AddColumns("workflowid", "name", "ownerid", "statecode", "category", "primaryentity", "solutionid");
            // Category = 5 (Modern Flow)
            query.Criteria.AddCondition("category", ConditionOperator.Equal, 5);
            // owned by a specific user
            query.Criteria.AddCondition("ownerid", ConditionOperator.Equal, "23d670c5-d02d-ee11-bdf4-0022482db7da");
            var cloudFlows = myServiceClient.RetrieveMultiple(query);
            foreach(var flow in cloudFlows.Entities)
            {
                var myFlow = new Entity("workflow", flow.Id);
                // statecode = 1 (Turn On) and  statecode = 0 (Turn Off)
                myFlow.Attributes["statecode"] = new OptionSetValue(1);
                myServiceClient.Update(myFlow);
            }        
        }

The result :

A screenshot of a computer

Description automatically generated

Check Work with cloud flows using code.

Hope it helps..

Advertisements

Fixed- Flow not getting triggered (Callback Registration)– Power Automate / Dataverse


Recently in one of our test environments, the out the box flow – “Deserialization of Inspection Definition” wasn’t getting triggered even when it was in the On state.

This was because the “CallbackRegistrationExpanderFilter” operation was failing with the below error.

Unexpected failure during ValidateUserAccessCached. Ex: System.ServiceModel.FaultException`1[Microsoft.Xrm.Sdk.OrganizationServiceFault]: The specified user(Id) is disabled. Consider enabling this user. Additional Details: User IsDisabled=True, IsLicensed=True. ImpersonatingSystemUserId. (Fault Detail is equal to Exception details:

ErrorCode: 0x80040225

Message: The specified user(Id = xxxx-xxxx-xxxx-xxx-xxxxxx) is disabled. Consider enabling this user. Additional Details: User IsDisabled=True, IsLicensed=True. ImpersonatingSystemUserId=xxxx-xxxx-xxxx-xxx-xxxxxx)

TimeStamp: 2024-05-20T21:50:42.6724270Z

More on Callback Registration Expander Job – https://rajeevpentyala.com/2021/01/30/callback-registration-expander-system-jobs-waiting-for-resources/

This was because the flow’s corresponding Callback Registration record’s Owner was a user whose account was now disabled in CRM.

So the option to fix was to Turn off and Turn On the flow, which will delete the old callback registration record and will create a new one with the user who is turning off / on the flow. Updating the owner of the flow will not work here, as it won’t update the existing callback registration record.

Also we had another observation, if a callback registration is owned by a user who is a proper / enabled user in CRM, simply turning off and on the flow using a different user account will not delete the existing callback registration record and create a new one with that particular user as the owner. In this case we will have to explicitly delete the existing callback registration record.

Hope it helps..

Advertisements

Fixed – Resource not found for the segment action error – Power Automate / Dataverse


Recently we got the error for one of our flows – Resource not found for the segment ‘dow_ArtefactMoveNotesAttacmentToBlob’.

The flow was running fine with no errors a couple of days back.

On trying to edit the flow we can see it not listing the action in the Perform an unbound action step, instead giving “Request to XRM API failed with error: ‘ Message:Code:InnerError:’.

A screenshot of a computer

Description automatically generated

Eventually, as the error message suggests, we realized that the dev team had unregistered the action and registered a new action in its place. Updating the action to use the correct / new action fixed the issue for us.

A computer screen shot of a computer screen

Description automatically generated

The error could also show up if the action is in the disabled state.

https://community.dynamics.com/forums/thread/details/?threadid=1069c036-683b-4178-a592-6deae37002eb

https://community.dynamics.com/forums/thread/details/?threadid=1069c036-683b-4178-a592-6deae37002eb

Hope it helps..

Advertisements

Use Filter Array action to remove values from an array variable – Power Automate


Suppose we have below array variable – varTestVariable having a list of GUIDs in it.

A screenshot of a computer

Description automatically generated

And we have another array variable varWorkOrderGuidToBeRemoved which has the list of GUIDs we want to remove from our first variable varTestVariable

A screenshot of a computer

Description automatically generated

For this, we can make use of the Filter array action

A screenshot of a computer

Description automatically generated

In From, first we have specified the array variable from which we want to remove the values, followed by the variable that holds the values to be removed. Next, we have specified the “does not contain” operator and finally specified the item()

We can also click on Edit in advanced mode to see or edit the expression.

We can use the Body of the Filter array action that will hold the result in subsequent actions.

A screenshot of a computer

Description automatically generated

On running the flow, we can see that the values that existed in the second array variable were removed from the first array variable.

A screenshot of a computer

Description automatically generated

Get more details – https://www.damobird365.com/efficien-union-except-and-intersect-great-method/

Hope it helps..

Advertisements