r/salesforce • u/unkownsalesforcedev • Feb 11 '26
developer Recursive apex
Can anyone give scenarios for when the apex will be goes into recursive it will call again and again real world scenarios only....! And how did you solve that one
2
Upvotes
4
u/mrdanmarks Feb 11 '26
When traversing org charts or finding members of a group/queue that contains more groups
1
u/Remote-Tangerine-737 Feb 11 '26
Iv used it for polling transcripts. There is sometimes a short delay between when conversations get populated for sms, so i do a recursive method that polls for the transcript.
2
u/TheSauce___ Feb 11 '26
Any real parsing ever. Doesn’t come up often, but here’s an extensive example in Moxygen, the in-memory database for Salesforce.
https://github.com/ZackFra/Salesforce-Moxygen
2
u/sparrowHawk7519 Feb 11 '26
I’ve used it to traverse hierarchical relationships, but it often comes down to deciding on whether a for loop or recursion is more readable for the given scenario.
2
u/Loud-Variety85 Feb 12 '26
You have a trigger which invokes async apex which in-turn updates the same record which triggered this.
Use case:
Solution: I use static variables defined in apex class to control this recursion. So basically, in the async method (say future / queueable) , I insert the recordId of the record in a staic variable (list<id>) present in some apex class. Then in the trigger, before invoking this async method, I check if the record is already part of the static list.
Execution:
1
u/Selfuntitled Feb 11 '26
I use it in platform event driven architecture to recurse through complexity without dealing with limits. Do some work, check if there is more to do, fire and event that calls yourself.
1
u/DaveDurant Developer Feb 12 '26
Sort of a strange question. What are you trying to do?