Hi J.,
we are using postinstall class to do multiple task:
- assign permission set
- populate custom settings
- backup existing reference data
- insert new reference data
- upsert skuid pages
- backup skuid page assignments
- insert new page assignments
- 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 + '
';
}
else {
updateList.add(newPages[ndx].Name);
updatedPages += newPages[ndx].Name + '
';
}
}
else {
string errMsg = r.getErrors()[0].getMessage();
errMsg += ',' + newPages[ndx].Name + '
';
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
' + csvString;
}
insert batch;
string feedTitle = 'Page Upsert Result:
';
string msgBody = 'Upsert Result:
';
msgBody += newPages.size() + ' pages processed
';
msgBody += insertList.size() + ' pages inserted
';
msgBody += updateList.size() + ' pages updated
';
msgBody += upsertErrList.size() + ' pages failed
';
msgBody += 'Inserted Pages:
' + insertedPages + '
';
msgBody += 'Updated Pages:
' + updatedPages + '
';
msgBody += 'Failed Pages:
' + 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
<b><i>RefreshPagesInModule('Module_Name');</i></b>
// 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.