Empresas
Empleos
  • Sobre nosotros
  • Soluciones
    • Publicación de vacantes
      Publica tu vacante y recibe candidatos calificados en 48h.
    • Evaluación de candidatos
      500+ pruebas técnicas y psicológicas, más anti-fraude.
    • Headhunting
      Búsqueda ejecutiva a la medida de principio a fin.
    • Nómina + EOR
      Dispersión de nómina y EOR en más de 15 países de LATAM.
  • Precios
  • Empleos

0

163
Vistas
Promise or Async

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
}
about 4 years ago · Juan Pablo Isaza
2 Respuestas
Responde la pregunta

0

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.

about 4 years ago · Juan Pablo Isaza Denunciar

0

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 || [];
}
about 4 years ago · Juan Pablo Isaza Denunciar
Responde la pregunta
Encuentra empleos remotos

¡Descubre la nueva forma de encontrar empleo!

Top de empleos
Top categorías de empleo
Empresas
Publicar vacante Precios Comercial
Legal
Términos y condiciones Política de privacidad
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomiéndame algunas ofertas
Necesito ayuda