Ssquad54
Ssquad54

Reputation: 11

Suitescript ClientScript Error JS_EXCEPTION - TypeError: Cannot read property 'name' of undefined


Sorry first, my english is not good at all,
I want to create a Suitescript 2.0 to validate the customer credit limit when user want to save the item fulffilment.
if the total customer balance and amount of item to be fulfill is exceeded the customer credit limit, there will have a confirmation dialog to ask user want to submit for fulfillment approval or no.
if user click "Yes", the script update approval reason, approval status, quantity to approve, and check the need approval to fulfillment checkbox field on the sales order record and then redirect user to Sales order record.
and when the script save the sales order record, I get error "JS_EXCEPTION - TypeError: Cannot read property 'name' of undefined."
can anyone tell me which line is cause the error or how to solved my issue,
Thank you.
Here is my script :

 *@NApiVersion 2.x
 *@NScriptType ClientScript
 */
define(['N/record', 'N/currentRecord', 'N/search'], function (record, currentRecord, search) {
    function saveRecord(context) {
        var currentRecord = context.currentRecord;

        var isDynamic = currentRecord.isDynamic;
        log.debug({
            title: 'isDynamic',
            details: isDynamic
        });

        var soId = currentRecord.getValue({
            fieldId: 'createdfrom'
        });

        var fromRecord = search.lookupFields({ // Get Record Type from Createfrom field
            type: search.Type.TRANSACTION,
            id: soId,
            columns: 'recordtype',
        });
        log.debug("createdfrom Type", fromRecord);

        if (fromRecord.recordtype != 'salesorder')
            return;

        else if (fromRecord.recordtype == 'salesorder') {
            var soRecord = record.load({
                type: record.Type.SALES_ORDER,
                id: soId,
                isDynamic: true
            });

            var pymtMethod = soRecord.getValue({
                fieldId: 'custbody_bmpt_metode_pembayaran'
            });
            log.debug('Payment Method', pymtMethod);

            if (pymtMethod == 3 || pymtMethod == 4) {
                var creditLimit = soRecord.getValue({
                    fieldId: 'credlim'
                });
                log.debug('creditLimit', creditLimit);

                var balance = soRecord.getValue({
                    fieldId: 'balance'
                });
                log.debug('balance', balance);

                var remainingBalance = (creditLimit * 1.15) - balance;
                log.debug({
                    title: "Customer Remaining Balance",
                    details: remainingBalance
                });

                var itemCount = currentRecord.getLineCount({
                    sublistId: 'item'
                });
                log.debug({
                    title: "itemCount",
                    details: itemCount
                });

                var soItemCount = soRecord.getLineCount({
                    sublistId: 'item'
                });
                log.debug({
                    title: "soItemCount",
                    details: soItemCount
                });

                var totalAmount = 0;
                var amount = 0;
                //get item Detail per Line
                for (var i = 0; i < itemCount; i++) {
                    var fulfillCheck = currentRecord.getSublistValue({
                        sublistId: 'item',
                        fieldId: 'itemreceive',
                        line: i
                    });
                    log.debug({
                        title: "fulfillCheck" + [i],
                        details: fulfillCheck
                    });
                    // validate if fulfill field is checked
                    if (fulfillCheck == true) {
                        var ifItem = currentRecord.getSublistValue({
                            sublistId: 'item',
                            fieldId: 'item',
                            line: i
                        });
                        log.debug({
                            title: "ifItem",
                            details: ifItem
                        });

                        var itemQty = currentRecord.getSublistValue({
                            sublistId: 'item',
                            fieldId: 'quantity',
                            line: i
                        });
                        log.debug({
                            title: "itemQty",
                            details: itemQty
                        });

                        var ifItemRate = currentRecord.getSublistValue({
                            sublistId: 'item',
                            fieldId: 'itemunitprice',
                            line: i
                        });
                        log.debug({
                            title: "ifItemRate",
                            details: ifItemRate
                        });

                        amount = itemQty * ifItemRate;
                        log.debug({
                            title: "amount",
                            details: amount
                        });
                    }
                    totalAmount += amount;
                    log.debug({
                        title: 'Amount Total',
                        details: totalAmount
                    });
                }

                var currencySymbol = {
                    style: "currency",
                    currency: "IDR"
                };
                var totalBalance = balance + totalAmount;
                log.debug({
                    title: 'totalBalance',
                    details: totalBalance
                });

                var reason = soRecord.getValue({
                    fieldId: 'custbody_approval_reason'
                });

                if (totalAmount > remainingBalance) {
                    // confirmation dialog
                    var confirmation = confirm('Total Customer Balance ' + totalBalance.toLocaleString("id-ID", currencySymbol) +
                        '\nhas Exceeded Customer Credit Limit of ' + creditLimit.toLocaleString("id-ID", currencySymbol) +
                        '\nDo You Want Submit Approval ?');

                    log.debug({
                        title: "confirmation",
                        details: confirmation
                    });

                    if (confirmation) {
                        if (reason == "" || reason == null) {
                            var input_reason = prompt('Please enter approval reason');

                            // set Sales Order Record 'approval reason' field Value . 
                            var soUpdate = record.submitFields({
                                type: record.Type.SALES_ORDER,
                                id: soId,
                                values: {
                                    'custbody_approval_reason': input_reason,
                                    'custbody_approval_status_so': 1
                                }
                            });

                            log.debug({
                                title: 'soUpdate',
                                details: soUpdate
                            });
                        }

                        //set approval status to 1 - pending approval
                        var approvalSO = record.submitFields({
                            type: record.Type.SALES_ORDER,
                            id: soId,
                            values: {
                                'custbody_approval_status_so': 1
                            }
                        });

                        for (var u = 0; u < itemCount; u++) {
                            var fulfill = currentRecord.getSublistValue({
                                sublistId: 'item',
                                fieldId: 'itemreceive',
                                line: u
                            });
                            log.debug({
                                title: "fulfill" + [u],
                                details: fulfill
                            });
                            // validate if fulfill field is checked
                            if (fulfill == true) {
                                var currentItem = currentRecord.getSublistValue({
                                    sublistId: 'item',
                                    fieldId: 'item',
                                    line: u
                                });
                                log.debug({
                                    title: "currentItem",
                                    details: currentItem
                                });

                                var currentQty = currentRecord.getSublistValue({
                                    sublistId: 'item',
                                    fieldId: 'quantity',
                                    line: u
                                });
                                log.debug({
                                    title: "currentQty",
                                    details: currentQty
                                });

                                // get SO item information. 
                                for (var so = 0; so < soItemCount; so++) {
                                    var soLine = soRecord.selectLine({
                                        sublistId: 'item',
                                        line: so
                                    });

                                    var SOItem = soRecord.getCurrentSublistValue({
                                        sublistId: 'item',
                                        fieldId: 'item'
                                    });
                                    log.debug({
                                        title: 'SOItem ' + [so],
                                        details: SOItem
                                    });

                                    if (currentItem == SOItem) {
                                        soRecord.setCurrentSublistValue({
                                            sublistId: 'item',
                                            fieldId: 'custcol_need_approval',
                                            value: true
                                        });

                                        soRecord.setCurrentSublistValue({
                                            sublistId: 'item',
                                            fieldId: 'custcol_fulfill_qty',
                                            value: currentQty
                                        });
                                    }
                                    soRecord.commitLine({
                                        sublistId: 'item'
                                    });
                                }
                            }
                        }

                        soRecord.save();

                        //open Sales Order Record at current Windows.
                        window.open('https://xxxx.app.netsuite.com/app/accounting/transactions/salesord.nl?id=' + soId, +'_self');
                    } else {
                        alert('Silahkan Perbaiki dan Submit Kembali !!');
                    }
                    return false;
                }
            }
        }
        return true;
    }
    return {
        saveRecord: saveRecord
    };
}); ```

Upvotes: 0

Views: 1724

Answers (2)

Martha
Martha

Reputation: 764

I don't see any issue specifically in your provided code that would directly cause the issue. It is likely an error that is being thrown by a script/workflow that is triggered by this client script.

Option 1: Check script deployment records, there is a Context Filtering tab that will let you set what context the script will execute in.

Option 2: N/runtime module has runtime.executionContext and runtime.ContextType. Depending on the purpose of other scripts you have, you can utilize this at the beginning of scripts to limit when they are triggered.

Option 3: When you use the record.submitFields(options) method, try setting the optional parameter "enablesourcing". Also decide if you want to use the "ignoreMandatoryFields" parameter.

So instead of:

var soUpdate = record.submitFields({
 type: record.Type.SALES_ORDER,
 id: soId,
 values: {
  'custbody_approval_reason': input_reason,
  'custbody_approval_status_so': 1
 }
});

try:

var id = record.submitFields({
 type: record.Type.SALES_ORDER,
 id: 1,
 values: {
  memo: 'ABC'
 },
 options: {
  enableSourcing: false //default is true
 }
});

There was a parameter in older documentation named "fireSlavingSync" that was available in client scripts when using methods like record.setValue(options), or record.setSublistValue(options). I've used it successfully in the past to not trigger other programmed actions to occur.

Upvotes: 0

Addy
Addy

Reputation: 77

Can you check if the error is form any other script deployed on Sales order, as when you try to save the record with client script all other scripts deployed will execute.

Try undeploying other scripts and test once to rule out error

Upvotes: 0

Related Questions