Introduction
Reconciliation (Blog 79) helps identify data mismatches β but fixing them manually can still take hours.
SuiteScript automation can repair these issues automatically and safely, turning your integrations into self-maintaining systems.
In this blog, weβll cover:
- How to detect fixable errors.
- Auto-correction logic using SuiteScript.
- Rollback and safety controls.
- Validation after correction.
π― What Is Auto-Correction Automation?
Itβs a scheduled or Map/Reduce script that:
- Reads mismatched records from a reconciliation log.
- Validates that a fix is safe.
- Performs the correction (update, recreate, or delete).
- Logs the change for audit and notifies admins.
βοΈ 1. Custom Record for Tracking Fixes
Create: customrecord_auto_fix_log
Field ID | Label | Type |
---|---|---|
custrecord_error_type | Error Type | List/Text |
custrecord_fixed_record | Fixed Record ID | Text |
custrecord_action | Action Taken | Text |
custrecord_fix_status | Status | List (Pending/Success/Failed) |
custrecord_message | Notes | Long Text |
custrecord_fix_date | Date | DateTime |
β Keeps every automated fix auditable.
π§© 2. Sample Map/Reduce Script β Auto-Fix Mismatched Totals
/**
* @NApiVersion 2.1
* @NScriptType MapReduceScript
*/
define(['N/search','N/record','N/runtime','N/log'], (search, record, runtime, log) => {
const getInputData = () => search.create({
type:'customrecord_reconciliation_log',
filters:[['custrecord_status','is','UNMATCHED'], 'AND', ['custrecord_source_system','is','Shopify']],
columns:['custrecord_diff','custrecord_extid','custrecord_nsid']
});
const map = (ctx) => {
const data = JSON.parse(ctx.value);
const diff = JSON.parse(data.values.custrecord_diff);
try {
if (diff.type === 'Amount Mismatch') {
const so = record.load({ type: record.Type.SALES_ORDER, id: data.values.custrecord_nsid });
so.setValue('custbody_total_verified', diff.shopify);
so.save();
record.create({ type:'customrecord_auto_fix_log' })
.setValue('custrecord_error_type','Amount Mismatch')
.setValue('custrecord_fixed_record', data.values.custrecord_nsid)
.setValue('custrecord_action','Updated total from Shopify')
.setValue('custrecord_fix_status','Success')
.save();
}
} catch(e){
log.error('Auto Fix Failed', e);
}
};
return { getInputData, map };
});
β
Automatically updates mismatched totals using Shopify data.
β
Logs every action in customrecord_auto_fix_log
.
π§± 3. Other Fix Types
Error Type | Action |
---|---|
Missing Record in NetSuite | Recreate from external API payload |
Duplicate Entry | Identify duplicates via unique external ID and delete extra |
Field Mismatch | Update fields like tax, currency, or email |
Failed API Sync | Retry call and log new attempt |
Outdated Status | Update record state (e.g., mark βShippedβ) |
π§© 4. Rollback Safety
Add rollback control to prevent unwanted overwrites:
if (so.getValue('custbody_total_verified') !== diff.ns) {
throw Error('Data changed since reconciliation. Skipping auto-fix.');
}
β Prevents overwriting updated or manually fixed records.
π§ 5. Post-Fix Notification
After all fixes:
email.send({
author: -5,
recipients: ['integration.admin@company.com'],
subject: 'Auto-Fix Summary',
body: `Completed ${successCount} fixes. ${failCount} failed.`
});
You can also post results to Slack (reuse from Blog 75).
π 6. Validation After Correction
Re-run the reconciliation search or test framework (Blog 78) automatically after fixes:
task.create({
taskType: task.TaskType.SCHEDULED_SCRIPT,
scriptId: 'customscript_reconcile_validation'
}).submit();
β Confirms fixes actually resolved discrepancies.
π§ 7. Governance & Performance
- Use Map/Reduce for large fix batches.
- Yield when usage < 200 units.
- Add checkpoint logging every 100 records.
- Skip already fixed entries.
π 8. Best Practices
Practice | Benefit |
---|---|
Keep fix logic modular | Easier maintenance |
Log every change | Full audit trail |
Validate before update | Prevent data loss |
Combine with dashboards | Real-time tracking |
Run off-peak hours | Avoid performance impact |
β Example Output
Type | Action | Status | Message |
---|---|---|---|
Amount Mismatch | Updated total from Shopify | β Success | SO #1532 corrected |
Missing Record | Created new Sales Order | β Success | Imported from API |
Duplicate Entry | Removed duplicate Invoice | β Warning | Skipped manual edit |
Conclusion
Automated data correction closes the loop of integration quality management.
Your system now not only detects errors but repairs them safely and transparently β cutting manual reconciliation time to minutes.
When combined with your existing monitoring and QA framework, it creates a self-healing integration ecosystem inside NetSuite.
Discover more from The NetSuite Pro
Subscribe to get the latest posts sent to your email.
Leave a Reply