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();
}
}