How to create journal entry in netsuite api?

2.6k views Asked by At

I am trying to create a journal entry on NetSuite using its SuiteScript API.

Here is my code.

export function post() {
  var rec = record.create({
    type: record.Type.JOURNAL_ENTRY,
    isDynamic: true,
  });
  rec.setValue({
    fieldId: "memo",
    value: "from api ",
  });
  rec.setValue({
    fieldId: "type",
    value: "Journal",
  });
  rec.setText({
    fieldId: "type",
    text: "Journal",
  });

  rec.setValue({ fieldId: "transactionnumber", value: "158" });
  rec.setValue({ fieldId: "subsidiary", value: "2" });
  rec.setText({ fieldId: "subsidiary", text: "Headquarters : United States" });
  rec.setValue({ fieldId: "postingperiod", value: "238" });
  rec.setText({ fieldId: "postingperiod", text: "Dec 2021" });
  rec.setValue({ fieldId: "customform", value: "30" });
  rec.setText({ fieldId: "customform", text: "Standard Journal Entry" });

   
  rec.setValue({ fieldId: "exchangerate", value: "1.00" });
  //-------------------------debit------------------------------------------
  //start a new line
  rec.selectNewLine({
    sublistId: "line",
  });

  rec.setCurrentSublistValue({
    sublistId: "line",
    fieldId: "account",
    value: "1",
  });

  rec.setCurrentSublistValue({
    sublistId: "line",
    fieldId: "accounttype",
    value: "AcctRec",
  });

  rec.setCurrentSublistValue({
    sublistId: "line",
    fieldId: "account_display",
    value: "1100 Accounts Receivable",
  });

  rec.setCurrentSublistValue({
    sublistId: "line",
    fieldId: "debit",
    value: "200",
  });
  rec.setCurrentSublistValue({
    sublistId: "line",
    fieldId: "credit",
    value: "",
  });
  //close of line
  rec.commitLine({
    sublistId: "line",
  });
  //----------------------------credit---------------------------------------
  //new line again

  rec.selectNewLine({
    sublistId: "line",
  });

  rec.setCurrentSublistValue({
    sublistId: "line",
    fieldId: "account",
    value: "10",
  });

  rec.setCurrentSublistValue({
    sublistId: "line",
    fieldId: "accounttype",
    value: "OthCurrAsset",
  });

  rec.setCurrentSublistValue({
    sublistId: "line",
    fieldId: "account_display",
    value: "1200 Inventory",
  });

  rec.setCurrentSublistValue({
    sublistId: "line",
    fieldId: "credit",
    value: "200",
  });
  rec.setCurrentSublistValue({
    sublistId: "line",
    fieldId: "debit",
    value: "",
  });
  rec.commitLine({
    sublistId: "line",
  });
  var recordId = rec.save();
  //-------------------------------------------------------------------
  return JSON.stringify(recordId);
}

Everything looks good for me but I am getting the following error

{
    "error": {
        "code": "INVALID_FLD_VALUE",
        "message": "{\"type\":\"error.SuiteScriptError\",\"name\":\"INVALID_FLD_VALUE\",\"message\":\"You have entered an Invalid Field Value 1 for the following field: account\",\"stack\":[\"anonymous(N/serverRecordService)\",\"post(/SuiteScripts/ssv2/journal_restlet.js:73)\"],\"cause\":{\"type\":\"internal error\",\"code\":\"INVALID_FLD_VALUE\",\"details\":\"You have entered an Invalid Field Value 1 for the following field: account\",\"userEvent\":null,\"stackTrace\":[\"anonymous(N/serverRecordService)\",\"post(/SuiteScripts/ssv2/journal_restlet.js:73)\"],\"notifyOff\":false},\"id\":\"\",\"notifyOff\":false,\"userFacing\":false}"
    }
}

Basically which means the account value 1 which I m sending to create line is invalid. But 1 is a valid account id of a Cash account.

This is the response of lines when I call get method.

{
    "line": [
        {
            "scheduletype": "",
            "oldstartdate": "",
            "entity_display": "",
            "memo": "",
            "residual": "",
            "account_cur": "",
            "amortermsrc": "",
            "startdate": "",
            "tax1acct": "",
            "account_cur_fx": "F",
            "cleareddate": "",
            "custcol_adjustment_tax_code": "",
            "grossamt": "",
            "custcol_2663_isperson": false,
            "credit": "",
            "origdebit": 5000,
            "item": "",
            "sys_parentid": "2040634920479734",
            "tax1amt": "",
            "amortizationtype": "",
            "entity2": "",
            "custcol_emirate": "",
            "enddate": "",
            "entitytype": "Entity",
            "lineuniquekey": "89669",
            "hasimmutableamortization": false,
            "taxcode": "",
            "taxcode_display": "",
            "custcol_2663_eft_file_format": "",
            "cleared": "F",
            "line": 0,
            "accounttype": "AcctRec",
            "kpayitem": "",
            "timelineid": "",
            "eliminate": false,
            "custcol_adjustment_field": "",
            "accounteliminate": "F",
            "class_display": "",
            "giftcertcode": "",
            "origcredit": "",
            "sys_id": "2040634962802984",
            "account_display": "1100 Accounts Receivable",
            "btoach": false,
            "custcol_2663_lastname": "",
            "debit": 5000,
            "department": "",
            "oldresidual": "",
            "class": "",
            "ritemcount": "",
            "oldenddate": "",
            "custcol_2663_firstname": "",
            "custcol_far_trn_relatedasset": "",
            "department_display": "",
            "location_display": "",
            "schedule": "",
            "schedulenum": "",
            "taxrate1": "",
            "custcol_2663_companyname": "",
            "location": "",
            "entity_cur": "",
            "account": "6",
            "entity": ""
        },
        {
            "scheduletype": "",
            "oldstartdate": "",
            "entity_display": "",
            "memo": "",
            "residual": "",
            "account_cur": "",
            "amortermsrc": "",
            "startdate": "",
            "tax1acct": "",
            "account_cur_fx": "F",
            "cleareddate": "",
            "custcol_adjustment_tax_code": "",
            "grossamt": "",
            "custcol_2663_isperson": false,
            "credit": 5000,
            "origdebit": "",
            "item": "",
            "sys_parentid": "2040634920479734",
            "tax1amt": "",
            "amortizationtype": "",
            "entity2": "",
            "custcol_emirate": "",
            "enddate": "",
            "entitytype": "Entity",
            "lineuniquekey": "89670",
            "hasimmutableamortization": false,
            "taxcode": "",
            "taxcode_display": "",
            "custcol_2663_eft_file_format": "",
            "cleared": "F",
            "line": 1,
            "accounttype": "OthCurrAsset",
            "kpayitem": "",
            "timelineid": "",
            "eliminate": false,
            "custcol_adjustment_field": "",
            "accounteliminate": "F",
            "class_display": "",
            "giftcertcode": "",
            "origcredit": 5000,
            "sys_id": "2040634962760922",
            "account_display": "1200 Inventory",
            "btoach": false,
            "custcol_2663_lastname": "",
            "debit": "",
            "department": "",
            "oldresidual": "",
            "class": "",
            "ritemcount": "",
            "oldenddate": "",
            "custcol_2663_firstname": "",
            "custcol_far_trn_relatedasset": "",
            "department_display": "",
            "location_display": "",
            "schedule": "",
            "schedulenum": "",
            "taxrate1": "",
            "custcol_2663_companyname": "",
            "location": "",
            "entity_cur": "",
            "account": "10",
            "entity": ""
        }
    ],
    "recmachcustrecord109": [],
    "recmachcustrecord_2663_parent_bill": [],
    "recmachcustrecord_2663_parent_payment": [],
    "recmachcustrecord_2663_transaction": [],
    "recmachcustrecord_assetsourcetrn": [],
    "recmachcustrecord_deprhistjournal": [],
    "recmachcustrecord_far_expinc_transaction": [],
    "recmachcustrecord_nsts_rfc_created_trans": [],
    "recmachcustrecord_nsts_rfc_related_records": [],
    "recmachcustrecord_summary_histjournal": [],
    "recmachcustrecord_wmsse_order_no": [],
    "recmachcustrecord_wmsse_order_no_clt": [],
    "recmachcustrecord_wmsse_ordno": [],
    "recmachcustrecord_wmsse_parent_orderno": [],
    "recmachcustrecord_wmsse_ship_order": [],
    "activities": [],
    "mediaitem": [],
    "usernotes": [],
    "links": [],
    "systemnotes": [],
    "calls": [],
    "tasks": [],
    "events": []
}

Here account ids are 6 and 10. I still get the same error even I use 6 and 10 in the account fields. Does anyone know how to solve this issue?

Thanks, Krishna

3

There are 3 answers

0
Krishna Karki On BEST ANSWER

The problem was subsidiary and my account ids were not matching. Here is the complete code if someone needs it in the future.

export function post() {
  var rec = record.create({
    type: record.Type.JOURNAL_ENTRY,
    isDynamic: true,
  });
  rec.setValue({
    fieldId: "memo",
    value: "from api ",
  });
  rec.setValue({
    fieldId: "type",
    value: "Journal",
  });
  rec.setText({
    fieldId: "type",
    text: "Journal",
  });

  // rec.setValue({ fieldId: "transactionnumber", value: "158" });
  rec.setValue({ fieldId: "subsidiary", value: "2" });
  rec.setText({ fieldId: "subsidiary", text: "Headquarters : United States" });
  rec.setValue({ fieldId: "postingperiod", value: "238" });
  rec.setText({ fieldId: "postingperiod", text: "Dec 2021" });
  rec.setValue({ fieldId: "trandate", value: new Date("12/27/2021") });

  // rec.setValue({ fieldId: "customform", value: "30" });
  // rec.setText({ fieldId: "customform", text: "Standard Journal Entry" });

  rec.setValue({ fieldId: "exchangerate", value: "1.00" });
  //-------------------------debit------------------------------------------
  //start a new line
  rec.selectNewLine({
    sublistId: "line",
  });

  rec.setCurrentSublistValue({
    sublistId: "line",
    fieldId: "account",
    value: 363,
  });

  rec.setCurrentSublistValue({
    sublistId: "line",
    fieldId: "accounttype",
    value: "AcctRec",
  });

  rec.setCurrentSublistValue({
    sublistId: "line",
    fieldId: "account_display",
    value: "1100 Accounts Receivable",
  });

  rec.setCurrentSublistValue({
    sublistId: "line",
    fieldId: "debit",
    value: "200",
  });
  rec.setCurrentSublistValue({
    sublistId: "line",
    fieldId: "credit",
    value: "",
  });
  //close of line
  rec.commitLine({
    sublistId: "line",
  });
  //----------------------------credit---------------------------------------
  //new line again

  rec.selectNewLine({
    sublistId: "line",
  });

  rec.setCurrentSublistValue({
    sublistId: "line",
    fieldId: "account",
    value: 10,
  });

  rec.setCurrentSublistValue({
    sublistId: "line",
    fieldId: "accounttype",
    value: "OthCurrAsset",
  });

  rec.setCurrentSublistValue({
    sublistId: "line",
    fieldId: "account_display",
    value: "1200 Inventory",
  });

  rec.setCurrentSublistValue({
    sublistId: "line",
    fieldId: "credit",
    value: "200",
  });
  rec.setCurrentSublistValue({
    sublistId: "line",
    fieldId: "debit",
    value: "",
  });
  rec.commitLine({
    sublistId: "line",
  });
  var recordId = rec.save();
  //-------------------------------------------------------------------
  return JSON.stringify(recordId);
}
0
Sayeesh On

I guess its throwing an error because you are passing the account value as a string.

Try this -

rec.setCurrentSublistValue({
    sublistId: "line",
    fieldId: "account",
    value: Number(1)   //Change this
});

Let me know in case of any issues.

0
Pierre Plourde On

It looks like you are trying to set the account using the account name or account number rather than the account internal id. The setCurrentSublistValue call, when dealing with a linked record like this, needs the internal ID, not the name or number:

rec.setCurrentSublistValue({
    sublistId: "line",
    fieldId: "account_display",
    value: "{internalid of account}",
  });

EDIT: Assuming here that you mean to use SuiteScript 2.0, since that's the code you wrote, and not SuiteTalk, which is different code altogether.