Skip to main content

Notifications

Customer experience | Sales, Customer Insights,...
Unanswered

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

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. 
  • Dengliang Li Profile Picture
    Dengliang Li Microsoft Employee on at
    Subgrid's Add Existing Button Lookup records Filtering - One to Many Relationship
    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

December Spotlight Star - Muhammad Affan

Congratulations to a top community star!

Top 10 leaders for November!

Congratulations to our November super stars!

Community AMA December 12th

Join us as we continue to demystify the Dynamics 365 Contact Center

Leaderboard

#1
André Arnaud de Calavon Profile Picture

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

#2
Martin Dráb Profile Picture

Martin Dráb 230,188 Most Valuable Professional

#3
nmaenpaa Profile Picture

nmaenpaa 101,156

Leaderboard

Featured topics

Product updates

Dynamics 365 release plans