You are here:
XLIBatchValidationNotification
Send an email message with the validation results to the user who executed the batch validation process. It also updates the parent objects—Opportunity, Order, or Quote—on the validation results.
Interface
VlocityOpenInterface
Parameters
notify
Returns Validation_Result,
XLIValidationResults that the system can use to notify. The
response is a string to indicate if the notification was processed or not.
Sample Code
public with sharing class XLIBatchValidationNotification implements VlocityOpenInterface {
public Boolean invokeMethod(String methodName, Map < String, Object > input, Map < String, Object > output, Map < String, Object > options) {
if (methodName.equals('notify')) {
notify(input, output, options);
return true;
}
return false;
}
private void notify(Map < String, Object > input, Map < String, Object > output, Map < String, Object > options) {
Logger.dbg('XLIBatchValidationNotification.notify():' + input);
XLIValidationResults validationResults =
(XLIValidationResults) input.get(XLIValidationResults.KEY_VALIDATION_RESULTS);
// if nothing failed, then no need to update line item status
if (validationResults == null || validationResults.isEmpty()) {
output.put('response', 'Nothing was validated.');
return;
}
Set < Id > parentIds = validationResults.getParentIds();
for (Id parentId: parentIds) {
XLIItemValidationResult itemValidationResult = (XLIItemValidationResult) validationResults.getXLIItemValidationResult(parentId);
// Update Parent item
updateParentItem(parentId, itemValidationResult);
}
emailNotification(validationResults);
output.put('response', 'Process notification successfully.');
}
private void updateParentItem(String parentId, XLIItemValidationResult itemValidationResult) {
if (parentId == null || itemValidationResult == null) {
return;
}
Logger.dbg('XLIBatchValidationNotification.updateParentItem().parentId=' + parentId);
Logger.dbg('XLIBatchValidationNotification.updateParentItem().itemValidationResult=' + itemValidationResult);
Id sObjectId = Id.valueOf(parentId);
SObjectType parentSObjectType = sObjectId.getSObjectType();
Logger.dbg('XLIBatchValidationNotification.updateParentItem().parentSObjectType=' + parentSObjectType);
if (Order.sObjectType == parentSObjectType) {
Order validatedOrder = new Order(
Id = sObjectId,
ValidationDate__c = Datetime.now(),
ValidationStatus__c = (itemValidationResult.isQualified() ? 'Passed' : 'Failed'),
ValidationMessage__c = itemValidationResult.getErrorMessage()
);
Logger.dbg('XLIBatchValidationNotification.updateParentItem().validatedOrder=' + validatedOrder);
update validatedOrder;
} else if (Quote.sObjectType == parentSObjectType) {
Quote validatedQuote = new Quote(
Id = sObjectId,
ValidationDate__c = Datetime.now(),
ValidationStatus__c = (itemValidationResult.isQualified() ? 'Passed' : 'Failed'),
ValidationMessage__c = itemValidationResult.getErrorMessage()
);
Logger.dbg('XLIBatchValidationNotification.updateParentItem().validatedQuote=' + validatedQuote);
update validatedQuote;
} else if (Opportunity.sObjectType == parentSObjectType) {
Opportunity validatedOpportunity = new Opportunity(
Id = sObjectId,
ValidationDate__c = Datetime.now(),
ValidationStatus__c = (itemValidationResult.isQualified() ? 'Passed' : 'Failed'),
ValidationMessage__c = itemValidationResult.getErrorMessage()
);
Logger.dbg('XLIBatchValidationNotification.updateParentItem().validatedOpportunity=' + validatedOpportunity);
update validatedOpportunity;
}
}
private void emailNotification(XLIValidationResults validationResults) {
if (validationResults == null) {
return;
}
XLIBatchValidationSetting_c[] validationSettings = [SELECT Id, Name, NotificationEmailc FROM XLIBatchValidationSettingc WHERE NotificationEmail_c != null];
Logger.dbg('XLIBatchValidationNotification.emailNotification().validationSettings=' + validationSettings);
if (validationSettings == null || validationSettings.isEmpty()) {
return;
}
String[] toAddresses = new List < String > ();
for (XLIBatchValidationSetting__c setting: validationSettings) {
if (setting.NotificationEmail__c == null) {
continue;
}
toAddresses.add(setting.NotificationEmail__c);
}
Logger.dbg('XLIBatchValidationNotification.emailNotification().toAddresses=' + toAddresses);
if (toAddresses.isEmpty()) {
return;
}
List < XLIItemValidationResult > passedItemValidationResults = validationResults.getXLIItemValidationResults(true);
List < XLIItemValidationResult > failedItemValidationResults = validationResults.getXLIItemValidationResults(false);
String messageBody = 'The batch validation job processed ' + validationResults.size() + ' item.\n';
messageBody += passedItemValidationResults.size() + ' items passed validaiton.\n';
messageBody += failedItemValidationResults.size() + ' items failed validaiton.\n';
Messaging.SingleEmailMessage mail = new Messaging.SingleEmailMessage();
mail.setToAddresses(toAddresses);
mail.setSubject('XLI Batch Validation Completed');
mail.setPlainTextBody(messageBody);
Messaging.sendEmail(new Messaging.SingleEmailMessage[] {
mail
});
}
}
Related Implementations
XLIBatchValidationConfiguration
