Guest Checkout: Disallowed if Email Address is already used by existing Web Customer Account

When a guest customer tries to checkout with an existing email we are not allowing them and throwing an error to the user to prevent duplicate customers with same email. It is an UE script deployed to lead records on BEFORE SUBMIT FUNCTION

/**
 * @NApiVersion 2.1
 * @NScriptType UserEventScript
 */
define(['N/error', 'N/log', 'N/record', 'N/search'],
    /**
 * @param{error} error
 * @param{log} log
 * @param{record} record
 * @param{search} search
 */
    (error, log, record, search) => {

        /**
         * Defines the function definition that is executed before record is submitted.
         * @param {Object} scriptContext
         * @param {Record} scriptContext.newRecord - New record
         * @param {Record} scriptContext.oldRecord - Old record
         * @param {string} scriptContext.type - Trigger type; use values from the context.UserEventType enum
         * @since 2015.2
         */
        const beforeSubmit = (scriptContext) => {
            let logTitle = 'beforeSubmit'

            try {
                let newRec = scriptContext.newRecord
                let type = scriptContext.type

                if (type !== scriptContext.UserEventType.CREATE) return

                // skip validation based on some conditions like this 
                if (newRec.getValue('custentity_req_unique_id')) return


                let email = newRec.getValue({
                    fieldId: 'email'
                })

                let mySearch = search.create({
                    type: search.Type.CUSTOMER,
                    columns: ['email'],
                    filters: ['email', search.Operator.IS, email]
                });

                let myResultSet = mySearch.run();

                let resultRange = myResultSet.getRange({
                    start: 0,
                    end: 1
                });


                if (resultRange.length > 0) {
                    let myCustomError = error.create({
                        name: 'ERR_WS_GUEST_REGISTRATION',
                        message: 'You already have an account, please log in to check out.',
                        notifyOff: true
                    })
                    throw myCustomError
                }

            } catch (error) {
                log.error(logTitle + ' ' + error.name, error.message)
                if (error.name === 'ERR_WS_GUEST_REGISTRATION') throw error
            }
        }

        return {beforeSubmit}

    });

Leave a comment

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