web
You’re offline. This is a read only version of the page.
close
Skip to main content

Announcements

News and Announcements icon
Community site session details

Community site session details

Session Id :
Customer experience | Sales, Customer Insights,...
Answered

Javascript to prepopulate To field in Email

(0) ShareShare
ReportReport
Posted on by 244

Hi,

I have an Account entity with Contacts Grid, I placed Custom Button, I selected Contact records from Grid and on Custom button click,

I need to Open an Email with the selected contacts in To field.

I am able to retrieve the Guids of the contact , but I am not not able to iterate loop and send the selected contacts as parameters to the Email record using openEntityForm. 

I have the same question (0)
  • cloflyMao Profile Picture
    25,210 on at

    Hi Miss Dynamics,

    To field is party list type field(also for Regarding field), such type of field is not able to be populated with parameters from openEntityForm function.

    The workaround is that you could create additional input parameters to email form properties, run another javascript function at email form OnLoad event to query parameters passed from openEntityForm function,

    then use these parameters to populate To field, as what we do when populating a lookup field.

    formContext.getAttribute("to").setValue([{id:'',name:'',entityType:'contact'}]);
    
    

    Please refer to following thread for how to add custom input parameters to form:

    https://community.dynamics.com/crm/f/microsoft-dynamics-crm-forum/145341/how-to-pass-values-between-form-using-javascript

    In addition, as Xrm.Page.context.getQueryStringParameters and openEntityForm will be deprecated, please replace them with formContext.data.attributes and Xrm.Navigation.openForm

    https://docs.microsoft.com/en-us/power-platform/important-changes-coming#some-client-apis-are-deprecated (getQueryStringParameters)

    https://docs.microsoft.com/en-us/powerapps/developer/model-driven-apps/clientapi/reference/xrm-utility#deprecated-methods (openEntityForm)

    Regards,

    Clofly

  • Verified answer
    Bipin D365 Profile Picture
    28,985 Moderator on at

    Hi,

    Try below code for your requirement.

    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);
                        });
    }
    					

    You can comment the code for regarding field if you don't need.

    If found helpful, Please mark my answer verified.

  • NK Bhairi Profile Picture
    244 on at

    Thanks Bipin. Its working.

    Just that you forgot to initialize I variable.

  • NK Bhairi Profile Picture
    244 on at

    Thanks

Under review

Thank you for your reply! To ensure a great experience for everyone, your content is awaiting approval by our Community Managers. Please check back later.

Helpful resources

Quick Links

Season of Sharing Community Challenge Launch!

Jump in, show your community spirit, and win prizes!

Women in Power Builds Momentum

Expanding mentorship, skilling, and AI innovation

Congratulations to the May Top 10 Community Leaders

These are the community rock stars!

Leaderboard > Customer experience | Sales, Customer Insights, CRM

#1
Hamza H Profile Picture

Hamza H 142 Super User 2026 Season 1

#2
Nagaraju_Matta Profile Picture

Nagaraju_Matta 128

#3
11manish Profile Picture

11manish 119

Last 30 days Overall leaderboard

Product updates

Dynamics 365 release plans