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.