Hi Guys, I have written UserEventScript which is u...
# suitescript
m
Hi Guys, I have written UserEventScript which is updating a purchase order on Bill creation, at UI level it is working fine but when we are creating through webservice it is throwing below error
Copy code
{
  "type": "error.SuiteScriptError",
  "name": "USER_ERROR",
  "message": "This expense is already on a bill. Are you sure you want to edit it?",
  "stack": [
    "anonymous(N/serverRecordService)",
    "afterSubmit(/SuiteScripts/Octavius App/vendorBillToItemReceiptUE.js:62)"
  ],
  "cause": {
    "type": "internal error",
    "code": "USER_ERROR",
    "details": "This expense is already on a bill. Are you sure you want to edit it?",
    "userEvent": "aftersubmit",
    "stackTrace": [
      "anonymous(N/serverRecordService)",
      "afterSubmit(/SuiteScripts/Octavius App/vendorBillToItemReceiptUE.js:62)"
    ],
    "notifyOff": false
  },
  "id": "",
  "notifyOff": false,
  "userFacing": false
}
please have a look at the original code the one is throwing error
Copy code
define(['N/record'], function (record) {
    function afterSubmit(context) {
        try {
            if (context.type === context.UserEventType.CREATE) {
                var currentRecord = context.newRecord;

                var purchaseOrderId = currentRecord.getSublistValue({ sublistId: 'expense', fieldId: 'orderdoc', line: 0 });
                if (purchaseOrderId) {
                    var lineExpanses = [];
                    var lineCount = currentRecord.getLineCount({ sublistId: 'expense' });
                    var TR_RECORD = record.transform({
                        fromType: record.Type.PURCHASE_ORDER,
                        fromId: purchaseOrderId,
                        toType: record.Type.ITEM_RECEIPT
                    });
                    var TR_LINES = TR_RECORD.getLineCount({ sublistId: 'expense' });
                    for (var i = 0; i < TR_LINES; i++) {
                        TR_RECORD.setSublistValue({ sublistId: 'expense', fieldId: 'markreceived', line: i, value: false });
                    }
                    for (var i = 0; i < lineCount; i++) {
                        var currentOrderLine = currentRecord.getSublistValue({ sublistId: 'expense', fieldId: 'orderline', line: i });
                        for (var j = 0; j < TR_LINES; j++) {
                            var receiptOrderLine = TR_RECORD.getSublistValue({ sublistId: 'expense', fieldId: 'orderline', line: j });
                            if (currentOrderLine == receiptOrderLine) {
                                TR_RECORD.setSublistValue({ sublistId: 'expense', fieldId: 'markreceived', line: j, value: true });

                                var _account = currentRecord.getSublistValue({ sublistId: 'expense', fieldId: 'account', line: i })
                                var _amount = currentRecord.getSublistValue({ sublistId: 'expense', fieldId: 'amount', line: i })
                                lineExpanses.push({
                                    account: _account,
                                    amount: _amount,
                                    orderLine: currentOrderLine
                                });
                            }
                        }
                    }
                    var savedReceiptId = TR_RECORD.save({
                        enableSourceing: true
                    });
                    var dat = ' ';
                    var PO_RCD = record.load({ type: record.Type.PURCHASE_ORDER, id: purchaseOrderId });
                    var PO_LINES = PO_RCD.getLineCount({ sublistId: 'expense' });
                    for (var i = 0; i < lineCount; i++) {
                        var currentOrderLine = currentRecord.getSublistValue({ sublistId: 'expense', fieldId: 'orderline', line: i });
                        //dat = dat + 'currentOrderLine: ' + currentOrderLine + ' loopIndex:' + i;
                        for (var k = 0; k < PO_LINES; k++) {
                            var orderLine = PO_RCD.getSublistValue({ sublistId: 'expense', fieldId: 'line', line: k });
                            // might need to add more check account, amount, filter
                            if (currentOrderLine == orderLine) {
                                PO_RCD.setSublistValue({ sublistId: 'expense', fieldId: 'custcol_billed', line: k, value: true });
                                PO_RCD.setSublistValue({ sublistId: 'expense', fieldId: 'custcol_received', line: k, value: true });
                                dat = dat + ' PO OrderLine: ' + orderLine;
                            }
                            //dat = dat + '  orderLine: ' + orderLine + ' semiloopIndex:' + k;
                        }
                    }
                    var poorder = PO_RCD.save({
                        enableSourceing: false
                    });
                    log.debug({
                        title: 'ItemReceipt Id: ' + savedReceiptId + ' is created for PurchaseOrderId: ' + purchaseOrderId + ' existingPurchaseOrder: ' + poorder,
                        details: JSON.stringify(lineExpanses) + dat
                    });
                }
            }
        }
        catch (ex) {
            log.debug({
                title: 'Error creating ItemReceipt',
                details: JSON.stringify(ex)
            });
        }
    }
    return { afterSubmit: afterSubmit };
});
b
are you sure you want to edit it
that message is a warning that you are doing something unusual
and its likely that your web service request is setup to Treat Warnings as Errors
👍 1
id also like to add this this kind of script is one that will destroy performance
👍 1
you really need to consider using N/task to do the work asynchronously
👍 1
m
@battk is there anyway i can ignore this error in UserEvent Script
b
probably not, its a choice made by the webservice
or the company
the preference can be on the request or company level
m
@battk thing is we are saving bill through webservice and in UserEvent we are updating line items checkbox which is custom field of purchaseorder it should not raise any error
b
netsuite disagrees
and its vote is the only one that matters
if you want to see the equivalent ui action, edit the purchase order yourself in the ui
m
I have checked Netsuite Setup, It is showing there treat as an error is unchecked. Setup<Integration<SOAP Preferences
b
the help link lists both ways that the preference is controlled
check both
👍 1
m
@battk same message is coming in the UI, is there any way I can update the PO Line items checkboxes without any error message, or do you think we can update fields using workflow?
b
no
m
Hey @battk this is what i get response from Netsuite, do you think this will work
b
read the docs on the suitetalk soap preferences
same place as the warning documentation