5

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???

Thanks

Sathish

3

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.

6
  • That would probably only work if the workflow user was not the user who triggered the workflow event... because even in system code, you can't deactivate yourself.
    – sfdcfox
    Oct 8 '13 at 20:11
  • 1
    Just because you're running in system code doesn't mean that "it's no longer tied to the user making the request." It only means that sharing may not be enabled; you're actually assuming that the @future method is also stored in a class "without sharing".
    – sfdcfox
    Oct 8 '13 at 20:13
  • Good point @fox. I hadn't actually tried this (that's probably clear). With your modifications, do you think it would work, or should I delete this answer?
    – Benj
    Oct 8 '13 at 20:14
  • 1
    You were close, though; your idea gave me one of my own that may work. Have the Deactivate_Me__c field, like here, but instead have a scheduled class that "sweeps" users every once in a while and deactivates them. Have the sweeper scheduled by a system administrator.
    – sfdcfox
    Oct 8 '13 at 20:18
  • Do you want to add that as an answer, or should I update mine?
    – Benj
    Oct 8 '13 at 20:29

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.