i have a custom setting named lendingxl__LeadApplicationFieldMapping__c in my org. i want to create it's record after installing the package. & update the same records after upgrading the package. So How should i write the apex class for it ?
2 Answers
This is the pattern we use:
global class YourInstallHandler implements InstallHandler {
private Version previousVersion;
global void onInstall(InstallContext context) {
if (context.previousVersion() != null) {
previousVersion = context.previousVersion();
doUpgrade();
} else {
doInstall();
}
}
private void doUpgrade() {
// Version upgrade steps with more added over time
if (isInstalledVersionBefore(new Version(1, 3))) {
...
}
if (isInstalledVersionBefore(new Version(1, 12))) {
...
}
if (isInstalledVersionBefore(new Version(2, 3))) {
...
}
...
}
private void doInstall() {
// Any first time install logic
...
}
private Boolean isInstalledVersionBefore(Version version) {
return previousVersion.compareTo(version) < 0;
}
}
where over time more version upgrade steps are added.
-
Thank you very much @Keith C. It is very much useful for me Commented Feb 1, 2017 at 11:52
Well you have to implement the InstallHandler interface so that after installation the methods defined would work.
There are context variables like
context.isUpgrade()
/ context.isPush()
that will tell you if its a first installation or push instalation.
Similarly you can delete the data via implementing UninstallInterface
.
https://developer.salesforce.com/docs/atlas.en-us.packagingGuide.meta/packagingGuide/apex_uninstall_script_intro.htm
As you said you have to update your custom settings value, I would suggest you to not use any InstallScript and replace custom setings via custom metadata types. Records of custom metadata types can be deployed.