Activity › Forums › Salesforce® Discussions › Can Anyone Explain Post Install and Uninstall Script in Salesforce?
Tagged: Global Interface, Method in Apex Class, Organization Id, Post Install Script, Salesforce Apex Class, Scripts in Salesforce, Uninstall Package, Uninstall Script, User Authentication, Version Control
-
Can Anyone Explain Post Install and Uninstall Script in Salesforce?
Posted by madhulika shah on July 23, 2018 at 2:10 PMExplain Post Install and Uninstall Script in Salesforce.
shariq replied 7 years, 7 months ago 3 Members · 2 Replies -
2 Replies
-
Post Install Script :
A post install script is an Apex class that implements the InstallHandler interface. This interface has a single method called onInstall that specifies the actions to be performed on installation.
global interface InstallHandler {
void onInstall(InstallContext context)
}The onInstall method takes a context object as its argument, which provides the following information:
1.The org ID of the organization in which the installation takes place.
2. The user ID of the user who initiated the installation.
3. The version number of the previously installed package (specified using the Version class). This is always a three-part number, such as 1.2.0.
4. Whether the installation is an upgrade.
5. Whether the installation is a push.Post Uninstall Script:
An uninstall script is an Apex class that implements the UninstallHandler interface. This interface has a single method called onUninstall that specifies the actions to be performed on uninstall.
global interface UninstallHandler {
void onUninstall(UninstallContext context)
}The onUninstall method takes a context object as its argument, which provides the following information:
1. The org ID of the organization in which the uninstall takes place.
2. The user ID of the user who initiated the uninstall. - [adinserter block='9']
-
Hi,
Post Install
Example –
global class PostInstallClass implements InstallHandler {
global void onInstall(InstallContext context) {
if(context.previousVersion() == null) {
Account a = new Account(name=’Newco’);
insert(a);Survey__c obj = new Survey__c(name=’Client Satisfaction Survey’);
insert obj;User u = [Select Id, Email from User where Id =:context.installerID()];
String toAddress= u.Email;
String[] toAddresses = new String[]{toAddress};
Messaging.SingleEmailMessage mail =
new Messaging.SingleEmailMessage();
mail.setToAddresses(toAddresses);
mail.setReplyTo(‘support@package.dev’);
mail.setSenderDisplayName(‘My Package Support’);
mail.setSubject(‘Package install successful’);
mail.setPlainTextBody(‘Thanks for installing the package.’);
Messaging.sendEmail(new Messaging.Email[] { mail });
}
else
if(context.previousVersion().compareTo(new Version(1,0)) == 0) {
Survey__c obj = new Survey__c(name=’Upgrading from Version 1.0′);
insert(obj);
}
if(context.isUpgrade()) {
Survey__c obj = new Survey__c(name=’Sample Survey during Upgrade’);
insert obj;
}
if(context.isPush()) {
Survey__c obj = new Survey__c(name=’Sample Survey during Push’);
insert obj;
}
}
}Post Uninstall
Example how to test uninstall script-
@isTest
static void testUninstallScript() {
Id UninstallerId = UserInfo.getUserId();
List<FeedItem> feedPostsBefore =
[SELECT Id FROM FeedItem WHERE parentId=:UninstallerId AND CreatedDate=TODAY];
Test.testUninstall(new UninstallClass());
List<FeedItem> feedPostsAfter =
[SELECT Id FROM FeedItem WHERE parentId=:UninstallerId AND CreatedDate=TODAY];
System.assertEquals(feedPostsBefore.size() + 1, feedPostsAfter.size(),
‘Post to uninstaller failed.’);
}Hope this helps.
Log In to reply.