Gordon Truslove
Gordon Truslove

Reputation: 109

How do I change a line item of a sales order using SuiteScript 2

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();
        }
}

Upvotes: 3

Views: 14266

Answers (6)

Tzesho
Tzesho

Reputation: 1

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

rec.commitLine();
rec.save();

Upvotes: 0

Akash Kriplani
Akash Kriplani

Reputation: 161

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.

Upvotes: 2

w3bguy
w3bguy

Reputation: 2250

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

Upvotes: 1

2ps
2ps

Reputation: 15916

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();

Upvotes: 0

erictgrubaugh
erictgrubaugh

Reputation: 8847

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++;
    }
  }
});

Upvotes: 0

TonyH
TonyH

Reputation: 1181

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.

Upvotes: 0

Related Questions