0

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!

1
  • for await... of is meant to be used for async iterable objects. Is sfOpps an array of Promises? Commented Jan 24, 2022 at 17:06

1 Answer 1

1

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) {...
    }
Sign up to request clarification or add additional context in comments.

3 Comments

I thought I tried that, but I will do it again tonight and try it. Keep you posted.
It will not work because the pledges array has a value that needs to be available in the second loop. What I think I am going to do is change the line let oppIds = pledges.map(p => p.Opportunity__c) to let oppIds = pledge.map(p => p.Opportunity__c). What do you think?
I'm not sure to understand what you mean. If you need other pledge related info inside the last loop try to create the document one by one maybe without using the map method. I'll need more info to be able to help further. The last bit of code you provided inside your original answer seems about right, you just need to figure out the solutions to your other problems.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.