Note: This release is in preview. Features described here don’t become generally available until the latest general availability date that Salesforce announces for this release. Before then, and where features are noted as beta, pilot, or developer preview, we can’t guarantee general availability within any particular time frame or at all. Make your purchase decisions only on the basis of generally available products and features.
Sample Post Install Script for a Push Upgrade for First- and Second-Generation Managed Packages
Automate the assignment of new components to existing users of a package.
For more information on writing a post-install Apex script, see Run Apex on Package Install/Upgrade.
In this sample script, the package upgrade contains new Visualforce pages and a new permission set that grants access to those pages. The script performs the following actions.
- Gets the Id of the Visualforce pages in the old version of the package
- Gets the permission sets that have access to those pages
- Gets the list of profiles associated with these permission sets
- Gets the list of users who have those profiles assigned
- Assigns the permission set in the new package to those users
1global class PostInstallClass implements InstallHandler {
2 global void onInstall(InstallContext context) {
3
4 //Get the Id of the Visualforce pages
5 List<ApexPage> pagesList = [SELECT Id FROM ApexPage WHERE NamespacePrefix =
6 'TestPackage' AND Name = 'vfpage1'];
7
8 //Get the permission sets that have access to those pages
9 List<SetupEntityAccess> setupEntityAccessList = [SELECT Id,
10 ParentId, SetupEntityId, SetupEntityType FROM SetupEntityAccess
11 WHERE SetupEntityId IN :pagesList];
12 Set<ID> PermissionSetList = new Set<ID> ();
13
14 for (SetupEntityAccess sea : setupEntityAccessList) {
15 PermissionSetList.add(sea.ParentId);
16 }
17 List<PermissionSet> PermissionSetWithProfileIdList =
18 [SELECT id, Name, IsOwnedByProfile, Profile.Name,
19 ProfileId FROM PermissionSet WHERE IsOwnedByProfile = true
20 AND Id IN :PermissionSetList];
21
22 //Get the list of profiles associated with those permission sets
23 Set<ID> ProfileList = new Set<ID> ();
24 for (PermissionSet per : PermissionSetWithProfileIdList) {
25 ProfileList.add(per.ProfileId);
26 }
27
28 //Get the list of users who have those profiles assigned
29 List<User> UserList = [SELECT id FROM User where ProfileId IN :ProfileList];
30
31 //Assign the permission set in the new package to those users
32 List<PermissionSet> PermissionSetToAssignList = [SELECT id, Name
33 FROM PermissionSet WHERE Name='TestPermSet' AND
34 NamespacePrefix = 'TestPackage'];
35 PermissionSet PermissionSetToAssign = PermissionSetToAssignList[0];
36 List<PermissionSetAssignment> PermissionSetAssignmentList = new List<PermissionSetAssignment>();
37 for (User us : UserList) {
38 PermissionSetAssignment psa = new PermissionSetAssignment();
39 psa.PermissionSetId = PermissionSetToAssign.id;
40 psa.AssigneeId = us.id;
41 PermissionSetAssignmentList.add(psa);
42 }
43 insert PermissionSetAssignmentList;
44 }
45}1// Test for the post install class
2@isTest
3private class PostInstallClassTest {
4 @isTest
5 public static void test() {
6 PostInstallClass myClass = new PostInstallClass();
7 Test.testInstall(myClass, null);
8 }
9}