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 + '
';
}
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));
}
}
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
' + 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;
}
}
Any Advice would be much appreciated. Thanks.