5

Trying to delete groups and queues within Apex code when executing an uninstall script, Salesforce raises me this error:

DML operation DELETE not allowed on Group

Although this is pretty clear error, I am able to delete groups using the Developer Console, querying for groups and using the syntax delete groups (where groups is a list of the Group object).

Is it really impossible to delete groups using Apex? Should I use some fancy @future annotation to the method that deletes them? Or I really am unable to delete groups, and have to delete the GroupMember object instead?

  • 1
    I think you can use Apex to delete Groups - a test proves it, so it looks like it's possibly something to do with the uninstall script and the ghost user used to run it? This is in interesting post salesforce.stackexchange.com/questions/32607/… that might give you some ideas? – Phil Hawthorn Oct 14 '15 at 14:02
  • Did it work? Adding 'without sharing' dint work for me? any inputs would be appreciated. – Surya Chandra Rao Gandreddi Mar 8 '16 at 13:47
  • 1
    No. It didn't work either. In the end, we switched our method, and created a Visualforce page to handle the delete of the components that would need to be removed if the user wanted to uninstall the package. – Renato Oliveira Mar 8 '16 at 18:56
6
+25

The reason that this is happening is because within your UninstallHandler, the user in context is a temporary user that is used when performing managed package operations. This user type is LicenseManager. I'm assuming for security purposes, they do not have access to many dml operations on setup objects.

We tried a similar thing in our uninstall handler to remove our packaged permission sets from users. That also fails in the same way yours does.

I haven't tried it, but I assume that calls to @future will either throw an exception, or be discarded because the uninstall process needs to run synchronously right before the actual uninstall.

According to this post, adding the without sharing keyword may get this temporary user the appropriate permissions but obviously, without creating a package and testing DML on groups, it's hard to tell if it will work.

  • I'm going to try the 'without sharing' solution soon. If I succeed, I will update this question and mark it as answered. – Renato Oliveira Oct 15 '15 at 18:09
1

I tried making the uninstall handler class 'without sharing' but that does not seem to work either. Also, the uninstall fails when trying to uninstall the managed package. But if I go to the uninstall handler class and click on edit and cancel, I am able to uninstall the package, but the script doesn't delete the intended records.

So, no, adding 'without sharing' to the uninstall handler class does not make the records delete either. I believe deletion is just not allowed as a part of uninstall.

-4

You can run the following Apex in developer console.

Group g = [SELECT Id, Name, DeveloperName FROM Group 
           Where DeveloperName = 'Test_Group' LIMIT 1];
delete g;
  • 1
    I know I can run it via developer console, but I want to run it in a class, inside a managed package. When I tried to do it, SF raised the error I told in the question. – Renato Oliveira Oct 8 '15 at 11:09

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.