function readOnly(count){ }
Starting November 20, the site will be set to read-only. On December 4, 2023,
forum discussions will move to the Trailblazer Community.
+ Start a Discussion
eyewelleyewell 

Issues with Asynchronous queries in Flex - complete

I am a bit of a newbie with asynchronous calls, and I am having a problem with my code.
 
Background:
I have a for loop within a for loop.
For each account that is returned in the first loop, I query its opportunities, and want to set a field on those child opps.
 
Problem:
The problem is that the code seems to only do the update on opps for the last account in the set of accounts. It seems as though each async call is aborting the one before it.
 
Can someone suggest a fix (don't use asynch calls for this purpose?, or am I doing it wrong?).
 
pseudocode is below:
 
for each ID in accountArray[]
{
    apex.query("select all opps where acct ID = 'ID' ",

          new AsyncResponder( function saveChildOpptyQueryResult(result : Object):void
         
{

             for each oppty in result
             {

                 
set field in oppty updOpp
                 
apex.update([updOpp], new AsyncResponder(updResult,genericFault ));

             }  //end oppty for loop
         }  // end saveChildOpptyQueryResult()

        ,genericFault() //this is the fault handling function of the asynch call
} //end account for loop

SteveBowerSteveBower

Without looking into into your async issue at all, you might want to consider that your method will require nA*nO  update calls to salesforce.  You might be better off, performance wise, to batch your updates in a local array and then submit them 200 at a time.  (Or build the array until it reaches 200, submit it asychronously, and then build a new array.)

You're also issuing a query for each account ID to retrieve it's related opportunities.  You could do this at the same time that you get the accounts.So, perhaps you could issue a single 

Select a.Name, a.Id, (Select Id, Name, TotalOpportunityQuantity From Opportunities) From Account a

sort of query and get all your data in one shot, looping through it with queryMore or the next iterator, etc.

Best, Steve.

eyewelleyewell
Steve,
 
Thanks for the fast feedback, and good points.
 
What I want to do is
  • query a list of accounts
  • display them to the user
  • capture user changes to fields
  • update the accounts with these fields
  • modify/enhance the related opportunity information with the data provided for the accounts
 
Can you make any suggestions on how to best architect that back and forth?
 
The best I can think of is the following:
  • query the accounts, sort by accountID
  • dislpay to user and grab user input
  • update the accounts with user input
  • query the related opps into oppsArray, sort by parentaccountID
  • update the changes to the oppsArray
  • call apex.update on the oppsArray
Thanks!
 
Erik
 
SteveBowerSteveBower
What about:

query the accounts *and* the opportunity objects associated with those account at the same time.
Loop through queryMore until you have them all so you can display all the accounts.  (I'd sort them by something more useful that Account Id, perhaps name, time, etc.)

Display them, have the code that fires when the user makes a change:
1. See if that Account is already on your new accArray and If not, create a new account object on your accArray and set the ID of the new object to the one you're processing.  If its already there, just re-use it.
2. Set the fields you wish to change.

Do the same logic for the Opportunity records associated with that Account.

When all changes are done, then issue update on the AccArray and the Opps array.

Steve.

P.S. If the Salesforce accounts you're writing for have Apex, I think you could also just update the Accounts, and have the Opportunities updated by a post-change trigger.