Sample Code – Update format of Autonumber attribute in Dataverse (Dynamics 365 / CRM)


At times we may have a requirement to update the format of the Auto Number dynamically / programmatically so that the new set of records take up the new format.

Let us understand through a simple example.

Right now for the below field, we have the auto-number format set as

{SEQNUM:4}


Below is the sample code to update the auto-number format programmatically.

It makes use of our RetrieveAttribute and UpdateAttribute requests.

https://docs.microsoft.com/en-us/dynamics365/customerengagement/on-premises/developer/create-auto-number-attributes?view=op-9-1#autonumberformat-options

We have retrieved the attribute then have specified a new auto number format, followed by update request.

After executing the above code successfully, and creating a new contact record we can see the new number generated as per the specified format.

Sample Code

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


        CrmServiceClient svc = new CrmServiceClient(ConnectionString);

        if (svc.IsReady)
        {
            var retAttributeRequest = ((RetrieveAttributeResponse)
                             svc.Execute(new RetrieveAttributeRequest
                             {
                                 EntityLogicalName = "contact",
                                 LogicalName = "cr59f_myautonumber",
                                 RetrieveAsIfPublished = true
                             })).AttributeMetadata;

            retAttributeRequest.AutoNumberFormat = "MyFormat-{SEQNUM:6}-{DATETIMEUTC:yyyyMMddhh}-{RANDSTRING:6}";

            var updateAttrRequest = new UpdateAttributeRequest
            {
                EntityName = "contact",
                Attribute = retAttributeRequest
            };

            var updateAttrResponse = (UpdateAttributeResponse)
                svc.Execute(updateAttrRequest);

Check other posts on Auto Number attribute –

Hope it helps..

Advertisements

Power Apps grid control in Model-driven apps (Dynamics 365 / CRM)


Power Apps grid control is a new read-only control, now auto-enabled as part of 2022 Release Wave 1 for Model-driven apps.

We can also enable it by navigating to Customization, selecting the entity, then the Controls tab, and clicking on Add Control option.

Select the Power Apps Grid control.


Here we have enabled it for the Web.

The different properties that can be set for the control are –

  • Jump bar – this will be disabled by default.
  • Reflow behavior
  • Allow filtering

With Jump bar disabled –


Enable the jump bar.

After enabling the jump bar, we get the option to filter by alphabets.

It will also support Infinite Scrolling

We can use Edit Columns to add, remove and order the columns for the view

Edit Filters allows us to edit the filters.

Any changes made to the column or filter can be saved as a personal view.

Also, Grid remembers the context, here we have filtered the record by search text = “Blue”

Let us open the first record.

On navigating back, the context is retained.

We can also show and hide the Edit Columns and Edit filters option on views from Power Platform Admin Center >> [Environment] >> Settings >> Features >> Grid and Views section


Hope it helps ..

Advertisements

Manage and share views in Modern Advanced Find – Dataverse (Dynamics 365 / CRM)


Let us have a quick look at this new feature introduced as part of 2022 Release Wave 1.

Make sure we have updated the environment to 2022 Release Wave 1.

Then navigate to [Environment] >> Settings >> Features

Switch On – Modern Advanced Find and Allow users to hide system view 


Reset Default View
is the only option available before we switch on the Modern Advanced find feature.


After enabling the Modern Advanced Find feature

We get the option to Search Views as well as Manage and share views


Search Views – It allows us to filter / search within the views.


Manage and share views – It opens the dialog listing the views.


We can sort by

  • Personal before system, A to Z
  • System before personal, A to Z
  • A to Z


To hide a view we can select hover and select the option to hide.

With Allow users to hide system views  option switched on, the user has the option to hide system view also, else he could only hide the personal views.


For System Views, we have the option to either Hide or Set as a default view.

For Personal Views, we have more options like 

Hide, Set as default view, Share etc. as shown below.

Learn everything about the new Modern Advanced Find View  – https://jukkaniiranen.com/2022/02/modern-advanced-find-test-drive/

https://powerapps.microsoft.com/en-us/blog/modern-advanced-find-with-enhanced-view-management-in-model-driven-apps/

Hope it helps..

Advertisements

How to – Use RetrieveAttributeChangeHistoryRequest to get audit data for an attribute (Dataverse/ Dynamics 365/ CRM)


We can use RetrieveAttributeChangeHistoryRequest to get the change history of a particular field / attribute of a record.

We need to set the Target and the AttributeLogicalName property of the request.

AuditDetails records of the RetrieveAttributeChangeHistoryResponse contains the detail of the audit records.

The Audit History records in CRM- 

AttributeAuditDetail contains the details of the changes made on the field’s value. It contains property like – objectid, userid, operation etc. as well as new value and the old value as shown below.

Sample Code (C#) 

 string ConnectionString = "AuthType = OAuth; " +
                  "AppId=51f81489-12ee-4a9e-aaae-a2591f45987d; " +
                  "Username=User1@xxxx.onmicrosoft.com; " +
                  "Password=*******; " +
                  "RedirectUri=app://58145B91-0C36-4500-8554-080854F2AC97;" +
                  "Url = https://xxx.crm.dynamics.com//;";


            CrmServiceClient svc = new CrmServiceClient(ConnectionString);

            if (svc.IsReady)
            {

                var attributeChangeHistoryReq = new RetrieveAttributeChangeHistoryRequest();

                attributeChangeHistoryReq.Target =
                    new EntityReference("incident", new Guid("0a9f62a8-90df-e311-9565-a45d36fc5fe8"));
                attributeChangeHistoryReq.AttributeLogicalName = "prioritycode";

                var attrChangeResponse = (RetrieveAttributeChangeHistoryResponse)svc.Execute(attributeChangeHistoryReq);
                var auditDetailCollection = attrChangeResponse.AuditDetailCollection;


                foreach (var auditDetails in auditDetailCollection.AuditDetails)
                {
                    // Type =  AttributeAuditDetail, AuditDetail, 
                    var type = auditDetails.GetType();

                    if (type == typeof(AttributeAuditDetail))
                    {
                        var attributeDetail = (AttributeAuditDetail)auditDetails;

                        var userName = attributeDetail.AuditRecord.GetAttributeValue<EntityReference>("userid").Name;
                        var operation = attributeDetail.AuditRecord.FormattedValues["operation"];
                        var action = attributeDetail.AuditRecord.FormattedValues["action"];
                        var createdOn = attributeDetail.AuditRecord.GetAttributeValue<DateTime>("createdon");
                        var newValue = attributeDetail.NewValue.FormattedValues["prioritycode"];
                        var oldValue = attributeDetail.OldValue?.FormattedValues["prioritycode"];

                    }
                }
            }

Check other posts on Audit

Hope it helps..

Advertisements

How to – Use AlwaysMoveRecordToOwnerBusinessUnit setting in Dynamics 365 / PowerApps/ Dataverse


The setting – AlwaysMoveRecordToOwnerBusinessUnit allows us to move a user to another business unit without moving all his owned records to that unit.

This makes sure that other users in the new business unit cannot access the user’s records from his previous business unit (unless they have organization-level access / or have a role in that unit / or records are shared)

Also Check –

EnableOwnershipAcrossBusinessUnits setting https://nishantrana.me/2022/01/05/how-to-change-users-business-unit-without-removing-the-security-roles-in-dynamics-365-powerapps-enableownershipacrossbusinessunits-setting/

Modernize Business Units https://nishantrana.me/2022/01/04/modernize-business-units-matrix-data-access-structure-record-ownership-across-business-units-preview-in-dynamics-365-dataverse/

By default this setting is true.

Get the Organization Settings Editor – https://github.com/seanmcne/OrgDbOrgSettings/releases

Let us see the default behavior first, below are the 2 contact records created by User 2 who belongs to BU 1.

Now let us change the Business Unit of User 2 to BU 2.

After changing the BU of the user 2 we have assigned the same security role to the user which he has had in BU 1 that gave him BU level access on the Contact records.

We can see both Owner and Owning Business Unit getting updated as expected.

Now we’d see what will happen if we update the setting AlwaysMoveRecordToOwnerBusinessUnit to False.

But before doing that let us change the business unit of User 2 back to BU 1.

As expected Owning Business Unit is updated back to BU 1

Now let us update the setting to false

This time changing the business unit of User 2 to BU 2 should not update the Owning Business Unit of the contact records owned by User 2 to BU2.

As expected this time the Owning Business Unit remained BU 1.

Based on the true or false value set for AlwaysMoveRecordToOwnerBusinessUnit , we can see the checkbox “Move records to new business unit” either checked or unchecked – but always DISABLED.

If AlwaysMoveRecordToOwnerBusinessUnit is true than – the disabled checkbox is checked – 

cbu

Get more details below –

https://docs.microsoft.com/en-us/powerapps/developer/data-platform/configure-entity-relationship-cascading-behavior#allowed-record-ownership-across-business-unites-is-enabled

Hope it helps..

Advertisements

How to – change user’s business unit without removing the security roles in Dynamics 365 / PowerApps (EnableOwnershipAcrossBusinessUnits setting)


When we change the user’s business unit, all the current security roles of the users are removed and we need to assign the roles again to the user. This has always been the default behavior. 

Also check – Modernize Business Units –  https://nishantrana.me/2022/01/04/modernize-business-units-matrix-data-access-structure-record-ownership-across-business-units-preview-in-dynamics-365-dataverse/

For E.g. below user User 2 belongs to Business Unit – BU 1 and has the following security roles assigned.

Now changing the user’s business unit to BU 2

will remove all his security roles assigned.

We can now override this behavior by updating the new option /setting added

DoNotRemoveRolesOnChangeBusinessUnit(this property determines if roles are removed when the principal changes business units) through the
Organization Settings Editor tool

After we have installed the managed solution, we can update the setting and set it as true

Let us assign the security roles to User 2 in BU 2.

Let us now change the BU of user 2 back to BU 1.


As expected after updating that setting – DoNotRemoveRolesOnChangeBusinessUnit– as true – we can see the security roles still intact, even on the change of Business Unit for the user.

What happens if user 2 is assigned a security role – BU2 Security Role, which is created in BU 2 Business unit and is not available in BU 1.

Let us change the business unit to BU 1.

As expected BU2 security role is not available in BU 1, so that role is not assigned, only the common security role coming from parent BU remains intact.

Hope it helps..

Advertisements