Find your content:

Search form

You are here

Post Install Script - Is it possible to run as a proper user?

 
Share

I would like to create a CollaborationGroup when a managed package is installed. Currently I get an error

'INVALID_CROSS_REFERENCE_KEY, This user can't be added to the group.: []'

when I run the following in a postInstall Script

     CollaborationGroup cg = new CollaborationGroup();
        cg.name = 'Lorem';
        cg.description = 'Lorem Ipsum';
        cg.collaborationType = 'Private';

        insert cg;

I believe this is due to the special user context the postinstall script runs under as the code works fine if run normally. Any way around this?


Attribution to: Richard Durrant

Possible Suggestion/Solution #1

The short answer is not you can not run as a proper user. But you can get the Id of the user who initiated the install using

context.installerId()

My suggestion for you Chatter Group problem would be to try and set the OwnerId of the CollaborationGroup e.g

global class PostInstallClass implements InstallHandler {
    global void onInstall(InstallContext context) {
        CollaborationGroup cg = new CollaborationGroup(
            Name='Lorem',
            Description='Lorem Ipsum',
            CollaborationType='Private',
            OwnerId=context.installerId()
        );
        insert cg;
    }
}

Disclaimer: I have not tested this so I don't know if it will work for sure.


Attribution to: Daniel Blackhall
This content is remixed from stackoverflow or stackexchange. Please visit https://salesforce.stackexchange.com/questions/4282

My Block Status

My Block Content