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

Announcements

No record found.

News and Announcements icon
Community site session details

Community site session details

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

Subgrid's Add Existing Button Lookup records Filtering - One to Many Relationship

(0) ShareShare
ReportReport
Posted on by
In my case there is Account and Contact, the contact subgrid on the accounts form. One to many relationship, and on click of add existing button of the subgrid, its lookup records should only shows the contacts of type vendor (121540000), basically add existing lookup records filtering. So I used this code below:

function SubgridLookup(selectedEntityTypeName, selectedControl, firstPrimaryItemId,primarycontrol) {
    debugger;
       var formContext = primarycontrol;
          if (selectedControl.getRelationship().name === /cm_contact_Account_account/ ) { 
              var options = {
                  allowMultiSelect: true,
                  defaultEntityType: /contact/,
                  entityTypes: [/contact/],
                  
                  disableMru: true,
                  filters: [{
                      entityLogicalName: /contact/,
                  //filterXml: /<filter><condition attribute='imd_project' operator='eq' value='/+Xrm.Page.getAttribute('imd_project').getValue()[0].id+/'/></filter>/
                  filterXml:/<filter type='and'>/+
                  /<condition attribute='cm_relatedto' operator='eq' value='121540000' />/+
                /</filter>/
                  }]
              };
              lookupAddExistingRecords(/cm_contact_Account_account/, /account/, /contact/, firstPrimaryItemId, selectedControl, options,primarycontrol);
          }   
          else {
              XrmCore.Commands.AddFromSubGrid.addExistingFromSubGridAssociated(selectedEntityTypeName, selectedControl);
          }
      
     
  }
  function lookupAddExistingRecords(relationshipName, primaryEntity, relatedEntity, parentRecordId, gridControl, lookupOptions,primarycontrol) {
      //debugger;
      Xrm.Utility.lookupObjects(lookupOptions).then(function (results) {
          Xrm.Utility.getEntityMetadata(primaryEntity).then(function (primaryEntityData) {
              var primaryEntitySetName = primaryEntityData.EntitySetName;
              Xrm.Utility.getEntityMetadata(relatedEntity).then(function (relatedEntityData) {
                  var relatedEntitySetName = relatedEntityData.EntitySetName;
                  associateAddExistingResults(relationshipName, primarycontrol, primaryEntitySetName, relatedEntitySetName, relatedEntity, parentRecordId.replace(/{/, //).replace(/}/, //), gridControl, results, 0)
              });
          });
      });
  }
  function associateAddExistingResults(relationshipName, primarycontrol, primaryEntitySetName, relatedEntitySetName, relatedEntity, parentRecordId, gridControl, results, index) {
      //debugger;
      var formContext = primarycontrol;
      if (index >= results.length) {
          if (gridControl) { gridControl.refresh(); }
          return;
      }
      var lookupId = results[index].id.replace(/{/, //).replace(/}/, //);
      var lookupEntity = results[index].entityType || results[index].typename;
      var primaryId = parentRecordId;
      var relatedId = lookupId;
      if (lookupEntity.toLowerCase() !== relatedEntity.toLowerCase()) {
          primaryId = lookupId;
          relatedId = parentRecordId;
      }
      var association = { '@odata.id': Xrm.Page.context.getClientUrl() + /api/data/v9.0// + relatedEntitySetName + /(/ + relatedId + /)/ };
      var req = new XMLHttpRequest();
      req.open(/POST/, Xrm.Page.context.getClientUrl() + /api/data/v9.0// + primaryEntitySetName + /(/ + primaryId + /)// + relationshipName + //$ref/, true);
      req.setRequestHeader(/Accept/, /application/json/);
      req.setRequestHeader(/Content-Type/, /application/json; charset=utf-8/);
      req.setRequestHeader(/OData-MaxVersion/, /4.0/);
      req.setRequestHeader(/OData-Version/, /4.0/);
      req.onreadystatechange = function () {
          if (this.readyState === 4) {
              req.onreadystatechange = null;
              index++;
              if (this.status === 204 || this.status === 1223) {
                  associateAddExistingResults(relationshipName, primarycontrol, primaryEntitySetName, relatedEntitySetName, relatedEntity, parentRecordId, gridControl, results, index);
              }
              else {
                  var error = JSON.parse(this.response).error.message;
                  if (error === /A record with matching key values already exists./) {
                      associateAddExistingResults(relationshipName, primarycontrol, primaryEntitySetName, relatedEntitySetName, relatedEntity, parentRecordId, gridControl, results, index);
                  }
                  else {
                      Xrm.Navigation.openAlertDialog(error);
                      if (gridControl) { gridControl.refresh(); }
                  }
              }
          }
      };
      req.send(JSON.stringify(association));
  }


But using this code the lookup records shows correct filtering records, but on select that record and on Adding that record on subgrid, its give error popup message: Bad Request - Error in query syntax. And the record not added.




Please correct me, what or where is the issue. 
I have the same question (0)
  • Dengliang Li Profile Picture
    Microsoft Employee on at
    Hi,
     
    Why do you have two Filter xml here?
     
    Best Regards,
    Dengliang Li
     

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

Introducing the 2026 Season 1 community Super Users

Congratulations to our 2026 Super Stars!

Meet the Microsoft Dynamics 365 Contact Center Champions

We are thrilled to have these Champions in our Community!

Congratulations to the March Top 10 Community Leaders

These are the community rock stars!

Leaderboard > Customer experience | Sales, Customer Insights, CRM

#1
11manish Profile Picture

11manish 176

#2
ManoVerse Profile Picture

ManoVerse 158 Super User 2026 Season 1

#3
Jimmy Passeti Profile Picture

Jimmy Passeti 57 Most Valuable Professional

Last 30 days Overall leaderboard

Product updates

Dynamics 365 release plans