Deploying Child Pages with Post Install Apex Script

  • 1
  • Problem
  • Updated 2 years ago
  • Acknowledged
Figured this should have its own thread. Continued from here: https://community.skuidify.com/skuid/topics/appexchange-package-upload-to-include-custom-themes

Child pages do not update at all via the post install script and are untouched. Manually I can unpack the pages from the static resource.

I am unable to figure out how to get my post install script to auto deploy my child pages. I imagine this has to do with every org having different Ids for pages. The script would need to identify master pages, deploy those first, identify child pages, and link those back to the already deployed master pages. 
Photo of Craig Rosenbaum

Craig Rosenbaum

  • 4,776 Points 4k badge 2x thumb

Posted 3 years ago

  • 1
Photo of J.

J., Official Rep

  • 7,470 Points 5k badge 2x thumb
Craig,

Which Apex method are you using to unpack those Child Pages in your Post Install script? We may have bug, but we are at least attempting to account for this scenario in 
RefreshPagesFromStaticResource and RefreshSkuidPages. In your Page Pack, do your pages have a value for skuid__MasterPage__r.skuid__UniqueId__c on those Child Pages?
Photo of Craig Rosenbaum

Craig Rosenbaum

  • 4,776 Points 4k badge 2x thumb
Here's my full post install script. Note, it should be the same as the sample one here. Where do you want me to check for the skuid__MasterPage__r.skuid__UniqueId__c value? In my development environment or in a org that I'm trying to install the package in? Currently, the child pages do not deploy automatically. If I manually click "unpack from page pack" they deploy correctly with the correct master page assigned.

 public without sharing class InstallScript_CheetahBMS implements InstallHandler {    
    public static final String NAMESPACE_PREFIX = 'CheetahBMS';
    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);
        IsRunning=false;
    }
    
    public static List<skuid__Page__c> 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<skuid__Page__c> newPages
             = (List<skuid__Page__c>) JSON.deserialize(sr.Body.toString(),List<skuid__Page__c>.class);
        List<Schema.SObjectField> layoutFields = new List<Schema.SObjectField>{
            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<Database.UpsertResult> cr = Database.upsert(newPages,f,false);
        }   
        return newPages;
    }
    
}
Photo of Craig Rosenbaum

Craig Rosenbaum

  • 4,776 Points 4k badge 2x thumb
I looked at the code in my Page Pack. Here's what all of my child pages show: 

"skuid__MasterPage__c":"a04j000000AIaOnAAL","skuid__MasterPage__r":{"attributes":{"type":"skuid__Page__c","url":"/services/data/v34.0/sobjects/skuid__Page__c/a04j000000AIaOnAAL"},"Name":"CheetahNavigation","skuid__UniqueId__c":"CheetahBMS_CheetahNavigation"}
Photo of J.

J., Official Rep

  • 7,470 Points 5k badge 2x thumb
OK, so the Page Pack does have a value for skuid__MasterPage__r.skuid__UniqueId__c, which is good, because that's the only reliable way to identify the same Page in two orgs. Looking at your Post Install script though, I see the issue: the tutorial is out of date as of Banzai. The gist of it is that version of the script doesn't account for the Master/Child page functionality because it was written before they existed. After doing the initial upsert of all the Pages in the Page Pack, you have to go through all the Child Pages and associate them with the correct Master Page (which may not even be in the Page Pack). We'll get the tutorial updated, but that's the overall strategy.
Photo of Gyan

Gyan

  • 1,400 Points 1k badge 2x thumb
Hi J.,

we are using postinstall class to do multiple task:

1. assign permission set
2. populate custom settings
3. backup existing reference data 
4. insert new reference data
5. upsert skuid pages
7. backup skuid page assignments
8. insert new page assignments
9. initiate batch apex (if needed)

I used skuid provided code for upserting  pages with little modification. i.e. I created private method to process skuid pages like this: 



    // private method to upsert pages from static resource (JSON)    
private static void RefreshPagesInModule(String module) {
        string[] upsertErrList = new List<string>();
        string[] updateList = new List<string>();
        string[] insertList = new List<string>();
        string insertedPages = '';
        string updatedPages = '';
        string failedPages = '';
        cloupra__Batch__c batch = new cloupra__Batch__c();
        string csvString  = '';
        Attachment att;
        string event;
        
        // 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<skuid__Page__c> newPages
        = (List<skuid__Page__c>) JSON.deserialize(sr.Body.toString(),List<skuid__Page__c>.class);
        List<Schema.SObjectField> layoutFields = new List<Schema.SObjectField>{
            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;
            try {
                event = 'upsert';
                List<Database.UpsertResult> cr = Database.upsert(newPages,f,false);
                integer ndx = 0;
                
                for (Database.UpsertResult r : cr) {
                    
                    if (r.isSuccess()) {
                        // Operation was successful, so get the ID of the record that was processed
                        //System.debug('Successfully Inserted record: ' + r.getId() + ' - ' + r);
                        
                        if(r.isCreated()) { 
                            insertList.add(newPages[ndx].Name);
                            insertedPages += newPages[ndx].Name + '\n';
                        }
                        else {
                            updateList.add(newPages[ndx].Name);
                            updatedPages += newPages[ndx].Name + '\n';
                        }
                    }
                    else {
                        string errMsg = r.getErrors()[0].getMessage();
                        errMsg += ',' + newPages[ndx].Name + '\n';
                        upsertErrList.add(errMsg);
                    }
                    ndx++; 
                }
            
            }
            catch (exception e) {
                 system.debug(logginglevel.info, 'upsert error--------- ' + string.valueOf(e));
            }       
        }        
        if(!newPages.isEmpty()) {
            batch.cloupra__Status__c = 'Completed';
            batch.cloupra__Description__c = 'Batch_Creted_By_Post_Install_Script - skuid__Page__c Upsert';
            
            if(!upsertErrList.isEmpty()) {
                batch.cloupra__Status__c = 'Completed (With Errors)';
                
                for(string s: upsertErrList) {
                    csvString = csvString + s;
                }
                
                csvString = 'Error Message,Page Name\n' + csvString;
            }
                        
            insert batch;
            
            string feedTitle = 'Page Upsert Result:\n';
            string msgBody = 'Upsert Result:\n';
            msgBody += newPages.size() + ' pages processed \n';
            msgBody += insertList.size() + ' pages inserted\n';
            msgBody += updateList.size() + ' pages updated\n';
            msgBody += upsertErrList.size() + ' pages failed\n\n';
            msgBody += 'Inserted Pages:\n' + insertedPages + '\n';
            msgBody += 'Updated Pages:\n' + updatedPages + '\n';
            msgBody += 'Failed Pages:\n' + csvString;
            
            if(!upsertErrList.isEmpty()) {
                att = new Attachment();
                att.Name = 'Page upsert error result';
                att.Description = msgBody;
                att.Body = blob.valueOf(csvString);
                att.ParentId = batch.Id;
                insert att; 
            }
            
            FeedItem post = new FeedItem();
            post.ParentId = batch.Id;
            post.Body = msgBody;
            post.Type = 'TextPost';
            post.Title = feedTitle;
            insert post;           
        }        
    }

And called this method form OnInstall method like this


    global void onInstall(InstallContext context) {       
     IsRunning = true;
        // if this is an upgrade
        if(context.isUpgrade() || context.isPush()) {
            // upsert skuid pages
RefreshPagesInModule('Module_Name');
                
            // Assign required permission sets   
            // This check is to avoid mixed-dml-error in test class         
            
            if(Test.isRunningTest()) {
                User adminuser = [select Id from User where Profile.Name = 'System Administrator' and IsActive = true limit 1];
                System.runAs(adminuser){
                    Utils.AssignPermissionSets();
                }
            }
            else {
                Utils.AssignPermissionSets();
            }            
            
            // Populate Custom Settings fields
            Utils.PopulateCustomSettings();               
        
            // Take full backup of clients reference data and save to Documents (PractiFI Backup folder)
            Utils.BackupRefData();              
        
            //Populate Reference data 
            Utils.InsertNewRefData();
            
            //Update Content Items for process launch
            Utils.UpdateContentItems();              
            
            
            if(context.previousVersion().compareTo(new Version(5,10)) < 0) {
                // Update ProcessSteps
                Utils.UpdateProcessSteps();
                
                // Update Task Rich Description                
                string updateTaskDescriptions = Database.executeBatch(new BatchableJob(new ChangeTaskDescription()), 200);
            }                     
        }
        IsRunning = false;        
    }


Now when I see the tutorial (which presumably addressed master page issue), it's a class that was extended from skuid's global class InstallScript.

I was wondering how could I use this new approach to upsert pages without breaking my postinstall script.   Or can I get an idea how I can update my existing method  RefreshPagesInModule to handle Master Page issue. 

Thanks. 
(Edited)
Photo of Gyan

Gyan

  • 1,400 Points 1k badge 2x thumb
Hi J., 

I updated the method RefreshPagesInModule  to handle Master and child pages and now seems to work well. Just wanted to know if I'm doing anything wrong in the method: 


   // private method to upsert pages from static resource (JSON)   
   private static void RefreshPagesInModule(String module) {
        string[] upsertErrList = new List<string>();
        string[] updateList = new List<string>();
        string[] insertList = new List<string>();
        string insertedPages = '';
        string updatedPages = '';
        string failedPages = '';
        cloupra__Batch__c batch = new cloupra__Batch__c();
        string csvString  = '';
        Attachment att;
        string event;
        
        // 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<skuid__Page__c> newPages
        = (List<skuid__Page__c>) JSON.deserialize(sr.Body.toString(),List<skuid__Page__c>.class);
        List<Schema.SObjectField> layoutFields = new List<Schema.SObjectField>{
            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
        };        
        
        
        Map<string, string> pageMap = new Map<string, string>();
        
        for (skuid__Page__c p : newPages) {
            if(p.skuid__MasterPage__c != null) {
                // map of unique Id of page to unique id of master page to be used to set master page for child pages in newPages list
                pageMap.put(p.skuid__UniqueId__c, p.skuid__MasterPage__r.skuid__UniqueId__c);
                // Get rid of the master page Ids so that upsert will proceed           
                p.skuid__MasterPage__c = null;
                p.skuid__MasterPage__r = null;
            }
            // 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;
            try {
                event = 'upsert';
                List<Database.UpsertResult> cr = Database.upsert(newPages,f,false);
                integer ndx = 0;
                
                for (Database.UpsertResult r : cr) {
                    
                    if (r.isSuccess()) {
                        // Operation was successful, so get the ID of the record that was processed
                        //System.debug('Successfully Inserted record: ' + r.getId() + ' - ' + r);
                        
                        if(r.isCreated()) { 
                            insertList.add(newPages[ndx].Name);
                            insertedPages += newPages[ndx].Name + '\n';
                        }
                        else {
                            updateList.add(newPages[ndx].Name);
                            updatedPages += newPages[ndx].Name + '\n';
                        }
                    }
                    else {
                        string errMsg = r.getErrors()[0].getMessage();
                        errMsg += ',' + newPages[ndx].Name + '\n';
                        upsertErrList.add(errMsg);
                    }
                    ndx++; 
                }            
            }
            catch (exception e) {
                 system.debug(logginglevel.info, 'upsert error--------- ' + string.valueOf(e));
            }       
        }
        
        skuid__Page__c[]  allPages = [select Id, skuid__UniqueId__c, skuid__MasterPage__c from skuid__Page__c where skuid__Module__c = :module];
        Map<string, string> IdUniqueIdMap = new Map<string, string>();
        
        for(skuid__Page__c p: allPages) {
            IdUniqueIdMap.put(p.skuid__UniqueId__c, p.Id);
        }
        skuid__Page__c [] childPages = new List<skuid__Page__c>();
        
        for(skuid__Page__c p: allPages) {
            // if page did not have master page in the pagepack continue
            if(pageMap.get(p.skuid__UniqueId__c) == null) continue;
            p.skuid__MasterPage__c = IdUniqueIdMap.get(pageMap.get(p.skuid__UniqueId__c));
            childPages.add(p);
        }
        
        if(!childPages.isEmpty()) {       
            database.update(childPages, false); 
        }
        
        if(!newPages.isEmpty()) {
            batch.cloupra__Status__c = 'Completed';
            batch.cloupra__Description__c = 'Batch_Creted_By_Post_Install_Script - skuid__Page__c Upsert';
            
            if(!upsertErrList.isEmpty()) {
                batch.cloupra__Status__c = 'Completed (With Errors)';
                
                for(string s: upsertErrList) {
                    csvString = csvString + s;
                }
                
                csvString = 'Error Message,Page Name\n' + csvString;
            }
                        
            insert batch;
            
            string feedTitle = 'Page Upsert Result:\n';
            string msgBody = 'Upsert Result:\n';
            msgBody += newPages.size() + ' pages processed \n';
            msgBody += insertList.size() + ' pages inserted\n';
            msgBody += updateList.size() + ' pages updated\n';
            msgBody += upsertErrList.size() + ' pages failed\n\n';
            msgBody += 'Inserted Pages:\n' + insertedPages + '\n';
            msgBody += 'Updated Pages:\n' + updatedPages + '\n';
            msgBody += 'Failed Pages:\n' + csvString;
            
            if(!upsertErrList.isEmpty()) {
                att = new Attachment();
                att.Name = 'Page upsert error result';
                att.Description = msgBody;
                att.Body = blob.valueOf(csvString);
                att.ParentId = batch.Id;
                insert att; 
            }
            
            FeedItem post = new FeedItem();
            post.ParentId = batch.Id;
            post.Body = msgBody;
            post.Type = 'TextPost';
            post.Title = feedTitle;
            insert post;           
        }     
    }

Any Advice would be much appreciated. Thanks.