Sunday, August 2, 2020

Check User Roles - Power Automate

We'd have come across requirements to check the roles of a User in Power automate, but not have found any message to get a User Roles directly from the flow. So the way to do it is to retrieve the System User: Role N:N intermediate entity records and check the count.

There's a different challenge going this way! the intermediate entity name to be used for list Records action in the flow is not same as the name that we see in the CRM solution, we need to get that from the API service. You can get the same by searching for the relation in the page: https://[OrganisationName].api.crm [Version] .dynamics.com/api/data/v9.0/         (Source : https://ajitpatra.com/2019/08/26/microsoft-flow-retrieve-nn-records-in-d365/)
For a System user roles the entity name is systemuserrolescollection

Below is the list records action fetch xml to retrieve No. of roles out of roleName1/roleName2/roleName3 that a User with guid "XXXXXXXX-XXXX-XXXX-XXXX-XXXXXXXXXXXX" this guid can be passed directly or  can also be replaced with a variable as in the snapshot.
fetchxml for list records:

<fetch>
  <entity name="systemuserroles" >
    <attribute name="systemuserroleid" />
    <filter>
      <condition attribute="systemuserid" operator="eq" value="XXXXXXXX-XXXX-XXXX-XXXX-XXXXXXXXXXXX" />
    </filter>
    <link-entity name="role" from="roleid" to="roleid" link-type="inner" alias="Roles" >
      <filter type="or" >
        <condition attribute="name" operator="eq" value="roleName1" />
        <condition attribute="name" operator="eq" value="roleName2" />
        <condition attribute="name" operator="eq" value="roleName3 />
      </filter>
    </link-entity>
  </entity>
</fetch> 

The Length of the list - in this case  length(outputs('Retrieve_Creating_User_Roles_intent_roles_count')?['body']?['value']) returns non zero if User has at least one role out of above 3  mentioned. Replace Retrieve_Creating_User_Roles_intent_roles_count with the name of list records step.
And you are ready!

Wednesday, June 10, 2020

Set a Field on the Editable grid disabled.

Add the below script to JS webresource and add that to Form Libraries:
function setChildGridEditability(gridContext) {
    var childGrid = gridContext.getFormContext().data.entity;
    childGrid.attributes.getByName("attributeLogicalName").controls.get(0).setDisabled(true);
}
Register event handler - function setChildGridEditability on Editable Grid - OnRecordSelect event.

Set Pass execution context as first parameter  checked... and Ready!

Monday, March 16, 2020

Useful Powershell scripts

To Connect to CRM

1. Connect to CRM from Powershell Script using Command-line parameters(Ideal for using Script for automation purposes.)
# This PS snippet retreieves the Active Duplicate DEtection Rules frmo the crmDevOrg and activates them in crmTestOrg.
# The User  Name, Password and the Organization Name are to be updated before executing this.

## pass arguments OrganizationURL, UserName, Password
    [CmdletBinding()]
    param(
        [Parameter(Position=0, Mandatory=$true)] [string]$SourceOrganizationName,
        [Parameter(Position=1, Mandatory=$true)] [string]$SourceUserName,
        [Parameter(Position=2, Mandatory=$true)] [string]$SourcePassword,


Install-Module -Name Microsoft.Xrm.Data.Powershell -RequiredVersion 2.8.7 -Scope CurrentUser
$password = ConvertTo-SecureString $SourcePassword -AsPlainText -Force
$credentials = New-Object System.Management.Automation.PSCredential($SourceUserName,$password)
$crmDevOrg = Get-CrmConnection -Credential $credentials -DeploymentRegion NorthAmerica -OnlineType Office365 -OrganizationName $SourceOrganizationName  -MaxCrmConnectionTimeOutMinutes 5
 
2. Using interactive mode

Install-Module -Name Microsoft.Xrm.Data.Powershell -RequiredVersion 2.8.7 -Scope CurrentUser
$crmDevOrg = Get-CrmConnection –InteractiveMode 


3.Activate Duplicate detection rules with Powershell shell Script depending on the activatesd Rules in another Orgs - say - check form Source Org and ACtivate in the Destination Org

 This PS snippet retreieves the Active Duplicate DEtection Rules frmo the crmDevOrg and activates them in crmTestOrg.
# The User  Name, Password and the Organization Name are to be updated before executing this.

## pass arguments OrganizationURL, UserName, Password
    [CmdletBinding()]
    param(
        [Parameter(Position=0, Mandatory=$true)] [string]$DevOrganizationName,
        [Parameter(Position=1, Mandatory=$true)] [string]$DevUserName,
        [Parameter(Position=2, Mandatory=$true)] [string]$DevPassword,
 [Parameter(Position=3, Mandatory=$true)] [string]$TestOrganizationName,
        [Parameter(Position=4, Mandatory=$true)] [string]$TestUserName,
        [Parameter(Position=5, Mandatory=$true)] [string]$TestPassword
    )

Install-Module -Name Microsoft.Xrm.Data.Powershell -RequiredVersion 2.8.7 -Scope CurrentUser
$password = ConvertTo-SecureString $DevPassword -AsPlainText -Force
$credentials = New-Object System.Management.Automation.PSCredential($DevUserName,$password)
$crmDevOrg = Get-CrmConnection -Credential $credentials -DeploymentRegion NorthAmerica -OnlineType Office365 -OrganizationName $DevOrganizationName  -MaxCrmConnectionTimeOutMinutes 5
 
$duplicaterules = Get-CrmRecords -conn $crmDevOrg 'duplicaterule' -FilterAttribute statecode -FilterOperator eq -FilterValue Active
$duplicateruleIds = $duplicaterules['CrmRecords'] | select -ExpandProperty duplicateruleid

$password = ConvertTo-SecureString $TestPassword -AsPlainText -Force
$credentials = New-Object System.Management.Automation.PSCredential($TestUserName,$password)
$crmTestOrg = Get-CrmConnection -Credential $credentials -DeploymentRegion NorthAmerica -OnlineType Office365 -OrganizationName $TestOrganizationName  -MaxCrmConnectionTimeOutMinutes 5
 
foreach($ruleId in $duplicateruleIds)
        {
            write-host "publishing rule id: " $ruleId
            $ddRule_toPublish = New-Object Microsoft.Crm.Sdk.Messages.PublishDuplicateRuleRequest            
            $ddRule_toPublish.DuplicateRuleId= $ruleId
            $crmTestOrg.ExecuteCrmOrganizationRequest($ddRule_toPublish,$trace)  
            Write-Host "Rule Published"       
        }

Thursday, March 12, 2020

Set the Lookup Field value in Power automate, while using Common Data Service Connector (CDS)

When we try to set a Lookup field from the Flow, using a Guid field available in the flow, we get a Resource not found Segment for the segment error.
This is because, when the Unique If field is added directly for a Lookup, the Entity Schema name is not detected by the flow service.  Here's a Work around for it.
If the lookup target entity name is abc_list, then, first type in -  abc_lists/   and then choose the Guid field from the Dynamics entry window.