3

OK, this works in SuiteScript 1, but for some reason I can't get it to work with SuiteScript 2. What am I missing?

Client side (on field changed/on line changed) or server side (on submit), I can't alter the lines on a sales order, e.g. set a custom field.

Neither of these work. Either the function is not found or nothing happens. I've tried all sorts of different functions and combinations for setting the text or the value. I just doesn't seem to work:

1.

Rec.setSublistText({
    sublistId : 'item',
    fieldId : 'custcol_example',
    line : i,
    value : "A"
});

2.

Rec.selectLine({
    sublistId : 'item',
    line : i
});
Rec.setCurrentSublistText({
    sublistId : 'item',
    fieldId : 'custcol_example',
    value : "A",
    ignoreFieldChange: true
});
Rec.commitLine();

This works perfectly in SuiteScript 1

function clientFieldChanged(type, name, linenum) {
    var Count = nlapiGetLineItemCount("item");
    for (var i = 1; i <= Count; i++) {
        nlapiSelectLineItem("item", i);
        nlapiSetCurrentLineItemValue("item", "custcol_example", "A", false, false);
        nlapiCommitLineItem("item");
    }
}

As requested, a more complete SS2 example. Doesn't work.

function fieldChanged(scriptContext) {
        var Rec = scriptContext.currentRecord;
        var Count = Rec.getLineCount("item");
        for (var i = 0; i < Count; i++) {
            Rec.selectLine({
                sublistId : 'item',
                line : i
            });
            Rec.setCurrentSublistText({
                sublistId : 'item',
                fieldId : 'custcol_example',
                line : i,
                value : "A"
            });
            Rec.commitLine();
        }
}
Gordon Truslove
  • 109
  • 1
  • 1
  • 10

6 Answers6

2

There's a slight bug in your code 1.

Rec.setSublistText({
    sublistId : 'item',
    fieldId : 'custcol_example',
    line : i,
    value : "A"
});

Since you are using setSublistText() you should use 'text': 'A', not value. It goes like this:

Rec.setSublistText({
    sublistId : 'item',
    fieldId : 'custcol_example',
    line : i,
    text: "A"
});

Hope this helps.

Akash Kriplani
  • 162
  • 1
  • 12
1

I had a similar error when trying to grab address records on a contact. Looks like to get the data from a sublist subrecord, you have to load the record using record.load, and place it into dynamic mode.

Mine is an AfterSubmit function, though, so it may not solve your problem. Hope that helps out. Below is the link to the record.load function, if you don't already have it.

https://netsuite.custhelp.com/app/answers/detail/a_id/45155/kw/record.load

w3bguy
  • 2,215
  • 1
  • 19
  • 34
0

Your code looks like it should work in both cases. Have you tried using a JS console, and repeating these scripts line-by-line?

For instance, in Chrome, start editing a transaction record, then open the Dev console by hitting Ctrl-Shift-I. There you can copy and paste your code line by line, to interactively see the effects of each operation. Hopefully then, you can spot when things go wrong.

TonyH
  • 1,117
  • 8
  • 18
  • There are a variety of errors I get. Most common are errors like this: – Gordon Truslove May 29 '16 at 20:33
  • TypeError: Cannot find function selectLine in object DeferredDynamicRecord. – Gordon Truslove May 29 '16 at 20:33
  • It seem like somehow you don't have a normal record. You're getting this error in the GUI, right? – TonyH May 29 '16 at 20:38
  • That one is from the NS error page. I'm either using function beforeSubmit(scriptContext) or function fieldChanged(scriptContext) , the record object I am using is scriptContext.newRecord or scriptContext.currentRecord. Either the function are not found or the value is not changed. How would I test these events in a browser console? The record values come from events. – Gordon Truslove May 30 '16 at 18:57
0

Can you show more of your code as well as explain what type of script this is?

Make sure your index variable is correct; sublist line indexes in 1.0 started at 1, while in 2.0 they start at 0.

In order to use the selectLine and *Current* APIs, the record must be loaded in Dynamic mode. In a Client script, the currentRecord should always be in dynamic mode, but in other script types you need to explicitly load the record in Dynamic mode. The Cannot find function selectLine error occurs when you are working with a record in Standard mode instead of Dynamic mode.

Below is a 2.0 example that just runs in the console or debugger. It loads a Sales Order in Dynamic mode, then marks all of the item lines as Closed. Note how itemIndex starts at 0.

require(["N/record"], function (rec) {
  var salesOrder = rec.load({
    "type": rec.Type.SALES_ORDER,
    "id": 7610,
    "isDynamic": true
  });

  closeOrderDynamic(salesOrder);

  // Utility function that closes the provided Sales Order record
  // order must be in Dynamic mode
  function closeOrderDynamic(order) {
    var itemIndex = 0;
    var itemCount = order.getLineCount({
      "sublistId": "item"
    });

    while (itemIndex < itemCount) {
      order.selectLine({
        "sublistId": "item",
        "line": itemIndex
      });
      order.setCurrentSublistValue({
        "sublistId": "item",
        "fieldId": "isclosed",
        "value": true
      });
      // Must commit the line after we've changed it to save modifications
      order.commitLine({
        "sublistId": "item"
      });
      itemIndex++;
    }
  }
});
erictgrubaugh
  • 8,519
  • 1
  • 20
  • 28
0

Based on your SS1 code, you should be using setCurrentSublistValue or setSublistValue:

Rec.selectLine({
    sublistId : 'item',
    line : i
});
Rec.setCurrentSublistValue({
    sublistId : 'item',
    fieldId : 'custcol_example',
    value : "A",
    ignoreFieldChange: true
});
Rec.commitLine();
2ps
  • 15,099
  • 2
  • 27
  • 47
  • This is the error: TypeError: Cannot find function selectLine in object DeferredDynamicRecord. I think NS found a solution for me. I'll post it. – Gordon Truslove Jun 06 '16 at 07:10
0

You will need to save the record after you commit the line.

rec.commitLine();
rec.save();
Tzesho
  • 1
  • 2
  • I am trying the same, but getting the below error {"type":"error.SuiteScriptError","name":"RCRD_HAS_BEEN_CHANGED","message":"Record has been changed".... any help please? – Ba.Lal Sep 30 '19 at 13:05
  • 1
    This just means you have previously saved this record, or that this record has been saved outside of what you are doing. I would go through your code and check and make sure that the record isn't saved anywhere else. If you can't find anything then you might load the record just before the edit, save the record, and then discard the object.... reload rec as needed. – Tzesho Oct 01 '19 at 17:27
  • Yes. I previously saved the record somewhere and now I find its working by creating different object and save the record – Ba.Lal Oct 03 '19 at 07:57