<@UH9SBRX7E> <@U29QCJQF3> <@U2CQPCRFT> <@U898NF87J...
# suitescript
s
@Nikola Gavric @michoel @aaz @Nelli
n
Seems like you've missed a closing bracket somwhere.. trying to find it
s
i check , got no hint to resolve
n
Yeah I'm trying to find it too
Ok so here we go, try this @shadman Ashrafi :
Copy code
/**
 * @NApiVersion 2.x
 * @NScriptType UserEventScript
 * @NModuleScope SameAccount
 */
define(['N/record', 'N/search', 'N/runtime', 'N/ui/serverWidget', 'N/redirect', 'N/error'],

    function (record, search, runtime, serverWidget, redirect, error) {

        /**
         * Function definition to be triggered before record is loaded.
         *
         * @param {Object} scriptContext
         * @param {Record} scriptContext.newRecord - New record
         * @param {Record} scriptContext.oldRecord - Old record
         * @param {string} scriptContext.type - Trigger type
         * @Since 2015.2
         */
        function afterSubmit(scriptContext) {

            var test = "123";
            log.debug(test);

            if (scriptContext.type === "delete")
                return;

            log.debug(" scriptContext = ", scriptContext);
            var newRecord = scriptContext.newRecord;
            log.debug(" newRecord = ", newRecord);
            var jsonObj = JSON.stringify(scriptContext);
            log.debug("scriptContext = ", jsonObj);

            var recordId = newRecord.id;
            var recordType = newRecord.type;
            log.debug(" recordId = ", recordId);
            log.debug(" recordType = ", recordType);
            var currentRecord = record.load({
                type: recordType,
                id: recordId
            });

            log.debug(" currentRecord = ", currentRecord);
            var jsonObj = JSON.stringify(currentRecord);
            log.debug("currentRecord = ", jsonObj);

            var checkFormVal = currentRecord.getValue({
                fieldId: 'customform'
            });
            log.debug(" checkFormVal = ", checkFormVal);

            var docNo = currentRecord.getValue({
                fieldId: 'tranid'
            });
            log.debug(" docNo = ", docNo);

            var sourceVal = currentRecord.getValue({
                fieldId: 'custbody34'
            });
            log.debug(" sourceVal = ", sourceVal);

            if (sourceVal === 13) {

                currentRecord.setValue({
                    fieldId: 'externalid',
                    value: docNo
                });

                var invId = currentRecord.save({});
                log.debug(" invId = ", invId);
            }

            var r = record.load({
                type: recordType,
                id: recordId,
                isDynamic: true
            });
            log.debug(" invObj  = ", r);


            var itemCount = r.getLineCount({
                sublistId: 'item'
            });


            for (var j = 0; j < itemCount; j++) {
                r.selectLine({
                    sublistId: 'item',
                    line: j
                });

                var cf0Val = r.getCurrentSublistValue({
                    sublistId: 'item',
                    fieldId: 'custcol_tax_percent'
                });
                if (cf0Val === null || cf0Val === "") {
                    cf0Val = 0;
                    cf0Val = parseFloat(cf0Val);
                } else {
                    cf0Val = parseFloat(cf0Val);
                    log.debug(" cf0Val = ", cf0Val);
                    var cf0Val1 = cf0Val + parseFloat(100);
                    log.debug(" cf0Val1 = ", cf0Val1);
                }

                var cf1Val = r.getCurrentSublistValue({
                    sublistId: 'item',
                    fieldId: 'custcol_purchase_val'
                });
                log.debug(" cf1Val = ", cf1Val);

                var cf2Val = r.getCurrentSublistValue({
                    sublistId: 'item',
                    fieldId: 'custcol_sale_val'
                });
                log.debug(" cf2Val = ", cf2Val);

                if (cf1Val == null && cf2Val == null || cf1Val == "" && cf2Val == "" || cf1Val == null && cf2Val == "" || cf1Val == "" && cf2Val == null) {
                } else {
                    if (cf1Val == null || cf1Val == "") {
                        cf1Val = 0;
                        cf1Val = parseFloat(cf1Val);
                    }
                    if (cf2Val == null || cf2Val == "") {
                        cf2Val = 0;
                        cf2Val = parseFloat(cf2Val);
                    }
                    cf1Val = parseFloat(cf1Val);
                    cf2Val = parseFloat(cf2Val);

                    var cf3Val = cf2Val - cf1Val;
                    cf3Val = parseFloat(cf3Val);

                    r.setCurrentSublistValue({
                        sublistId: 'item',
                        fieldId: 'custcol_sale_purchase',
                        value: cf3Val,
                        ignoreFieldChange: true
                    });

                    var cf4Val = (cf3Val * 100) / cf0Val1;
                    cf4Val = parseFloat(cf4Val);

                    r.setCurrentSublistValue({
                        sublistId: 'item',
                        fieldId: 'custcol_cfformula',
                        value: cf4Val,
                        ignoreFieldChange: true
                    });

                    var amtcfVal = cf4Val + cf1Val;
                    amtcfVal = parseFloat(amtcfVal);

                    r.setCurrentSublistValue({
                        sublistId: 'item',
                        fieldId: 'amount',
                        value: amtcfVal,
                        ignoreFieldChange: true
                    });

                    r.commitLine({
                        sublistId: 'item'
                    });

                }
            }

            r.setValue({fieldId: 'taxdetailsoverride', value: true});
            var taxCount = r.getLineCount({
                sublistId: 'taxdetails'
            });


            for (var i = 0; i < taxCount; i++) {

                r.selectLine({
                    sublistId: 'taxdetails',
                    line: i
                });
                var taxRefNo = r.getCurrentSublistValue({
                    sublistId: 'taxdetails',
                    fieldId: 'taxdetailsreference'
                });
                log.debug(" taxRefNo = ", taxRefNo);
                var itemLineNo = r.findSublistLineWithValue({
                    sublistId: 'item',
                    fieldId: 'taxdetailsreference',
                    value: taxRefNo
                });

                log.debug(" itemLineNo = ", itemLineNo);
                var taxBasis = 0;
                var taxAmtVal = 0;
                if (itemLineNo !== -1) {
                    r.selectLine({
                        sublistId: 'item',
                        line: itemLineNo
                    });

                    var lineCf2Val = r.getCurrentSublistValue({
                        sublistId: 'item',
                        fieldId: 'custcol_sale_val'
                    });
                    log.debug(" lineCf2Val = ", lineCf2Val);
                    var lineCf4Val = r.getCurrentSublistValue({
                        sublistId: 'item',
                        fieldId: 'custcol_cfformula'
                    });
                    log.debug(" lineCf4Val = ", lineCf4Val);
                    var lineAmtVal = r.getCurrentSublistValue({
                        sublistId: 'item',
                        fieldId: 'amount'
                    });
                    log.debug(" lineAmtVal = ", lineAmtVal);

                    taxBasis = lineCf4Val;
                    taxBasis = parseFloat(taxBasis);

                    taxAmtVal = lineCf2Val - lineAmtVal;
                    taxAmtVal = parseFloat(taxAmtVal);

                    r.selectLine({
                        sublistId: 'taxdetails',
                        line: i
                    });

                    r.setCurrentSublistValue({
                        sublistId: 'taxdetails',
                        fieldId: 'taxbasis',
                        value: taxBasis,
                        ignoreFieldChange: true
                    });
                    r.setCurrentSublistValue({
                        sublistId: 'taxdetails',
                        fieldId: 'taxbasis',
                        value: taxAmtVal,
                        ignoreFieldChange: true
                    });

                    r.commitLine({
                        sublistId: 'taxdetails'
                    });

                }

            }

            r.save({
                enableSourcing: true,
                ignoreMandatoryFields: true
            });


        }

        return {
            afterSubmit: afterSubmit
        };

    });
message has been deleted
This is without the commented part that was in your code
Try it out
s
Thanks @Nikola Gavric now its working
n
@shadman Ashrafi nice. no problem, are you using an IDE? There were a couple of errors that should have been highlighted in an IDE
s
nope i am using notepad++ for now.
how about atom ide will that help me highlighting those errors
n
WIth the typescript/javascript language package, yes, but if you're gonna develope in NS a lot, and especially if you plan to use the SDF in your daily work to deploy etc, you should really get WebStorm :)
💯 1
s
Whatever editor you use, I highly recommend using a linter, like ESLint, to help find static code issues, and I am also a big fan of Prettier, which an opinionated code formatter. With the combination of those two, it is much easier to find and fix un-balanced brackets or parentheses, not to mention they help keep your code tidy and free of certain bugs.
👍 2
r
Use VScode(free) rather than Notepad++. and if you can buy it webstorm(paid) is the way to go.
👍 1