I have a lead owner assignment logic with many conditions.
When user presses a button "Get lead" a lead in the org is searched and ordered by the field rank (Soql
order by rank__c),
and when the lead is fetched its owner is assigned to the user.
We had a situation when multiple users clicked the button at the same time and got the same lead - so in case user1 and user2 click their buttons the same time - user 1 was assigned to leadA, and right afterwards -user 2 was assigned to leadA as well. So user 1 saw a message that he got the lead but actually it was overridden. Since we cant use FOR UPDATE and Order by in the same soql,
we changed the logic to 2 soql. first one the original soql -
select id from lead where... order by rank__c desc limit 10
then we had another query - select id from lead where...(all conditions from above) and id in: first list FOR UPDATE limit X.
then we search by the 1st list the lead in 2nd list - to get the one with the highest rank
My question is - how much should I limit the 2nd query (and the 1st query)? What could happen if I have 11 users clicking the same button the same time?
Any help will be appreciated. Thank you!!