To Change the sales order form based on the Domain.

We can create an extension to check the domain. For this, we have created a configuration record and a transaction body field. If the domain matches as expected, we will add the corresponding form to the sales order during creation. Using a workflow, we can then set a condition: if the transaction body field contains a value, the online case will be updated accordingly to match the domain-specific requirement.

define('JJ.RCPForm.RCPForm', ['SC.Configuration','LiveOrder.Model'], function (Configuration,LiveOrderModel) {
    'use strict';


    function toNumberOrNull(raw) {
        var n = Number(raw);
        return isNaN(n) ? null : n;
    }


    return {
        mountToApp: function (container) {
            var cart = container.getComponent('Cart');
            if (!cart || typeof cart.setTransactionBodyField !== 'function') {
                console.warn('[RCPForm] Cart component or setTransactionBodyField not available');
                return;
            }
            cart.on('beforeSubmit', function () {
                var order = LiveOrderModel.getInstance();
                var payments = order.get('paymentmethods') || [];


                var fieldId = 'custbody_jj_rcp_form';


                // get your configured option (should be the INTERNAL ID of the select option)
                var selected = null;
                if (payments && payments.models && payments.models.length > 0) {
                    selected = payments.models[0].attributes.creditcard || null;
                }
                 var raw;
                if (selected) {
                    // If credit card → use CCID from configuration
                    raw = Configuration.get('RCPForm.ccid');
                } else {
                    // Otherwise → use default RCPForm.id
                    raw = Configuration.get('RCPForm.id');
                }
                var internalId = toNumberOrNull(raw);
                if (internalId == null) {
                    return; // avoid INVALID_PARAM
                }


                // 1) Try sending as STRING (most reliable for select fields on many builds)
                var payloadString = { fieldId: fieldId, type: 'string', value: String(internalId) };
                return cart.setTransactionBodyField(payloadString)
                    .catch(function (e1) {
                        // 2) Fallback: send as NUMBER
                        var payloadNumber = { fieldId: fieldId, type: 'number', value: internalId };
                        return cart.setTransactionBodyField(payloadNumber).catch(function (e2) {
                            // Optionally rethrow to block submit:
                            // throw e2;
                        });
                    });
            });
        }
    };
});


Leave a comment

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