Thursday, August 13, 2020

MultiSelect Field in Dynamics Portal - Easy Solution

In Dynamics 365 Version 9 we got a new Attribute Type i.e Multiselect Option.

But it is not yet supported in PowerApps Portal/Dynamics 365 Portal when they are added on the form.

Multiselect fields are not visible when we utilize them through Web forms or Entity forms in Portal.

So to solve it there are multiple solutions based on requirements.

1. Easy Solution


If you have a Multi select option with fixed Options or which are in less number then this will do good.

Step 1 :  

Create a text field which you will use to save the values of multi select Option from CRM.
Let us assume it as "new_multiselecttext" field.

Step 2 : 

Now we have to do some Custom JS coding to add our fixed Multi Select Option in Entity Form. So hide the text field for users on portal using below code.

$("#new_multiselecttext").hide();

Make sure this is added on the form in CRM to work.

Step 3 : 

Create a multiselect field now and append it to that field where you want to see.

var multiselect = $('<select multiple><option value="1">Red</option><option value="2">Blue</option><option value="3">Green</option><option value="4">Orange</option></select>');
    multiselect.insertAfter("#new_multiselecttext");

Step 4: 

Now to get the selected options to text field we need to add Change property.

multiselect.change(function () {
        ("#new_multiselecttext").val($(this).val());
    });

Step 5: 

It may look like below where Ctrl button is required to select multiple Options.


 

Whole Code:

$(document).ready(function () {
    var multiselect = $('<select multiple><option value="1">Red</option><option value="2">Blue</option><option value="3">Green</option><option value="4">Orange</option></select>');
    multiselect.insertAfter("#new_multiselecttext");
            $("#new_multiselecttext").hide();
    multiselect.change(function () {
    
      var values = $(this).val();
        $("#new_multiselecttext").val(values);
    });

});


Step 6: 

We are done with Portal side, now to save this values into our actual OOB Multiselect field, write a plugin code which gets triggered on Create or Update of that Text field.

public void Execute(IServiceProvider serviceProvider)
{
	var context = (IPluginExecutionContext)serviceProvider.GetService(typeof(IPluginExecutionContext));

	var entity = context.InputParameters["Target"] as Entity;

	if (entity.Attributes.ContainsKey("new_multiselecttext"))
{ OptionSetValueCollection colours = new OptionSetValueCollection(); foreach (var option in entity.GetAttributeValue("new_multiselecttext").Split(','))
{ colours.Add(new OptionSetValue(int.Parse(option))); } entity["new_favcolour"] = colours; } }

If you have more options which can change or get added in future then you can go for Second Part.. 



No comments:

Post a Comment

Show Sitemap Sub areas based on Security Roles

 I came across a requirement where we had to show the Sitemap certain areas only to the Application Admins and hide it from the Users. Yes i...