1

Hopefully someone can help me correct the below code. I am not able to get anything in the final return statement. I know it looks messy as I have tried a few alternatives.

async function concatnames(allSelectedRows){ var flightName = []; var name = ""; var resultOutCome = ""; try { var result = await allSelectedRows.forEach( function (selectedRow, i){ recordidGuid = selectedRow.getData().getEntity().getEntityReference().id.replace('{', '').replace('}', '') //Flight Plan Event Guid name = Xrm.WebApi.online.retrieveRecord("crd80_flightplanevent", recordidGuid, "?$select=_crd80_learnerflight_value") }) resultOutCome = name["_crd80_learnerflight_value@OData.Community.Display.V1.FormattedValue"] console.log(resultOutCome) flightName.push(resultOutCome); } catch (error) { DisplayError(error) } return flightName } 

2 Answers 2

3

await only works with promises, allSelectedRows.forEach(...) it's not a promise, it's a function.

You want to do something like this:

await Promise.all(allSelectedRows.forEach( async function (selectedRow, i) { recordidGuid = await selectedRow.getData().getEntity().getEntityReference().id.replace('{', '').replace('}', '') //Flight Plan Event Guid name = await Xrm.WebApi.online.retrieveRecord("crd80_flightplanevent", recordidGuid, "?$select=_crd80_learnerflight_value") })) 

The Promise.all() method takes an iterable of promises as an input, and returns a single Promise that resolves to an array of the results of the input promises. This returned promise will resolve when all of the input's promises have resolved, or if the input iterable contains no promises.

And the new allSelectedRows.forEach(...) with now async functions, returns promises, because when you do async function NameOfFunction, you are actually creating a promise, that Promise.all() will return all them resolved.

And I am using an await in Xrm.WebApi.online.retrieveRecord("crd80_flightplanevent", recordidGuid, "?$select=_crd80_learnerflight_value") because I think it is a promise, so when I add await, it will return me the resolved value of the promise.

Sign up to request clarification or add additional context in comments.

5 Comments

Thank you for your reply, however when I use this I get. "undefined is not iterable (cannot read property Symbol(Symbol.iterator))"
Sorry, the function needs to return something, you and I forgot that. After the declaration of name variable, add the variable you want to return. Example: return name
I have added return name after the name = await ... but still get the same error, or did you mean return it somewhere else?
Sorry, I didn't notice that you need recordidGuid to get the var name, let me modify the answer.
I modified the answer, sorry, I didn't read correctly your code the first time, now I have a better idea of how it works. Tell me if that works, if it not works, I'll may know why.
2

Depending on what you to accomplish, there's a couple of ways to do this:

Process Each Request Sequentially

This is (a) slower than the alternative, and (b) more code. However, it gives you granular control of the error(s) encountered:

// process each request sequentially async function concatnames( allSelectedRows ) { const flightName = []; let name = ""; let resultOutCome = ""; try { for ( const selectedRow of allSelectedRows ) { recordidGuid = selectedRow .getData() .getEntity() .getEntityReference() .id .replace('{', '') .replace('}', ''); name = await Xrm .WebApi .online .retrieveRecord( "crd80_flightplanevent", recordidGuid, "?$select=_crd80_learnerflight_value" ); resultOutCome = name["_crd80_learnerflight_value@OData.Community.Display.V1.FormattedValue"]; flightName.push(resultOutCome); } } catch (error) { DisplayError(error) } return flightName } 

Run the requests in parallel This is nice and clean. Also faster than the above because all request are run in parallel:

async function concatnames(allSelectedRows) { const flightName = await Promise.all( allSelectedRows.map( row => { recordidGuid = selectedRow .getData() .getEntity() .getEntityReference() .id .replace('{', '') .replace('}', ''); return Xrm.WebApi.online .retrieveRecord( "crd80_flightplanevent", recordidGuid, "?$select=_crd80_learnerflight_value" ) .then( name => name["_crd80_learnerflight_value@OData.Community.Display.V1.FormattedValue"]); })) .catch( DisplayError ); return flightName || []; } 

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.