netsuitesuitescriptsuitescript2.0

Add new lines to suiblist from client script


There is a suitelet where I created a button Search, filter fields and a sublist. Initially sublist is emtpy. When user enters values to filter fields and clicks Search button it runs a saved search to get filtered transactions. Since function that runs on Search btn click is on Client Script Netsuite returns me this error TypeError: Cannot read properties of undefined (reading '0') when trying to add lines on Sublist. Here is my suitelet:

function onGetRequest(scriptContext) {
    const form = serverWidget.createForm({
        title: 'Suitelet',
    });

    form.clientScriptModulePath =
        '../client/cl_script.js';

    createFilterFields(form);
    createTransactionsSublist(form);
    createButtons(form);

    scriptContext.response.writePage(form);
}

function createTransactionsSublist(form) {
   const sublist = form.addSublist({
      id: CUSTPAGE_TXNS_SUBLIST,
      type: serverWidget.SublistType.LIST,
      label: 'Transactions',
   });

   sublist.addField({
      id: CUSTPAGE_TXNS_SUBLIST_TYPE,
      label: 'Type',
      type: serverWidget.FieldType.TEXT,
   }).updateDisplayType({
      displayType: serverWidget.FieldDisplayType.ENTRY,
   });
}

function createButtons(form) {
    form.addButton({
        id: CUSTPAGE_SEARCH_BUTTON,
        label: 'Search',
        functionName: 'onSearchButtonClick',
    });
}

And here is a portion of client script:

function onSearchButtonClick() {
    const currRec = currentRecord.get();
    const filterValues = getTransactionSearchFilterValues(currRec);
    const filteredTransactions = getFilteredTransactions(filterValues);

    currRec.selectNewLine({
        sublistId: CUSTPAGE_TXNS_SUBLIST,
    });
    currRec.setCurrentSublistValue({
        sublistId: CUSTPAGE_TXNS_SUBLIST,
        fieldId: CUSTPAGE_TXNS_SUBLIST_TYPE,
        value: 'Test text',
    });
    currRec.commitLine({
        sublistId: CUSTPAGE_TXNS_SUBLIST,
    });
}

According to documentation seems that I am doing this correctly but this still fails. When I tried to change sublist type to INLINEEDITOR this worked and sublist is being populated. However I need to achieve this with LIST type. May be there is a way to populate the sublist on suitelet, but I dont know how to pass trasnactions data back to the suitelet.


Solution

  • LIST Sublists have a fixed number of lines; you cannot add or remove lines to a LIST via either scripting or the UI.

    As you saw, an INLINEEDITOR works correctly with your technical approach.

    If you must have a LIST, you will need to populate the results server-side instead of client-side. You can turn your Search button into a SUBMIT button by using addSubmitButton() instead of addButton() to create it. When clicked, this will submit a POST request back to the Suitelet with the values specified in your filter fields populated in the incoming request.

    Although I don't recommend the coding style at all (putting all the logic in one entry point function), you can see an example of how to read the field values in NetSuite's example here.