Asset 360 Suite > Upgrading the Asset 360 Package > Upgrade Instructions > Running the Apex Script for Product Service Campaign Updates
Running the Apex Script for Product Service Campaign Updates
When you upgrade the Asset 360 org from 10.0 version to a later version, you must run an Apex script manually to update the org with a Product Service Campaign change. This update is not incorporated in the post-installation script.
The Apex script populates the value in the newly introduced Delivery Method field from the previously existing Delivery Method field in Product Service Campaign and Product Service Campaign Line Item objects.
* 
Provide valid email address for String emailAddress1 and String emailAddress2 before running the script. These email addresses are for success and failure notifications. You can provide the same email address in both fields, or two different email addresses.
Run the following script after upgrading the Asset 360 org from the 10.0 version:
//Please provide the email addresses
String emailAddress1 = '';
String emailAddress2 = '';

public List
emailAddressList = new List
();
if(String.isNotBlank(emailAddress1)){
emailAddressList.add(emailAddress1);
}

if(String.isNotBlank(emailAddress2)){
emailAddressList.add(emailAddress2);
}

Integer totalPscItemsCount = Database.countQuery('SELECT count() FROM ProductServiceCampaignItem WHERE SVMXA360__DeliveryMethod__c != null AND SVMXA360__SM_DeliveryMethod__c = null');
Integer totalPscCount = Database.countQuery('SELECT count() FROM ProductServiceCampaign WHERE SVMXA360__DeliveryMethod__c != null AND SVMXA360__SM_DeliveryMethod__c = null');
Boolean isRerunRequired = Limits.getLimitDmlRows() < (totalPscCount + totalPscItemsCount);

Map
pscItemFailedErrorMap = new Map
();
Map
pscFailedErrorMap = new Map
();

List
pscList = [SELECT Id, SVMXA360__DeliveryMethod__c, SVMXA360__SM_DeliveryMethod__c FROM ProductServiceCampaign WHERE SVMXA360__DeliveryMethod__c != null AND SVMXA360__SM_DeliveryMethod__c = null];

if(!pscList.isEmpty()){

for(ProductServiceCampaign psc : pscList) {
psc.SVMXA360__SM_DeliveryMethod__c = psc.SVMXA360__DeliveryMethod__c;
}

Database.SaveResult[] pscSaveResultList = Database.update(pscList,false);
Integer pscCounter = 0;
for(Database.SaveResult sr : pscSaveResultList){
if(!sr.isSuccess()) {
String errorMsg = sr.getErrors()[0].getMessage();
pscFailedErrorMap.put(pscList[pscCounter].Id, errorMsg);
}
pscCounter++;
}
}

Integer remainingDMLRowCount = Limits.getLimitDmlRows() - Limits.getDMLRows();

List
pscItemList = Database.query('SELECT Id, ProductServiceCampaignId, SVMXA360__DeliveryMethod__c, SVMXA360__SM_DeliveryMethod__c FROM ProductServiceCampaignItem WHERE SVMXA360__DeliveryMethod__c != null AND SVMXA360__SM_DeliveryMethod__c = null LIMIT :remainingDMLRowCount');


for(ProductServiceCampaignItem pscItem : pscItemList) {
pscItem.SVMXA360__SM_DeliveryMethod__c = pscItem.SVMXA360__DeliveryMethod__c;
}

Database.SaveResult[] pscItemSaveResultList = Database.update(pscItemList,false);

Integer pscItemCounter = 0;
for(Database.SaveResult sr : pscItemSaveResultList){
if(!sr.isSuccess()) {
String errorMsg = sr.getErrors()[0].getMessage();
pscItemFailedErrorMap.put(pscItemList[pscItemCounter].Id, errorMsg);
}
pscItemCounter++;
}

Messaging.EmailFileAttachment attachment;
if(!pscFailedErrorMap.isEmpty() || !pscItemFailedErrorMap.isEmpty()){
String logDetails = '';
if(!pscFailedErrorMap.isEmpty()){
logDetails = 'Please find the below list of Product Service Campaigns which got failed while updating the delivery method: \n';
Integer counter = 1;
for(String pscId : pscFailedErrorMap.keySet()){
String errorMsg = counter + '.' + ' ' + pscId + ' - ' + pscFailedErrorMap.get(pscId);
logDetails += errorMsg + '\n';
counter++;
}
logDetails += '\n';
}

if(!pscItemFailedErrorMap.isEmpty()){
Integer counter = 1;
logDetails += 'Please find the below list of Product Service Campaign Items which got failed while updating the delivery method: \n';
for(String pscItemId : pscItemFailedErrorMap.keySet()){
String errorMsg = counter + '.' + ' ' + pscItemId + ' - ' + pscItemFailedErrorMap.get(pscItemId);
logDetails += errorMsg + '\n';
counter++;
}
logDetails += '\n';
}

String curentDateTime = system.now().format('MM/dd/yyyy', UserInfo.getTimeZone().getID());
attachment = new Messaging.EmailFileAttachment();
string flename= 'Attachment' + '_' + curentDateTime +'.txt';
attachment.setFileName(flename);
attachment.setBody(Blob.valueOf(logDetails));
}

String emailBody = 'The process to update the delivey method on Product Service Campaigns and Product Service Campaign Items have been completed.\n\n';

emailBody += 'Total Product Service Campaigns processed : '+ String.valueOf(totalPscCount) + '\n';
emailBody += 'Total Product Service Campaigns updated successfully : '+ String.valueOf(totalPscCount - pscFailedErrorMap.size()) + '\n';
emailBody += 'Total Product Service Campaigns Failed : '+ String.valueOf(pscFailedErrorMap.size()) + '\n\n';

emailBody += 'Total Product Service Campaign Items processed : '+ String.valueOf(pscItemList.size()) + '\n';
emailBody += 'Total Product Service Campaigns Items updated successfully : '+ String.valueOf(pscItemList.size() - pscItemFailedErrorMap.size()) + '\n';
emailBody += 'Total Product Service Campaigns Items Failed : '+ String.valueOf(pscItemFailedErrorMap.size()) + '\n\n';

if(isRerunRequired){
emailBody += '\n\n' + 'NOTE: There are few more records left for the update, Please re-run the script again.';
}


Messaging.SingleEmailMessage email = new Messaging.SingleEmailMessage();
email.setSubject('Delivery Method Update Process Run');
email.setSaveAsActivity(false);
email.setPlainTextBody(emailBody);
email.setSenderDisplayName('ServiceMax');
email.setUseSignature(false);
email.setToAddresses(emailAddressList);
if(attachment != null){
email.setFileAttachments( new Messaging.EmailFileAttachment[] {attachment} );
}

if(!emailAddressList.isEmpty()){
Messaging.SendEmailResult[] result = Messaging.sendEmail(new Messaging.SingleEmailMessage[] { email });
if(!result[0].isSuccess()){
Messaging.SendEmailError[] error = result[0].getErrors();
System.debug( LoggingLevel.DEBUG, 'There was a problem processing the email. Please contact your system administrator. ' + error[0].getMessage() );
}
else{
System.debug( LoggingLevel.DEBUG, 'sendEmail() - Email sent successfully' );
}
}

Was this helpful?