You need to sign in to do that
Don't have an account?
eyewell
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
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.
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.