We are running a customer community and want to automate disabling customers from the community. By now I have learned that customer users can not do that themselves but I was wondering if there was a way to do this from apex. We could then run a batch every x hours to Disable Customer Accounts of customers that requested to be removed/unsubscribed.

Is this possible?

Edit: If yes, is there any system method that I can use to achieve this? I know that behind the screens Salesforce does more than just uncheck the active flag on the user.

For info: I am looking for this feature in apex: Disabling Customer Accounts


Did you note the following in the document you referenced?

Disabling the account permanently disables up to 100 external users associated with the account and removes them from all communities, groups, teams... ...Additionally, roles and groups associated with the account are permanently deleted and you won’t have the option to restore them from the Recycle Bin.

The second part of that is very different from just disabling a Community User. You mention "run a batch every x hours to Disable Customer Accounts". That's very different than merely disabling a Community User. Please be clear on which one of these (or both) it is that you want to do. Disabling an Account has a much stronger cascading effect. That's the essence of what the document you linked to was stating. Disabling a community user, has far less of a cascading effect. Yet, neither is ever deleted.

So, which is that you want to disable, the Account, the User or both? They can definitely be disabled using a queueable class that schedules itself to run again in "x" number of hours or a schedulable class, etc provided you have a means of marking the account or users that you want to disable from your community. All it would take is a checkbox field that you could query against or some other criteria that marks them for batch disable.


In response to your comments, you're asking a very broad question here that's really beyond the scope of what I can answer in this venue. Essentially, you'll want to run a batch or queueable class that queries for the flags you'd be setting then goes through the steps necessary to deactivate an account in your org when it finds them. It would also need to query for all the related Opportunities, Cases, Activities, Contacts, Community Users, etc. Your org may have specific settings on how it wants to handle each of those when an account is deactivated. You'll need to discover what those are as each org may handle those differently.

As you're aware, some records cannot be deleted, instead they can only be deactivated. Accounts, Contacts and Users fall into that category. Opportunities fall into a gray area that's sometimes a function of Product2 and also of Quotes and Orders. The same goes for Activities. Your batch class will need to process each of those types of records according to the criteria that applies to them.

Once you write your code, should you encounter issues with it, that would be the time to post for help with specific issues that you're encountering as each object has it's own issues to deal with.

  • Hi crmprogdev, In the doc I referenced they are talking about Disabling Customer Accounts by Clicking "Manage External Account", then click "Disable Customer Account" on the Salesforce Account Record. As described in the doc it looks like a manual action that would require an admin or help desk employee to do it. What I am looking to do is to automate this in a way so help desk or admin don't have to spend time on that. We have a means to mark accounts/users that need to be disabled but I don't know how to do the disabling in apex eg: what fields to update / what (system?) methods to run...
    – Nielsm
    Apr 12 '16 at 6:47

If your only question is "Is this possible?" the answer is yes. All you need to do is flip the IsActive field to False on the User record using batch apex.

You'll get the user license back, and they won't be able to log in any more until you Enable Customer User on their contact record.

I think you can even go as far as allowing them to disable themselves. I would grant my users the ability to edit their own Contact record using sharing sets, then make an Apex Trigger that runs whenever the Boolean custom field Disable_Me__c on Contact changes from false to true. That trigger will look up the User record and set IsActive to false.

  • I modified my question to be more clear as to what I'm looking for.
    – Nielsm
    Apr 12 '16 at 13:40
  • I don't believe Salesforce does anything more than just flip the IsActive checkbox. I've had a system that does exactly this running for about 4 months now, and I'm able to re-activate users without trouble. Apr 12 '16 at 13:44
  • Salesforce also changes the username (appends an underscore) and idk what else.
    – Nielsm
    Apr 12 '16 at 13:53

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.