I used RetrieveMultiple to retrieve records of entity B and in the successCallBack function I want to cycle results to add them to a variable which represents the list of these records, but I'm not sure that it's a correct approach and If I should better use more specific classes or methods to reach my goal and keep track of records.
General scenario: I have my Javascript function in the form of entity E, which is associated with Entity B.
I need to retrieve all records of Entity A associated with the records of B which are linked to this E.
So here I'm retrieving the B records which are linked to this E, and storing them into a variable which will be used in the retrieveMultiple of records A.
Here is my code:
var recordsB; var formContext = executionContext.getFormContext(); var targetId = formContext.data.entity.getId(); var recordsA; // retrieve records B associated with this record E: Xrm.WebApi.retrieveMultipleRecords("entityB", "?$select=recordB_Id, recordA_id,&$filter=entityE_Id eq" targetId).then( function success(result) { for (var i = 0; i < result.entities.length; i ) { // recordsB.push(result.entities[i]); // } }, function (error) { window.alert(error.message); } );
Hi, Joel
you are doing well, here are some thoughts
1.- Initialize your arrays
var recordsB = []; var recordsA = [];
2.-
a.- As you are filtering for a LookUp you must use _lookupname_value format and let a gap between eq and you string concatenation.
b.- If you need for the GUID of the record its by default entitynameid (there are some exceptions called activities)
So your filter should be like assuming you are in entity entityB "?$select=entityBid,_recordAid_value&$filter=_entityE_Id_value eq " targetId
3.- Cosider that your are working with promises when you are using Xrm.WebApi.retrieveMultipleRecords so success callback will rise at some point in your application.
4.- Your iteration through result it's good, you are getting all the object but you could just get what you really need and also what you queried
entityBid and _recordAid_value
function success(result) {
var n = result.entities.length;
for (var i = 0; i < n; i ) {
var entity = result.entities[i];
console.log(entity);
recordsB.push(entity["entityBid"]);
if(entity["_recordAid_value"]!==null)
recordsA.push(entity["_recordAid_value"]);
}
}
regards.
please consider marking as an answer if it was helpful
Stay up to date on forum activity by subscribing. You can also customize your in-app and email Notification settings across all subscriptions.
André Arnaud de Cal... 290,900 Super User 2024 Season 2
Martin Dráb 229,275 Most Valuable Professional
nmaenpaa 101,156