Skip to main content

Notifications

Announcements

No record found.

Customer experience | Sales, Customer Insights,...
Answered

Javascript to prepopulate To field in Email

Posted on by 240

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. 

  • NK Bhairi Profile Picture
    NK Bhairi 240 on at
    RE: Javascript to prepopulate To field in Email

    Thanks

  • NK Bhairi Profile Picture
    NK Bhairi 240 on at
    RE: Javascript to prepopulate To field in Email

    Thanks Bipin. Its working.

    Just that you forgot to initialize I variable.

  • Verified answer
    Bipin D365 Profile Picture
    Bipin D365 28,964 Super User 2024 Season 1 on at
    RE: Javascript to prepopulate To field in Email

    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.

  • cloflyMao Profile Picture
    cloflyMao 25,198 on at
    RE: Javascript to prepopulate To field in Email

    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

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

December Spotlight Star - Muhammad Affan

Congratulations to a top community star!

Top 10 leaders for November!

Congratulations to our November super stars!

Tips for Writing Effective Suggested Answers

Best practices for providing successful forum answers ✍️

Leaderboard

#1
André Arnaud de Calavon Profile Picture

André Arnaud de Cal... 291,269 Super User 2024 Season 2

#2
Martin Dráb Profile Picture

Martin Dráb 230,198 Most Valuable Professional

#3
nmaenpaa Profile Picture

nmaenpaa 101,156

Leaderboard

Featured topics

Product updates

Dynamics 365 release plans