Answered by:
$filter with lookup ids; SDK.REST call failing

Question
-
I've got a few situations where my select string for a retrieveMultipleRecords call contains filters that include lookups (entity references). An example is below which contains 3 lookups in one filter. For example, I have the GUID for a Price Level, and I want in my filter for the field PriceLevelId to equal that GUID. But you cannot set PriceLevelId = theGUID because PriceLevelId is an entity reference (or object). But you can theoretically get that object's Id with PriceLevelId.Id. But neither works. What am I missing? Is there a way to accomplish the goal of this code below? (For clarity, the error is in the selectString declaration).
function getPrice2() { //currently not working
var pricelevelid = Xrm.Page.getAttribute("pricelistid").getValue()[0].id;
var uomid = Xrm.Page.getAttribute("unitid").getValue()[0].id;
var productid = Xrm.Page.getAttribute("productid").getValue()[0].id;
var priceInfo = null;
if ((pricelevelid != null) && (uomid != null) && (productid != null)) {
var selectString = "$select=Amount,ProductPriceLevelId&$filter=(PriceLevelId.Id eq guid'" + pricelevelid + "') and (UoMId eq guid.Id'" + uomid + "') and (ProductId.Id eq guid'" + productid + "')";
SDK.REST.retrieveMultipleRecords
(
"ProductPriceLevel",
selectString,
function (results) {
if (results != null && results[0] != null) {
priceInfo = results[0];
}
},
function (error) {
alert("getPrice Error: " + error.message);
},
function () { },
false
);
}
if (priceInfo) {
return priceInfo.Amount;
}
else {
return 0;
}
}
Mark Rockwell
Tuesday, April 15, 2014 4:09 PM
Answers
-
I mentioned in my original post that I tried that. ("But you cannot set PriceLevelId = theGUID because PriceLevelId is an entity reference (or object).")
It turns out there's some goofy syntax you have to use when working with entity references. Instead of this:
(PriceLevelId.Id eq guid'" + pricelevelid + "')
You have to do this:
(PriceLevelId/Id eq guid'" + pricelevelid + "')
Go figure. Problem solved. (Why anyone would ever guess that answer is beyond me. Thank you David Yack.)
Mark Rockwell
- Marked as answer by Mark Rockwell Wednesday, April 16, 2014 5:10 AM
Wednesday, April 16, 2014 5:09 AM
All replies
-
What if you used something like this:
$select=Amount&$filter=ProductPriceLevelId eq guid'133bfa5a-34b2-e311-9429-6c3be5a8fdb8'
Jason Lattimer
My Blog - Follow me on Twitter - LinkedIn- Proposed as answer by JLattimerMVP, Moderator Tuesday, April 15, 2014 6:15 PM
Tuesday, April 15, 2014 6:15 PMModerator -
I mentioned in my original post that I tried that. ("But you cannot set PriceLevelId = theGUID because PriceLevelId is an entity reference (or object).")
It turns out there's some goofy syntax you have to use when working with entity references. Instead of this:
(PriceLevelId.Id eq guid'" + pricelevelid + "')
You have to do this:
(PriceLevelId/Id eq guid'" + pricelevelid + "')
Go figure. Problem solved. (Why anyone would ever guess that answer is beyond me. Thank you David Yack.)
Mark Rockwell
- Marked as answer by Mark Rockwell Wednesday, April 16, 2014 5:10 AM
Wednesday, April 16, 2014 5:09 AM