AppExchange Package Upload to Include Custom Themes

Hi Greg,

Here is a modified InstallScript that contains methods which should enable you to auto-create skuid__Theme__c records when installing your package into a customer’s org. In this example I’m assuming that you’re only including one Theme with your package, whose Name is “StockCheck” and which is stored in a Static Resource that is included in your package(very important!) called “StockCheckTheme”. This is just an example, you’ll need to to adjust to be whatever your Theme’s Name and Static Resource Name are.

Also, this post-install script does NOT automatically set the Org-Default Theme preference or change any Profile/User theme settings to be your Theme. If you want to do this this is possible but would take some more code.

 

public without sharing class StockCheckInstallScript implements InstallHandler {
    public static final String NAMESPACE_PREFIX = ‘gbc’;
    
    public static boolean IsRunning { 
        public get {
            if (IsRunning==null) IsRunning = false;
            return IsRunning;
        }
        public set; 
    }
    
    public void onInstall(InstallContext ctx) {
        IsRunning=true;
        RefreshPagesInModule(NAMESPACE_PREFIX);
        CreateDefaultThemes();
        IsRunning=false;
    }
    
    public static List RefreshPagesInModule(String module) {
        
        // See if a StaticResource containing new pages for this module yet exists      
        StaticResource sr = [
            select Body
            from StaticResource 
            where Name = :(module + ‘Pages’)
            and ((NamespacePrefix = NULL) OR (NamespacePrefix = :module))
            limit 1
        ];
        
        // The new Pages for our module that we will be inserting       
        List newPages
             = (List) JSON.deserialize(sr.Body.toString(),List.class);
        List layoutFields = new List{
            skuid__Page__c.skuid__Layout__c,
            skuid__Page__c.skuid__Layout2__c,
            skuid__Page__c.skuid__Layout3__c,
            skuid__Page__c.skuid__Layout4__c,
            skuid__Page__c.skuid__Layout5__c
        };
        
        for (skuid__Page__c p : newPages) {
            // Get rid of the Ids so that upsert will proceed           
            p.Id = null;
            // Ensure that unused Layout fields are set to null         
            for (Schema.Sobjectfield f : layoutFields) {
            if (p.get(f)==null) p.put(f,null);
            }
        }
        
        // If we have successfully compiled new Pages for this module,        
       // delete the old ones and replace them with the new.        
       if (newPages != null && !newPages.isEmpty()) {
        Schema.SObjectField f = skuid__Page__c.skuid__UniqueId__c;
        List cr = Database.upsert(newPages,f,false);
        }   
        return newPages;
    }
    
    // Returns a Map containing the definitions for expected default themes
   public static Map> GetDefaultThemes() {
      return new Map>{
         // Desktop Themes
         ‘StockCheck’    => new Map{ 
            ‘type’ => ‘Desktop’, 
            ‘resourceName’ => ‘StockCheckTheme’ 
         }
      };
   }
      
   // Create our default, expected Theme custom setting records if they don’t exist 
   public static void CreateDefaultThemes() {
      
      Map existingThemes = skuid__Theme__c.getAll();
      Map> defaultThemes = GetDefaultThemes();
      Set themeNamesToCreate = new Set();
      for (String themeName : defaultThemes.keyset()) {
         if (!existingThemes.containsKey(themeName)) {
            themeNamesToCreate.add(themeName); 
         }  
      }
      
      // If there are some default themes that do not exist in this org,
      // create them!
      if (!themeNamesToCreate.isEmpty()) {
         List themesToCreate = new List();
         for (String themeName : themeNamesToCreate) {
            Map themeDef = defaultThemes.get(themeName);
            themesToCreate.add(new skuid__Theme__c(
               Name                    = themeName,
               skuid__Resource_Namespace__c  = NAMESPACE_PREFIX,
               skuid__Resource_Name__c       = themeDef.get(‘resourceName’),
               skuid__Active__c           = true,
               skuid__Type__c                = themeDef.get(‘type’)
            ));
         }
         insert themesToCreate;  
      }
   }
       
}