Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

112
Visualizações
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 Respostas
Responde à pergunta

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 Relatório
Responde à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda