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

111
Vistas
Replace double for loop but getting array instead of object

Beginner here...

So I wanted to make what I thought was a simple change, but it has proven to be more difficult. I have a .js script that runs on a timer as a function. The function queries data and brings back a list of objects that turn into an array of ids of records in the first loop.

In its original format -

    // get pledge objects
    const pledges = await getOppPledges()
    
    //Check if there are records
    if (pledges && pledges.length > 0) {
        
        //get array of pledge opportunity ids
        for (let pledge of pledges) {
            let oppIds = pledges.map(p => p.Opportunity__c)
            let sfOpps = await getOpp(oppIds)
            
            //run loop to create oppty doc
            for await (let opp of sfOpps) {...
            }
         ...}

My problem is that the double "for" loop generates double documents when there is more than one pledge. When I tried closing the first loop right before the second loop, "sfOpps" was unavailable for the second loop, and the code broke.

I had rearranged the code to something like,

   if (pledges && pledges.length > 0) {
        for (let pledge of pledges) {
            let oppIds = [pledge.Opportunity__c]
            let opp = await getOpp(oppIds)
            

But it seems like it returns "opp" as an array instead of an object. This does not work either because it comes back as undefined when I call for a value of "opp" like "opp.OpportunityContactRoles.totalSize".

Any input is appreciated!

about 4 years ago · Juan Pablo Isaza
1 Respuestas
Responde la pregunta

0

If I understood your problem correctly you shouldn't be needing the first loop as you map your ids on all your pledges inside the loop. It's the reason why you do the operations n times. (n being the length of the "pledges" array)

let oppIds = pledges.map(p => p.Opportunity__c) // You're already mapping all the ids at once

The correct way to do it would be

// get pledge objects
const pledges = await getOppPledges()

//Check if there are records
if (pledges && pledges.length > 0) {
    
    //get array of pledge opportunity ids
    let oppIds = pledges.map(p => p.Opportunity__c)
    let sfOpps = await getOpp(oppIds)
        
    //run loop to create oppty doc
    for await (let opp of sfOpps) {...
    }
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