SuiteCommerce Mandatory Field Validation for Signature-Controlled Items

This article provides a complete explanation and reference implementation for adding mandatory field validation during checkout in SuiteCommerce Advanced (SCA) when the order contains Signature-Controlled items. The customization ensures that the order cannot be submitted unless required prescriber information is provided.

Overview

Certain item classes — such as “Controlled : Signature Controlled” — require additional information from the customer before an order can be processed.

This solution uses:

  • Application.on('before:LiveOrder.submit') event
  • A custom validation function added to the LiveOrderModel
  • Server‑side validation through SuiteCommerce SSP

The logic checks:

  1. Whether the cart contains a Signature‑Controlled item.
  2. If yes, validates mandatory fields:
  • Prescriber Name (custbody_prescriber_name)
  • Prescriber Email (custbody_prescriber_email)
  • Licensing Number (custbody_licensing_number)
  1. Blocks order submission if any field is missing or invalid.

Code Implementation

Below is the complete working code snippet to place inside your SSP / SCA backend extension:

Application.on('before:LiveOrder.submit', function beforeLiveOrderSubmit(LiveOrderModel) {
const validationDeferred = LiveOrderModel.validatemandatory();
nlapiLogExecution('ERROR', 'Success', 'INSIDE APPLICATIONsuccess', validationDeferred);
});


_.extend(LiveOrderModel, {
validatemandatory: function validatemandatory() {
nlapiLogExecution('ERROR', 'INSIDE liveordermodel', JSON.stringify(this, null, 2));


// Fetch item classes to identify Signature-Controlled items
const fieldValues = this.getFieldValues({ keys: [], items: null });
nlapiLogExecution('ERROR', 'Field Values:', JSON.stringify(fieldValues, null, 2));


var hasSignatureControlledItem = false;
_.each(ModelsInit.order.getItems(['class']), function (item) {
var itemClass = (item.class || '').trim();
if (itemClass === 'Controlled : Signature Controlled') {
hasSignatureControlledItem = true;
}
nlapiLogExecution('ERROR', 'item class:', JSON.stringify(item));
});


nlapiLogExecution('ERROR', 'hasSignatureControlledItem:', hasSignatureControlledItem);


// Skip validation if no signature-controlled items exist
if (!hasSignatureControlledItem) {
nlapiLogExecution('ERROR', 'No Signature Controlled Item',
'No signature-controlled items found, skipping mandatory field validation.');
return null;
}


// Fetch order custom fields
var order_fields = ModelsInit.order.getCustomFieldValues();
var prescriberName = (_.find(order_fields, { name: 'custbody_prescriber_name' }) || {}).value || '';
var prescriberEmail = (_.find(order_fields, { name: 'custbody_prescriber_email' }) || {}).value || '';
var licenseNumber = (_.find(order_fields, { name: 'custbody_licensing_number' }) || {}).value || '';


// Validate required fields
const missingFields = [];
if (!prescriberName) missingFields.push('Prescriber Name');
if (!prescriberEmail) missingFields.push('Prescriber Email');
else if (!/^[^s@]+@[^s@]+.[^s@]+$/.test(prescriberEmail)) missingFields.push('Valid Email');
if (!licenseNumber) missingFields.push('License Number');


// If validation fails → block checkout submission
if (missingFields.length > 0) {
const errorMessage = 'The following fields are required before placing the order: ' + missingFields.join(', ');
nlapiLogExecution('ERROR', 'Missing Mandatory Fields', missingFields.join(', '));


var error = new Error('Validation failed!');
error.name = 'ERROR';
error.message = errorMessage;
throw error; // Block the submission
}
}
});

Leave a comment

Your email address will not be published. Required fields are marked *