+ Start a Discussion
abhik dey 1abhik dey 1 

External User Freeze or Lock Out Issue

Hi,
I have to freeze or lock out an external user based on a custom logic. Once the custom logic condition sets to true, I need to freeze or lock out that user so that the user wont be able to login until the admin unfreeze or unlock that external user.

I tried with isPasswordlockout  API field, but i got the error as 'Field is not re-writable' so I tried with freeze option but since when the user already logged in and the salesforce apex code or trigger runs in that user mode , i got the error "CANNOT SELF FREEZE" self frozen is not allowed.

My requirement is to lock or freeze that user when he performs some action and based on my custom logic , the user should be locked or freezed.
Also, I cant use the site.login method for 3 invalid attempts. So that option is gone apart from that anything is possible?

I tried with system.runas method but that works only in case of test method.

Any suggestions??
Daniel BallingerDaniel Ballinger
Cross posting the same answer from StackOverflow (http://stackoverflow.com/a/29587906/54026).

I assume you tried setting UserLogin (http://www.salesforce.com/us/developer/docs/api/Content/sforce_api_objects_userlogin.htm).IsFrozen when you got the "CANNOT SELF FREEZE" message.

You could try and find a way to switch the transaction user. Some options:

Use a future or batch method. This may encounter the same issue with the transaction user being the user you are trying to freeze.
Add the user record to a queue that will be frozen by a scheduled/batch job.
Use the API to perform the freeze as another user. See Freezing Users from Visualforce (http://bobbuzzard.blogspot.co.uk/2013/11/freezing-users-from-visualforce.html)
abhik dey 1abhik dey 1
Yeah I only put the question in both the forums to get quicker reply. To switch to a different transaction user I need to run that code via different which I am unable to do because runas method works only in test class. Secondly I tried using future method but the same issue I encounter no luck.. Secondly I cannot use schedule a job because based on my custom logic the user needs to be deactivate or lock out immediately . Since schedule job will run at a particular time hence not feasible for my requirement. Through ajax toolkit also when my page executes, I think I will encounter the same issue being as a transaction user. Thanks, Abhik