LINQ: GroupBy, Sum and EntityCollection

Just resharing a sample code that could be used for group by and sum operation on EntityCollection.

Suppose below is the output that we want

Continue reading “LINQ: GroupBy, Sum and EntityCollection”

Execute js in synchronise mode

Repost with some minor change ..

One limit of the OData endpoint is that the response can only include up to 50 records, so if your result set has more records it’s necessary to reiterate the request using the url inside the __next property contained inside the returned object.

The following code shows how to fetch the request using synchronous calls, because sometimes you need (or want) to block the user 🙂

Continue reading “Execute js in synchronise mode”

Hide ‘See associated records’ and ‘See all records’ on Sub Grid

As part of UCI two new buttons has been added to the Sub-grid, these are ‘See Associated records’ and ‘See all records’. On click of ‘See Associated records’ will lead us to Associated view of the open record and ‘See al record’ will lead to all records view.

But some time we need to hide those buttons then these button wont show up in ‘Ribbon Workbench’. Thus, to hide these buttons we need to remove from ‘Navigation’ from ‘entity’s form’, you need to just remove only respective navigation not all, ref below image ..

Continue reading “Hide ‘See associated records’ and ‘See all records’ on Sub Grid”

Filter Lookups on MSD 365

  • Out of box filter No Code: This just configuration on form.
  • Simple Lookup filter
function AddCustomFilterToLookup(formContext, fieldName, lookuptypes) {

//below fetchxml will be a custom filter to the lookup 
    var fetchxml = "<filter type='and'><condition attribute='ownerid' operator='eq-userid'/></filter>";
    formContext.getControl(fieldName).addCustomFilter(fetchxml, lookuptypes[0]);
}
  • Regarding Object lookup filter

//here fieldName = "regardingobjectid";
//     lookuptypes = ["contact", "account", "soft_cust_EntityName", "incident"];
//     formContext = executionContext.getFormContext(); 
function RegardingObjectFilter(formContext, fieldName, lookuptypes) {
    var control = formContext.getControl(fieldName);
    control.setEntityTypes(lookuptypes);
}

That’s it for now. Thanks!! 🙂

‘Back’ and ‘Save and close’ buttons are not working

Problem: Form navigation within multiple forms ‘Back’ and ‘Save and Close’ buttons stopped working.

Resolution

function Loadform(formId) {
 if (!Xrm.Page.ui.formSelector.getCurrentItem) { return; }
 var currentForm = Xrm.Page.ui.formSelector.getCurrentItem();
 if (currentForm && currentForm.getId() === formId) { return; }

 Xrm.Page.ui.formSelector.items.forEach(function (form) {
     if (form.getId() === formId)
         form.navigate(); });
 }

Hope it help!

Thank you!

Security roles JS updates

UCI has plenty of new features, one it is security roles fetch is very good one though did not creek. Good thing about it is, it will gives us all security roles along with its name in userSettings itself.

Here is code snippet of it

// Get Logged In User's Context
     var userSettings = Xrm.Utility.getGlobalContext().userSettings;
     // Get Logged In User Security Roles
     var loggedInUsersecurityRolesGuids = userSettings.securityRoles;
     var loggedInUsersecurityRoles = userSettings.roles;
     var isUserAdmin = false;
     for (var i = 0; i < loggedInUsersecurityRolesGuids.length; i++) {
     if (loggedInUsersecurityRoles._collection[loggedInUsersecurityRolesGuids[i]].name === "System Administrator" ||         loggedInUsersecurityRoles._collection[loggedInUsersecurityRolesGuids[i]].name === "*System Administrator")     
{ isUserAdmin = true; } }

if (isUserAdmin) {
//do something      
     }

Thanks.

Open dynamics record in dialog/Modal popup using navigateTo Client API

In 2020 Release Wave 1 Update for Dynamics 365 introduced navigateTo Client API to open main form of dynamics record in dialog instead Xrm.Navigation.openForm. This helps record to open in new window or same window of browser.

Below, we have provided information about how to open the CRM record in dialog. So to open new form for the entity is by below code snippet ..

var parameters = {};
             parameters["soft_para1"] = formContext.data.entity.getId();
             parameters["soft_para2"] = formKeyString;
             parameters["soft_para3"] = 1;

var pageInput = {
             pageType: "entityrecord",
             entityName: "soft_entityLogicalName",
             data: parameters,
             formId: "71ca9461-3209-4740-bd62-7320af2ae89c"
         };  


Xrm.Navigation.navigateTo(pageInput, { target: 2 }).then(
         function success() {
             //do something 
             //do something
         },
         function error() { }
     );

To open existing record use below code snippet

var pageProperty = {
                     pageType: “entityrecord”,
                     entityName: “contact”,
                     formType: 2,
                     entityId: “979dfe31-0686-ea11-a811-000d3a579c9c”//guid of record
                  };

var navigationProperty = {
                 target: 2,
                 width: { value: 80, unit: “%” },
                 position: 1
               };
Xrm.Navigation.navigateTo(pageProperty, navigationProperty);

Now, to show some custom html page

var dialogParameters = {
 pageType: "webresource",//required
 webresourceName: "ab_/ModalDialog.html",//Html Webresource that will be shown
 data: data//optional
};
 
var navigationOptions = {
 target: 2,//use 1 if you want to open page inline or 2 to open it as dialog
 width: 400,
 height: 300,
 position: 1//1 to locate dialog in center and 2 to locate it on the side
};
 
Xrm.Navigation.navigateTo(dialogParameters, navigationOptions).then(
 function (returnValue){
 //you can add handling of resultValue here
 console.log(returnValue);
 //put your success handler here
 },
 function(e) {
 //put your error handler here
 });

hope it helps.

Thanks!

Get the list of the dirty fields on the Dynamics 365

To quickly get the list of all the dirty fields

Open the debugger tool and put the following script in the console window

Xrm.Page.data.entity.getDataXml();

Get logged in user’s Security roles names in UCI

Here is sample code to get security roles name of logged in user.

function GetLoggedInUserSecurityRoleNames() {
 // Get Logged In User Context
 var userSettings = Xrm.Utility.getGlobalContext().userSettings;
 // Get Logged In User Security Roles
 var loggedInUsersecurityRolesGuidArray = userSettings.securityRoles;
 var totalSecurityRolesArray = new Array();
 var rolesOutputText = "";

if (loggedInUsersecurityRolesGuidArray.length > 0) {
 Xrm.WebApi.retrieveMultipleRecords("roles", "?$select=name,roleid").then(
 function success(result) {
 if (result.entities.length > 0) {
 // Push Role Names and Role Ids to Array
 for (var rolesCount = 0; rolesCount < result.entities.length; rolesCount++) {
 totalSecurityRolesArray.push({ RoleName: result.entities[rolesCount].name, RoleId: result.entities[rolesCount].roleid });
 }

rolesOutputText = userSettings.userName + " has the below Security Roles\n------------------------------------\n";

// Compare the User Security Roles with Total Security Roles
 for (var userSecurityRolesCounter = 0; userSecurityRolesCounter < loggedInUsersecurityRolesGuidArray.length; userSecurityRolesCounter++) {
 for (var totalsecurityRolesCounter = 0; totalsecurityRolesCounter < totalSecurityRolesArray.length; totalsecurityRolesCounter++) {
 if (totalSecurityRolesArray[totalsecurityRolesCounter].RoleId.toLowerCase() == loggedInUsersecurityRolesGuidArray[userSecurityRolesCounter].toLowerCase()) {
 rolesOutputText += totalSecurityRolesArray[totalsecurityRolesCounter].RoleName + "\n";
 break;
 }
 }
 }
 }

// Show User Roles
 Xrm.Utility.alertDialog(rolesOutputText, null);
 },
 function (error) {
 // Show error
 Xrm.Utility.alertDialog(error.message, null);
 });
 }
}

Duplicate workflows in advanced find – CRM 365

While searching for the process in the Advanced Find of my MS Dynamics CRM 365 environment, I noticed that several “duplicates” of the process appeared. Although they had the same name, some of them were in Draft Mode and two were Active. One curious thing that I saw was few processes are same logic and steps, also, whenever I did a change in one of my Active workflow/business rule, the other was replicating the change. This made me had a hard time trying to figure out what was happening, specially because after searching the duplicates in the Default solution to delete them, only one appeared.

Continue reading “Duplicate workflows in advanced find – CRM 365”