Hey Can anyone help me with this requirement plz? ...
# suitescript
d
Hey Can anyone help me with this requirement plz? (User event script I want ) Need to show an error message when transaction going to save with the sub total amount greater than 100 bucks and also if transaction total amount is exceed the customer credit limit. Alert Messages : a. Confirm transaction amount greater than 100 bucks. b. Order amount exceeds credit limit.
b
user event is a pretty bad choice for those requirements
the record has already been submitted, there is no chance to make any changes, you can only throw an error and prevent the record from being saved
d
But my trainer told me to use user event script here
Can you help me with the code please, m' beginner just learning NS
b
first step is making a user event script
in this case, probably by implementing the beforeSubmit entry point
d
Yes i used beforeSubmit function
can you check my code please ?
Copy code
/**
 * @NApiVersion 2.1
 * @NScriptType UserEventScript
 * @NmoduleScope SameAccount
 */

define(['N/record'], function (record)  {
    function myBeforeSubmit(scriptContext) {
        var newRecord = scriptContext.newRecord;
        //check if subtotal is greater than 100
        var subtotal = newRecord.getValue({
            fieldId: 'subtotal',
        });

        if (subtotal > 100) {
           throw 'Confirm transaction amount greater than 100 bucks'; //throw the explicit error 
            log.debug({
                title:'transaction subtotal',
                details: 'transaction subtotal exceeds 100 bucks'
            });
            return false; // prevent the transaction from being saved
        }

        var customerId = newRecord.getValue({
            fieldId: 'entity'
        });
        var customerObj = record.load({ //lookup , credit limit column
            type: newRecord.Type.CUSTOMER,
            id: customerId
        });
        var customerCreditLimit = customerObj.getValue({
            fieldId: 'creditlimit'
        });
        var transactionTotal = newRecord.getValue({  // it should be transaction total or only total ? or customer balance ?
            fieldId: 'total'
        });
        if (transactionTotal > customerCreditLimit) {
            throw 'Order amount exceeds credit limit';
            log.debug({
                title:'order total',
                details: 'Transaction total ('+ transactionTotal+') exceeds credit limit('+ customerCreditLimit +')'
            });
            return false; //prevent the transactionfrom being saved
        }
        return true;
    }
        return {
        beforeSubmit: myBeforeSubmit
        };

    });
b
all the code related to the credit limit is mostly useless, its reimplementing functionality that already exists in netsuite
and in this case, probably implemented incorrectly, you arent usually interested if the transaction is larger than the credit limit
a credit limit usually goes into effect when the customer's balance plus the current transaction goes over the limit
Copy code
var subtotal = newRecord.getValue({
            fieldId: 'subtotal',
        });

        if (subtotal > 100) {
           throw 'Confirm transaction amount greater than 100 bucks'; //throw the explicit error 
            log.debug({
                title:'transaction subtotal',
                details: 'transaction subtotal exceeds 100 bucks'
            });
            return false; // prevent the transaction from being saved
        }
looks fine for the subtotal requirement
but
Copy code
log.debug({
                title:'transaction subtotal',
                details: 'transaction subtotal exceeds 100 bucks'
            });
            return false; // prevent the transaction from being saved
doesnt actually do anything
throwing an error stops those lines from running
although in this case, you are throwing a string, which is generally frowned upon
though will give a prettier error message for the user
d
like i am asking where do I write the code again or correct it ?
b
im not writing any code for you
you will have to go through what i wrote and see if it applies or doesnt
d
🥲okay, I will try , If still i face any error , can you help me out ?
b
ok