Skip to main content

Notifications

Announcements

No record found.

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

Why Isn't my Lookup Populating Data using Javascript?

Posted on by 5
Hey All,
 
I am having an issue with populating a lookup field in my quick view form. I created a custom table for users to subscribe to notifications from Project records in Project Operations. I want users to click a button on the Command Bar where a Quick Create Form will have the Project associated to the form. I created a lookup that points to the project table and developed a plugin to handle this logic. I can confirm that the Plugin is grabbing the appropriate values. I also have a fallback option where if a record already exists with notification settings on the Project for the User creating the record, it will open up their existing record. All of this works. The only part that does not work is when No record exists, the Quick Creat Form should open a new form to register with that pre populated field. It is not pulling over. Does anyone have any insight on this? I am sharing the plugin code below:
 
function openNotificationSettingsForm(primaryControl) {
    var userId = Xrm.Utility.getGlobalContext().userSettings.userId.replace(/{/, //).replace(/}/, //);
    var projectId = primaryControl.data.entity.getId().replace(/{/, //).replace(/}/, //);
    var entityName = /inotiv_notificationsettings/;
    var projectName = primaryControl.data.entity.getPrimaryAttributeValue();
    var filter = `_inotiv_project_value eq '${projectId}' and _ownerid_value eq '${userId}'`;
 
    // Check if an existing notification settings record is present for the current user and project
    Xrm.WebApi.retrieveMultipleRecords(entityName, `?$filter=${filter}`).then(
        function success(result) {
            if (result.entities.length > 0) {
                // If a record exists, open the existing record
                var existingRecordId = result.entities[0].inotiv_notificationsettingsid;
                Xrm.Navigation.openForm({
                    entityName: entityName,
                    entityId: existingRecordId
                });
            } else {
                // If no record exists, open a new Quick Create form
                var parameters = {};
                    parameters[/inotiv_project/] = [{ id: projectId, name: projectName, entityType: /msdyn_project/ }]; 
                    console.log(/Parameters for Quick Create Form:/, JSON.stringify(parameters, null, 2));

                Xrm.Navigation.openForm({
                    entityName: entityName,
                    useQuickCreateForm: true,
                    parameters: parameters
                }).then(
                    function success(result) {
                        console.log(/Quick Create Form opened successfully./);
                    },
                    function error() {
                        console.log(/Error opening Quick Create Form./);
                    }
                );
            }
        },
        function error(error) {
            console.log(/Error querying existing notification settings records: /, error.message);
        }
    );
}
 
 
Any help would be much appreciated. Thank you!
  • danielspillers Profile Picture
    danielspillers 5 on at
    Why Isn't my Lookup Populating Data using Javascript?
    Ray, thank you so much. This example helped me to populate the field properly. You're a lifesaver!
  • Verified answer
    Ray Profile Picture
    Ray 1,505 on at
    Why Isn't my Lookup Populating Data using Javascript?
    The parameter format is not correct. It should be 3 properties in the root object instead of an array property. You can refer below example:

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,280 Super User 2024 Season 2

#2
Martin Dráb Profile Picture

Martin Dráb 230,235 Most Valuable Professional

#3
nmaenpaa Profile Picture

nmaenpaa 101,156

Leaderboard

Featured topics

Product updates

Dynamics 365 release plans