The BatchPullSecureEmailStatus job is responsible for retrieving and updating the delivery status of Secure Emails sent through Cloud Maven’s Secure Email app (powered by Paubox).
Batch Job: BatchPullSecureEmailStatus
-
Salesforce Batch Apex class designed to query Secure Email messages and update their delivery status by making external API callouts.
It supports automated polling and rescheduling to ensure that message statuses are consistently refreshed.
How It Works
-
Fetch Records
QueriesSecure Email Messagerecords that meet all the following conditions:
-
Stored in Label → System.Label.GetBatchStatusQuery
-
Appends the where clause from Custom Setting-->
BatchStatus_WhereClause-
CreatedDate >= LAST_N_DAYS:2
-
cmsecureemail__Email_Status__c = 'Processing'
-
cmsecureemail__Email_Status__c = 'Delivered' and (cmsecureemail__Opened_Status__c = '' OR cmsecureemail__Delivery_Status__c = 'processing')
-
CreatedDate >= LAST_N_DAYS:2 and (cmsecureemail__Email_Status__c = 'Processing' OR (cmsecureemail__Email_Status__c = 'Delivered' and (cmsecureemail__Opened_Status__c = '' OR cmsecureemail__Delivery_Status__c = 'processing') ))
The above will pick up all the Secure Email Message records which are either stuck in Processing or are Delivered but Delivery status is still processing.
-
Key logic
-
Loops through
Secure_EmailMessage__crecords to retrieve the latest message delivery details from Paubox. -
Calls status API via:
-
WSSecureEmail.getSecureEmailStatus()
-
-
Key values returned by Paubox:
-
Delivery Status
-
Delivery Timestamp
-
Opened Status
-
Opened Time
-
Message ID
-
Any error returned from Paubox
-
This ensures Salesforce has the true, final delivery result for each email.
-
Check if the delivery status is in any of the Possible Error Status, then mark it as Failed.
Please don’t forget to create an entry for the Possible Error Status in the Custom Setting if it already doesn’t exists.
-
Login to the Salesforce Org
-
Go to Setup → Quick Find ‘Custom Settings’
-
Click Manage for Data Transfer Settings
-
Create New Entry for Possible_Error_Status
-
Set the value: soft bounced,soft bounced - mailbox full,hard bounced,Internal error. Please check back later.
-
Click Save,
-
Performs DML update via internal
data.modify()utility
-
Success Handling
If the capture is successful:-
✅
Email Status→ Updated to delivered -
Delivery Status of all Email Recipients will be updated to Delivered
-
In case the delivery status of any of the recipient is failed, the Email Status on the Secure Email Message Record be updated to Partially Delivered.
-
-
Failure Handling
If the capture fails:-
❌
Email Status→ Updated to Failed -
❌
Integration Sttaus(or error message field) → Populated with the error response
-
-
Next Scheduling
Schedules the next batch execution:
-
Reads time delay from
DataTransfer_Settings__c.NextScheduleBatchStatusTime -
Automatically schedules
ScheduleBatchPullSecureEmailStatus
Automation
-
This batch can be scheduled to run daily to automatically update the status of Secure Email Messages for each day.
ScheduleBatchPullSecureEmailStatus
Execution Script
👉 Run batch without filter (uses metadata-based query):
Database.executeBatch(new BatchPullSecureEmailStatus(), 1);
👉 Run batch with a custom SOQL query:
String sQuery = <custom query>;
Database.executeBatch(new BatchPullSecureEmailStatus(sQuery), 1);
Summary
-
Configure Custom Settings (query clause, retry interval, error statuses).
-
Use the provided BatchPullSecureEmailStatus class.
-
Schedule the batch to run hourly (or per your business requirement).
-
Monitor the batch job to ensure all emails are updated with the latest status.