Dynamics CRM Javascript · Javascript

Dynamics CRM Prepopulate To Activity Party on Email when multiple/single records selected from grid

Recently we had a requirement to open Email entity form with To field prepopulated with the selected records from grid.

We have added custom button on grid using Ribbon Workbench Tool and added Action to call below Javascript on click on button.

function OpenQuickCreateemail(selectedItems) 
{
var partylist = [];

if (selectedItems.length == 1) {
            OpenQuickCreateemailsingleRecorde(selectedItems);
        }
        else 
		{
            for (var indxIds = 0; indxIds < selectedItems.length; indxIds++) 
			{
                        partylist[i] = new Object();
                        partylist[i].id = selectedItems[indxIds].Id;
                        partylist[i].name = selectedItems[indxIds].Name;
                        partylist[i].entityType = selectedItems[indxIds].TypeName;
                        i++;
            }
			
				var entityFormOptions = {};
                        entityFormOptions["entityName"] = "email";
			// Set default values for the Contact form
                        var formParameters = {};
						//set regarding object
                        formParameters["regardingobjectid"] = selectedItems[0].Id;
                        formParameters["regardingobjectidname"] = selectedItems[0].Name;
                        formParameters["regardingobjectidtype"] = selectedItems[0].TypeName;
						//set To field
                        formParameters["to"] = partylist;
                        // Open the form.
                        Xrm.Navigation.openForm(entityFormOptions, formParameters).then(
                            function (success) {
                                console.log(success);
                            },
                            function (error) {
                                console.log(error);
                            });
		}
}
function OpenQuickCreateemailsingleRecorde(PrimaryControl)
{
partylist[0] = new Object();
                partylist[0].id = PrimaryControl[0].Id;
                partylist[0].name = PrimaryControl[0].Name;
                partylist[0].entityType = PrimaryControl[0].TypeName;

                var entityFormOptions = {};
                entityFormOptions["entityName"] = "email";

                var formParameters = {};
                //Set EMail Regarding Field
				formParameters["regardingobjectid"] = PrimaryControl[0].Id;
                formParameters["regardingobjectidname"] = PrimaryControl[0].Name;
                formParameters["regardingobjectidtype"] = PrimaryControl[0].TypeName;
				//Set Email TO Field
                formParameters["to"] = partylist;
                // Open the form.
                Xrm.Navigation.openForm(entityFormOptions, formParameters).then(
                    function (success) {
                        console.log(success);
                    },
                    function (error) {
                        console.log(error);
                    });
}

Key Notes :-

  1. Code is fairly straigth forward, we need to pass selected record CRM parameter when calling javascript web resource from ribbon
  2. In code we are first checking if selected record count is one then populating that to Activity party TO field. If count is more than one record then looping through each record and then Populating to Activity Party To field
  3. Enable rule added to show button only if minimum one record selected

Hope this helps!

2 thoughts on “Dynamics CRM Prepopulate To Activity Party on Email when multiple/single records selected from grid

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s