DeActivate Portal User by himself using apex class

Need Help on “how to deactivate the Customer portal user using apex“.

Currently we trying to deactivate the portal user with portal user sessionId. for this we are getting Exception as follows

@ EXCEPTION_THROWN|[183]|System.DmlException: Update failed. First
exception on row 0 with id 00511000000QnNCAA0; first error:
FIELD_INTEGRITY_EXCEPTION, Cannot de-activate self: Active: [IsActive]

i am using the code below for deactivate the portal user

         User u = [select Id, IsActive, IsPortalEnabled from User where Id =: temp];

         u.IsActive = false;

         IsPortalEnabled = false;

         update u;

with the same user session Id. how to do this???




This is a bit convoluted, but could work. The users won’t be deactivated immediately, but they will be deactivated (eventually) in response to their own action. (Thanks to @sfdcfox for helping design this approach.)

  1. Put a custom field on User called something like Deactivate_Me__c
  2. Write a Schedulable class that checks for any portal user accounts with the Deactivate_Me__c flag set, and deactivates them.
  3. Schedule the apex job to run on a schedule that makes sense for you, making sure to schedule the job as an admin user.
  4. If you need something to happen immediately, it may be possible to write a trigger that would change the user’s profile to a more restrictive profile (but I haven’t checked that). The second profile could have no record access and all tabs hidden except one that says “Your account is pending deactivation.”

Of course, the deeper question is what you’re trying to accomplish with this workflow. Why does a user want to deactivate their own account? If they do it by accident, how do they fix their error? Etc.

Source : Link , Question Author : sathishkumar , Answer Author : Benj

Leave a Comment