You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

如何在Salesforce一代包升级时保留自定义设置的复选框状态?

包升级时保留自定义设置的用户修改值

核心逻辑

Salesforce包升级时,会自动用包内自定义设置的默认值覆盖用户环境中已修改的值。要避免TestCheckbox__c被重置为false,需要在升级前备份用户的现有设置,升级完成后再恢复,通过预安装(PreInstall)和后安装(PostInstall)脚本实现。

具体实现

1. 创建预安装备份类

这个类会在升级开始前执行,保存用户当前的TestCheckbox__c状态:

global class CustomSettingPreUpgradeHandler implements PreInstallHandler {
    // 静态变量临时存储用户的设置值,安装过程中会保留
    public static Map<Id, Boolean> savedCheckboxStates = new Map<Id, Boolean>();

    global void onPreInstall(PreInstallContext context) {
        // 仅在升级时执行,跳过首次安装
        if (context.isUpgrade()) {
            // 针对列表型自定义设置:备份所有记录的复选框值
            for (TestCustomSetting__c setting : [SELECT Id, TestCheckbox__c FROM TestCustomSetting__c]) {
                savedCheckboxStates.put(setting.Id, setting.TestCheckbox__c);
            }

            // 如果是层次型自定义设置,备份组织默认值:
            // TestCustomSetting__c orgDefault = TestCustomSetting__c.getOrgDefaults();
            // if (orgDefault != null) {
            //     savedCheckboxStates.put(orgDefault.Id, orgDefault.TestCheckbox__c);
            // }
        }
    }
}

2. 创建后安装恢复类

这个类会在升级完成后执行,把备份的设置值写回自定义设置:

global class CustomSettingPostUpgradeHandler implements PostInstallHandler {
    global void onPostInstall(PostInstallContext context) {
        // 仅在升级时执行
        if (context.isUpgrade()) {
            List<TestCustomSetting__c> settingsToRestore = new List<TestCustomSetting__c>();

            // 恢复列表型自定义设置的复选框值
            for (Id settingId : CustomSettingPreUpgradeHandler.savedCheckboxStates.keySet()) {
                TestCustomSetting__c setting = new TestCustomSetting__c(
                    Id = settingId,
                    TestCheckbox__c = CustomSettingPreUpgradeHandler.savedCheckboxStates.get(settingId)
                );
                settingsToRestore.add(setting);
            }

            // 如果是层次型,恢复组织默认值:
            // if (!CustomSettingPreUpgradeHandler.savedCheckboxStates.isEmpty()) {
            //     Id orgDefaultId = CustomSettingPreUpgradeHandler.savedCheckboxStates.keySet().iterator().next();
            //     TestCustomSetting__c orgDefault = new TestCustomSetting__c(
            //         Id = orgDefaultId,
            //         TestCheckbox__c = CustomSettingPreUpgradeHandler.savedCheckboxStates.get(orgDefaultId)
            //     );
            //     settingsToRestore.add(orgDefault);
            // }

            // 批量更新恢复设置
            if (!settingsToRestore.isEmpty()) {
                update settingsToRestore;
            }
        }
    }
}

3. 包版本配置

上传包的新版本时,在安装设置中指定:

  • 预安装脚本:CustomSettingPreUpgradeHandler
  • 后安装脚本:CustomSettingPostUpgradeHandler

4. 验证步骤

  1. 安装旧版本包,手动将TestCheckbox__c勾选为true。
  2. 安装包含上述脚本的新版本包。
  3. 查看TestCustomSetting__c的TestCheckbox__c字段,确认仍为true,未被重置。

注意事项

  • 区分首次安装和升级:必须用context.isUpgrade()判断,避免首次安装时覆盖包的初始默认值。
  • 自定义设置类型:根据你的自定义设置是列表型还是层次型,选择对应的代码块(注释中已标出)。
  • 权限确保:脚本类需要有修改TestCustomSetting__c的权限,建议用with sharing或确保包的权限集包含该权限。

内容的提问来源于stack exchange,提问作者spuamtiitl

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.06.29 17:25:58