Hi All,
Did some searching and didn't really find an answer to this. To start with, here's the code I'm executing as a JS webresource onLoad of the and onChange of the customerid field on the Order entity.
Xrm.WebApi.retrieveRecord("account", <shipToCustomerGUID>, "?$expand=myLookupField($select=accountid,name)").then(
function success(result) {
if (result != null) {
if (verboseLogs) alert("Web API Called Successfully");
var myLookup = result.myLookupField;
if (verboseLogs) alert("myLookup: " + myLookup);
var myLookupName = result.myLookupField.name;
if (verboseLogs) alert("myLookupName:" + myLookupName);
var myLookupId = result.myLookupField.accountid;
if (verboseLogs) alert("myLookupId: " + myLookupId);
if (verboseLogs) alert("Setting to myLookup");
var myLookupArray = new Array();
myLookupArray[0] = new Object();
myLookupArray[0].id = myLookupId;
myLookupArray[0].name = myLookupName;
myLookupArray[0].entityType = "account";Xrm.Page.getAttribute(
"customerid"
)
.setValue(myLookupArray);
}
else {
if (verboseLogs) alert("Clearing customerid");Xrm.Page.getAttribute(
"customerid"
)
.setValue(myLookupArray);.setValue(null) && alert("Please choose a valid account");
}
},
function (error) {
if (verboseLogs) alert(error.message);
});
Now, when I pass a GUID of an account record that has myLookupField populated, everything goes swimmingly. I can turn on verbose logs and watch the whole thing unfold line by line.
However, if myLookupField does not contain data, it still appears as if the API is successfully called, but I don't get any alerts after if (verboseLogs) alert("myLookup: " + myLookup). It's almost like the function dies after that point. None of my remaining variables alert (at all), it doesn't try to build the array, nothing. I even have the whole thing nested in a try/catch, and it doesn't trigger the catch. Can anyone offer any insight as to how this is behaving? And how I might accomplishthe following:
If myLookupField (on the ShipToCustomer account record) contains a value, build an array from that and push it into customerid
if myLookupField is blank, clear out customerid on the order and display an alert
*This post is locked for comments