0



Anyone have experience in creating a CollaborationGroup using SandboxPostCopy class ? My requirement is to create a Public Group in newly refreshed sandbox.

I have tried following ways but no success:

Owner = (no value assigned)i.e., Automated Process User (autoproc)
CollaborationType = Public
Error = System.DmlException: Insert failed. First exception on row 0; first error: INVALID_CROSS_REFERENCE_KEY, This user can’t be added to the group.: []

Owner = <active_admin_user>
CollaborationType = Public
Error = System.DmlException: Insert failed. First exception on row 0; first error: INVALID_CROSS_REFERENCE_KEY, This user doesn’t have permission to own groups. : [OwnerId]

assigning Method run() with @Future
OwnerId = <active_admin_user>
CollaborationType = Public
Error = method didn’t execute, hence no Collaboration Group created.

Owner = <active_admin_user>
CollaborationType = Private
Error = System.DmlException: Insert failed. First exception on row 0; first error: INVALID_CROSS_REFERENCE_KEY, This user doesn’t have permission to own groups. : [OwnerId]

Apex Class:

global without sharing class PrepareMySandbox implements SandboxPostCopy {
global void runApexClass(SandboxContext context) {

    createChatterGroup();
}

global static void createChatterGroup() {
    User adminUser = new User(); // user having profile = System Administrator
    adminUser = [SELECT ID, username FROM User where isActive = TRUE AND username like '[email protected]%' LIMIT 1];
    CollaborationGroup OppForNetSuiteGroup = new CollaborationGroup(
        Name = Test.isRunningTest() ? 'Test Group' : 'My Group Name',
        Description = 'Some Group Description',
        CanHaveGuests = FALSE,
        CollaborationType = 'Public'
        OwnerId = adminUser.ID
    );

    insert OppForNetSuiteGroup;
}

}

Test Class

@isTest
public with sharing class PrepareMySandboxTest {

static testMethod void test_createChatterGroup(){
    
    Test.startTest();
        Test.testSandboxPostCopyScript(
            new PrepareMySandbox(),
            UserInfo.getOrganizationId(),
            UserInfo.getOrganizationId(),
            UserInfo.getOrganizationName());
        
        CollaborationGroup findChatterGroup = new CollaborationGroup();
        findChatterGroup = [SELECT Name, Owner.username from CollaborationGroup where Name = 'Test Group' LIMIT 1];
        system.assertEquals(findChatterGroup.Name, 'Test Group');
    Test.stopTest();
    
}

}

2 Answers 2

0

There is an Idea to include chatter groups sandbox refresh. https://ideas.salesforce.com/s/idea/a0B8W00000Gdf4QUAR/include-chatter-groups-in-sandbox-refresh-to-avoid-pb-errors

There you will find a comment with a code example, how the user solved this problem. Maybe you can use that code and make it work for you.

2
  • Already tried the code available there, before posting Query here ! It doesn't work. Jul 24, 2022 at 17:58
  • I am sorry. But I have no further ideas
    – q-jack
    Jul 25, 2022 at 7:53
-1

The documentation says

The SandboxPostCopy Apex class is executed at the end of the sandbox copy using a special Automated Process user that isn’t visible within the org. This user doesn’t have access to all object and features; therefore, the Apex script cannot access all objects and features. If the script fails, run the script after sandbox activation as a user with appropriate permissions.

https://developer.salesforce.com/docs/atlas.en-us.apexref.meta/apexref/apex_interface_System_SandboxPostCopy.htm

11
  • if that is the case, then why don't @future worked ? Jul 15, 2022 at 13:50
  • What do you mean by that? Try to run the code as another user. For Example as you from the debug console.
    – q-jack
    Jul 18, 2022 at 6:07
  • I know that while running the code as me in Developer Console is working fine. What I meant is "I tried annotating the method with @Future" in SandboxPostCopy class and it didn't work (refer my #3 scenario in question).</br > </br > @Future</br > global static void createChatterGroup()</br > Jul 18, 2022 at 10:13
  • how is the sharing defined in your apex class? with sharing or without sharing. When it is not seht to wihtout sharing it will run in the user context. In this case the user who executes the sandboxpostCopy class, does not have all permissions. It does not matter if it is a future method or not
    – q-jack
    Jul 18, 2022 at 10:45
  • I have used both "without sharing" including "at-Future" method and without "at-Future" method. It doesn't work and throws the same error "System.DmlException: Insert failed. First exception on row 0; first error: INVALID_CROSS_REFERENCE_KEY, This user doesn’t have permission to own groups. : [OwnerId]" Jul 19, 2022 at 12:05

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .