Here is the situation. I have a @Future method which has callouts is running along side of actual main CLASS. in the callout response, I am receiving some error/success XML, I am saving them in a STRING message. However, I can not show error from the String RETURN as @future will not send anything like that.
So, I am saving that String in another custom Field and updating at the end of FUTURE method.
Now, I want to show that field message in Lightning. Can anyone tell me how can I get it done?
Once we are proceeding, Main class executing and exiting then future method getting executed within a few millisec. I want to wait for 5 seconds after we run it in lightning so that I can use the updated field in @future method to show error.
It's a bit complicated it seems but I am not getting any clue. Please help.
Thank you,
Ashok Kumar.
Best Answer
To answer your actual question, here is how you can call a method after a wait period:
ControllerJS
But, I don't think it is an elegant approach, just because you are assuming that the future method will complete in 5 seconds. Why keep the user waiting if it returns in 1 second? And what if it takes more than 5 seconds... you won't show a message at all!
Because you mentioned that a field will be updated with the error message after the callout returns, why not regularly check for an update to that field like so:
ControllerJS
HelperJS
Notice that
window.setTimeout
executes once after a set delay. On the other hand,window.setInterval
executes over and over after a set time period (so make sure it stops after your callout returns).And also, did you know that you can detect changes to a specific value and fire a method when that happens? Like so: (just food for thought... you might end up not needing it at all)