Use apex to package and unpackage pages in a module?

  • 1
  • Question
  • Updated 3 years ago
  • Answered
I'm working on trying to better automated some of the steps in our deployment process, and am wondering if there's a way to run the package & unpackage pages features (where the pages are stored/pulled from a static resource) from apex. I'm using the metadata API to pull down our changes from our dev sandbox & push them to our QA sandbox, but right now, I have to go into the dev sandbox, package the pages, then remember to unpackage them once the deployment is done in the QA sandbox.

I think I have a way to execute anonymous apex with the metadata API - does Skuid currently give us a way to execute these 2 functions in apex?
Photo of Jonathan Gillespie

Jonathan Gillespie

  • 870 Points 500 badge 2x thumb

Posted 4 years ago

  • 1
Photo of Zach McElrath

Zach McElrath, Employee

  • 49,056 Points 20k badge 2x thumb
Here are some the methods Skuid provides in Apex. The "PackagePages" variations do almost everything that the "Build Page Pack" and "Package Pages in Module" buttons on the Skuid Pages List page do, except for actually creating/updating the StaticResources. In the Pages List page, Skuid uses the Tooling API to create/update the pages. We have so far opted not to do any Metadata creation / updates in Apex land because that would require us to add a Remote Site for every Salesforce Instance (na1, na2, cs30, etc...) to our package. However, in Visualforce land, we don't need to do this: Visualforce can run the Tooling API without any security settings modification. 

With that note of background, here's some of what you can use from Apex:

** UNPACKING PAGES **

To unpack pages from a StaticResource and do an UPSERT, use the following:

skuid.Utils.RefreshPagesFromStaticResource(staticResourceNames);

for example if you want to unpack a PagePack called "MyModulePages", do:

skuid.Utils.RefreshPagesFromStaticResource(new List<String>{'MyModulePages'}); 

If the StaticResource is in a Namespace, there is another variant where you can pass the method a List<StaticResource> records.

To unpack pages from a StaticResource, delete all existing pages in a Module, then an insert on the unpacked pages, use the following:

skuid.Utils.RefreshPagesInModule(moduleName);

** PACKAGING PAGES **

To grab all pages in a Module as a JSON string suitable for inclusion in a StaticResource body:

skuid.Utils.PackagePagesInModule(moduleName,sendEmail) 

This method returns all Pages in the given Module as a JSON string. If sendEmail is true, it will also send a .json file to you as an email attachment.

So what I imaging you could do, since you're doing Metadata API stuff already (are you using the Financial Force MDAPI package?) is to grab this String, convert it to a Blob / BASE64-encode it, and then put it into the Body of a StaticResource and do an Update. 
Photo of Zach McElrath

Zach McElrath, Employee

  • 49,056 Points 20k badge 2x thumb
Craig, that's a good question --- I believe that if you're using Trialforce you will not need to add an upsert to the post-install script, Trialforce should take whatever custom settings you have configured and use those. 
Photo of Craig Rosenbaum

Craig Rosenbaum

  • 4,776 Points 4k badge 2x thumb
Hi Zach, wanted to follow up and see if you had information on an auto update of page assignments like you mentioned above. 
Photo of Craig Rosenbaum

Craig Rosenbaum

  • 4,776 Points 4k badge 2x thumb
Hi Zach, wanted to follow up and see if you had information on an auto update of page assignments like you mentioned above. 
Photo of Craig Rosenbaum

Craig Rosenbaum

  • 4,776 Points 4k badge 2x thumb
Zach, just a bump to see if you could send some examples of the auto deployment page assignments like you mentioned above. 
Photo of Craig Rosenbaum

Craig Rosenbaum

  • 4,776 Points 4k badge 2x thumb
Zach, just a bump to see if you could send some examples of the auto deployment page assignments like you mentioned above. 
Photo of Gyan

Gyan

  • 1,400 Points 1k badge 2x thumb
Hi Zach,  to upsert the pages form pagepack (static resource) we were using another following version of skuid Utils method: 

static skuid.Utils.PageRefreshResult RefreshPagesFromStaticResource(List srList)

in our postinstall script . However, its preventing us from package install now ( I got error while installing Beta package in test org). 

Here's how our postinstall class looks like: 


global class PostInstallScript implements InstallHandler {    
    public PostInstallScript() {}
    global void onInstall(InstallContext context) {
        // if this is an upgrade
        if(context.isUpgrade() || context.isPush()) {
            // upsert Skuid pages
List<StaticResource> srList = new List<StaticResource>();
for(StaticResource s : [select Id, body from StaticResource where Name like '%allOther_skuidUpdate']){
srList.add(s);
}            
try {
skuid.Utils.RefreshPagesFromStaticResource(srlist);
}
catch(exception e) {
system.debug(e);
}
                        
        }        
    }
}



Any idea why we are getting package install failure?

Thanks.
Gyan
(Edited)
Photo of J.

J., Official Rep

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

I think we've had issues using the Utils methods in Post Install Scripts because Utils.cls is declared with sharing and the "Ghost User" that is actually running the script doesn't handle it. It could be something else, but that's the first thing that comes to mind. If you try running the query and call to Utils.RefreshPagesFromStaticResource via Anonymous Apex, does it work?
Photo of Gyan

Gyan

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

Thank you for your reply. Yes running Utils.RefreshPagesFromStaticResource  from Anonymous apex works. Not only that, it worked from PostInstall script as well previously (in previous version of PractiFI). Then I used another approach that skuid provided in tutorial which worked well so I was happy until I got issue with Master page. Please see my post at: 

Deploying Child Pages with Post Install Apex Script

https://community.skuid.com/skuid/topics/deploying-child-pages-with-post-install-apex-script?topic-r...

And lastly, I am not getting any notification for the community post update (I have opted in for getting notifications). Any Idea?

Kind regards,

Gyan (Jnanendra)