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!
for await... ofis meant to be used for async iterable objects. IssfOppsan array of Promises?