Sample code (C#) for Multi-Table Lookup / Polymorphic Lookup attribute in Dataverse / Dynamics 365


For creating multi-table lookup we already have an XrmToolBox plugin Polymorphic Lookup Creator which we should be using ideally, but in case somebody wants to try it out, the below shared sample code can be referred.

Below is our custom table named mycustomtable to which we’d add a multi-table lookup which references case, contact and account entity.

We need to call CreatePolymorphicLookupAttribute Action, which expects 3 parameters

Below is the sample code to create the polymorphic lookup.

string ConnectionString = "AuthType = OAuth; " +
                  "AppId=51f81489-12ee-4a9e-aaae-a2591f45987d; " +
                  "Username=user@domain.onmicrosoft.com; " +
                  "Password=pwd; " +
                  "RedirectUri=app://58145B91-0C36-4500-8554-080854F2AC97;" +
                  "Url = https://orgname.crm4.dynamics.com/;";


 CrmServiceClient svc = new CrmServiceClient(ConnectionString);

            if (svc.IsReady)
            {
                // Create PolymorphicLookupAttribute 
                // with mycustomtable custom entity / table
                // referencing case, contact and account entity
                var varOrgRequest = new OrganizationRequest();

                // specify the request name
                varOrgRequest.RequestName = "CreatePolymorphicLookupAttribute";

                // specify lookup attribute details
                varOrgRequest.Parameters["Lookup"] = new LookupAttributeMetadata()
                {
                    SchemaName = "crf82_mypolymorphiclookup",
                    DisplayName = new Label("My Polymorphic Lookup", 1033)
                };

                // referencing entity is our custom entity named my custom table
                // referenced entity is incident
                var oneToManyRelation1 = new OneToManyRelationshipMetadata();
                oneToManyRelation1.ReferencingEntity = "crf82_mycustomtable";
                oneToManyRelation1.ReferencedEntity = "incident";
                oneToManyRelation1.SchemaName = "crf82_mycustomtable_crf82_incident";

                // referencing entity is our custom entity named my custom table
                // referenced entity is contact
                var oneToManyRelation2 = new OneToManyRelationshipMetadata();
                oneToManyRelation2.ReferencingEntity = "crf82_mycustomtable";
                oneToManyRelation2.ReferencedEntity = "contact";
                oneToManyRelation2.SchemaName = "crf82_mycustomtable_crf82_contact";


                // referencing entity is our custom entity named my custom table
                // referenced entity is account
                var oneToManyRelation3 = new OneToManyRelationshipMetadata();
                oneToManyRelation3.ReferencingEntity = "crf82_mycustomtable";
                oneToManyRelation3.ReferencedEntity = "account";
                oneToManyRelation3.SchemaName = "crf82_mycustomtable_crf82_account";

                // populate OneToManyRelationships parameter of CreatePolymorphicLookupAttribute request
                varOrgRequest.Parameters["OneToManyRelationships"] = new OneToManyRelationshipMetadata[]
                {
                    oneToManyRelation1, oneToManyRelation2, oneToManyRelation3
                };

                // specify the existing solution name 
                varOrgRequest.Parameters["SolutionUniqueName"] = "MySolution";

                var response = svc.Execute(varOrgRequest);
            }

The lookup à

Sample code to add a new relationship to an existing lookup.

                var createOneToManyRelationshipRequest = new CreateOneToManyRequest();

                // referencing entity is our custom entity named mycustomtable
                // referenced entity is contact - add the entity to be added
                var oneToManyRelationAdd = new OneToManyRelationshipMetadata();
                oneToManyRelationAdd.ReferencingEntity = "crf82_mycustomtable";
                oneToManyRelationAdd.ReferencedEntity = "contact";
                oneToManyRelationAdd.SchemaName = "crf82_mycustomtable_crf82_contact";

                createOneToManyRelationshipRequest.OneToManyRelationship = oneToManyRelationAdd;


                // specify lookup attribute details to which new relationship is to be added
                createOneToManyRelationshipRequest.Parameters["Lookup"] = new LookupAttributeMetadata()
                {
                    SchemaName = "crf82_mypolymorphiclookup",
                    DisplayName = new Label("My Polymorphic Lookup", 1033)
                };


                var createOneToManyRelationshipResponse = svc.Execute(createOneToManyRelationshipRequest);

Sample code to remove relationship from an existing lookup.

 var deleteRelationShip = new DeleteRelationshipRequest();
                // specify schema name of the relationship 
                deleteRelationShip.Name = "crf82_mycustomtable_crf82_contact1";
                svc.Execute(deleteRelationShip);

Sample code to delete the lookup

  // User delete attribute request 
                DeleteAttributeRequest varDelRequest = new DeleteAttributeRequest();

                // specify the entity name
                varDelRequest.EntityLogicalName = "crf82_mycustomtable";

                // specify the schema name of the entity
                varDelRequest.LogicalName = "crf82_mymultitablelookup";
                svc.Execute(varDelRequest);

Get all the details here –

https://docs.microsoft.com/en-us/powerapps/developer/data-platform/webapi/multitable-lookup?branch=pr-en-us-4448

Hope it helps..

Advertisements

Can we update ‘SYSTEM’ user in Dynamics 365 / Dataverse?


We recently were asked to check the possibility of updating the SYSTEM user specifically the primary email field of it.

https://docs.microsoft.com/en-us/power-platform/admin/system-application-users

Usually, we use SYSTEM user to specify the plugin’s execution context for business requirements that needs elevation of privileges.

And also we can neither update it from the user interface nor it is recommended to update the SYSTEM user.

If we try to update the user through code, we’d get the below error message

Error updating Users – No modifications to the ‘SYSTEM’ or ‘INTEGRATION’ user are permitted. [See the Execution Plan tab for details of where this error occurred]


Hope it helps..

 

Advertisements

Update Personal Options / Personalization Settings using UpdateUserSettingsSystemUser Request – Dynamics 365


Recently we had a requirement to update the “Negative Currency Format” – Regional Options for all the users.

We could not find this option in our favorite plugin – User Settings Utility.

So to programmatically update it we use the below code.

Or using our most favorite plugin – SQL 4 CDS

Updating it to the required format – 5

The Result –

Get more details here-

https://docs.microsoft.com/en-us/dynamics365/customer-engagement/web-api/usersettings?view=dynamics-ce-odata-9

Hope it helps..

C# Code –

  CrmServiceClient svc = new CrmServiceClient(ConnectionString);
            
            if (svc.IsReady)
            {
                var reqUpdateUserSettings = new UpdateUserSettingsSystemUserRequest();

                // GUID of the System User
                reqUpdateUserSettings.UserId = new Guid("18392d47-9dc0-eb11-8235-00224808ff23");

                // reference the usersettings Entity
                reqUpdateUserSettings.Settings = new Entity("usersettings");

                // update the negativecurrencyformatcode to appropriate integer value
                reqUpdateUserSettings.Settings.Attributes["negativecurrencyformatcode"] = 5;

                var response = (UpdateUserSettingsSystemUserResponse)
                    svc.Execute(reqUpdateUserSettings);
            }

SQL –

update usersettings 
set negativecurrencyformatcode = 5
where systemuserid = '18392d47-9dc0-eb11-8235-00224808ff23'
Advertisements

How to – Hide and show controls in Canvas App based on security role of the user (Dataverse / Dynamics 365)


Suppose we have the below Maker and Checker Canvas App, with two button one for raising the request and the other for approving and rejecting the request.

We have below custom security roles created in our Dynamics 365

  • Maker
  • Checker

Here we want Submit a Request button to be visible only to users with Maker security role and the Approve and Reject Request button to users with Checker role.

For this we can write the below formula on Visible property of the button.

Add the Users and Security Roles data sources first.

If(

LookUp([@’Security Roles’], Name = “Maker”, Role) in Concat(LookUp([@Users], ‘Primary Email’ = User().Email).’Security Roles (systemuserroles_association)’, Role & “;”),

true,

false

)

 

Based on email address of the user, we are comparing the Maker security role with all the different roles assigned to the user, which we are fetching from systemuserroles_association.

  • User function in Power Apps

https://docs.microsoft.com/en-us/powerapps/maker/canvas-apps/functions/function-user

  • Lookup function in Power Apps

https://docs.microsoft.com/en-us/powerapps/maker/canvas-apps/functions/function-filter-lookup

Hope it helps..

If(LookUp([@'Security Roles'], Name = "Maker", Role) in Concat(LookUp([@Users], 'Primary Email' = User().Email).'Security Roles (systemuserroles_association)', Role & ";"),
true,
false)
Advertisements

Interactive login option in CDS/CRM Connection Manager in KingswaySoft Dynamics 365 Integration Toolkit


With the new release, the CDS/CRM connection manager adds a new Interactive Login option in the CDS/CRM Connection Manager for Authentication Type as OAuth.

Interactive login allows the user to log in using his account details (to establish the connection with CRM) without the need for registering the application in the Azure Active Directory.

This is supposed to be used only during design time.

Enter User Name and the CDS/CRM URL and click on Test Connection.

The login screen pops up, where we can enter the credentials and sign in.

We’d receive the Test connection succeeded message.

Now we are ready to use the CRM Connection.

Now when we will run the package from within the Visual Studio (SSDT), it will again ask for entering the credentials.

The other option is to use the OAuth Type Password along with default Client App ID and Redirect URL

https://docs.microsoft.com/en-us/powerapps/developer/data-platform/xrm-tooling/use-connection-strings-xrm-tooling-connect

Hope it helps..

Advertisements

Enable parent check option during Merge – Dynamics 365


The duplicate detection/merge feature has been optimized in the platform

https://nishantrana.me/2020/10/29/improved-duplicate-detection-and-merge-experience-in-dynamics-365-for-sales-2020-release-wave-2/

and now uses the new interface replacing the legacy web client.

The legacy experience –

The enhanced experience – now with merge option.

Merge allows to select up to 2 records to merge. Selecting more than 2 records to merge gives the below error.

And Merge button will also get disabled.

Below are the different options available while performing Merge.

  • Merge records by choosing fields with data.
  • View fields with conflicting data.
  • Enable Parent check

In case of qualify we are able to select 2 matching records for merge

dd2records

The merge process is always asynchronous. The merge also merges any child records.

As per Microsoft Docs, to find the progress of the cascading operation triggered as part of the merge, navigate to System Jobs in Audit and Logs settings within the Admin portal.

Filter by Cascade Operations

The Enable parent check option, if checked, will result in failure during merge operations if records are deleted while the system job is running. We need to uncheck this option if we want merge to continue even if a record is deleted while the Merge job is running.

There could be other reasons because of which merge operations could fail.

Get all the details here –

https://markcarrington.dev/2020/08/14/msdyn365-internals-async-record-merging/

Hope it helps..

Advertisements