Hi everyone, I am able to add a new vendor to an i...
# suitescript
a
Hi everyone, I am able to add a new vendor to an item record, but I am not able to update an existing vendors purchase price. No errors either. I rotate through the vendor sublist until I find the vendor I need, do a price comparison, and then attempt to update it, to no avail. This is in a Map/Reduce and loads the item in dynamic mode.
Copy code
var numLines = itemRecord.getLineCount({ sublistId: vendorSublist });
                for (var i = 0; i < numLines; i++) {
                    var currentVendor = itemRecord.getSublistValue({
                        sublistId: vendorSublist,
                        fieldId: 'vendor',
                        line: i
                    });
                    log.debug({ title: 'currentVendor', details: currentVendor + ', ' + 'line: ' + i });

                    if (currentVendor == vendorId) {
                        hasVendor = true;

                        var unformattedVendorPrice = itemRecord.getSublistValue({
                            sublistId: vendorSublist,
                            fieldId: 'purchaseprice',
                            line: i
                        });

                        var vendorPrice = parseFloat(unformattedVendorPrice).toFixed(2);

                        if (vendorPrice != tradePrice) {
                            itemRecord.selectLine({ sublistId: vendorSublist, line: i });
                            itemRecord.setCurrentSublistValue({
                                sublistId: vendorSublist,
                                fieldId: 'purchaseprice',
                                value: tradePrice
                            });
                            itemRecord.commitLine({ sublistId: vendorSublist });
                        }
                        break;
                    }
                }

                itemRecord.save();
b
your description is too vague, for example your code could fail at finding the vendor, the price comparison, or the attempt to update the price
either confirm that each piece of the logic works, or work on one at a time
for example, starting at code that always updates the first vendor's price to be a hardcoded number
a
I added a removeLine() and can readd the vendor line as new without issue.
something in the selectLine() setCurrentSublistValue() is not correct.
b
and what did that code look like
a
Old:
Copy code
itemRecord.selectLine({ sublistId:vendorSublist, line: i });
                            itemRecord.setCurrentSublistValue({
                                sublistId: vendorSublist,
                                fieldId: 'purchaseprice',
                                value: tradePrice
                            });
                            itemRecord.commitLine({ sublistId: vendorSublist });
Copy code
Old Code:

itemRecord.selectLine({ sublistId: vendorSublist, line: i });
itemRecord.setCurrentSublistValue({
    sublistId: vendorSublist,
    fieldId: 'purchaseprice',
    value: tradePrice
});
itemRecord.commitLine({ sublistId: vendorSublist });
Copy code
New code that does work... but I'd prefer to update the existing line.

//remove line
itemRecord.selectLine({
    sublistId: vendorSublist,
    line: i
});
itemRecord.removeLine({
    sublistId: vendorSublist,
    line: i
});
//readd it
itemRecord.selectNewLine({ sublistId: vendorSublist });
itemRecord.setCurrentSublistValue({
    sublistId: vendorSublist,
    fieldId: 'vendor',
    value: vendorId
});
itemRecord.setCurrentSublistValue({
    sublistId: vendorSublist,
    fieldId: 'subsidiary',
    value: 3
});
itemRecord.setCurrentSublistValue({
    sublistId: vendorSublist,
    fieldId: 'preferredvendor',
    value: true
});
itemRecord.setCurrentSublistValue({
    sublistId: vendorSublist,
    fieldId: 'purchasepricecurrency',
    value: 1
});
itemRecord.setCurrentSublistValue({
    sublistId: vendorSublist,
    fieldId: 'purchaseprice',
    value: tradePrice
});

itemRecord.commitLine({ sublistId: vendorSublist });
b
thats unusual, what does your item vendor sublist look like in the ui while editing the line you want to change
if you are trying to set currency, then the purchase price is not actually a field on the sublist and is instead represented by a subrecord
a
Standard multi-vendor line with currency. (the above does add currency correctly).
b
it works sometimes, purchase prices means you click a button to edit a subrecord in the ui
you need to do the same thing in script
a
Indeed, purchase price does have a click for currency/price.
I guess new lines don't require that?
b
the subrecord allows multiple different purchase prices for a single vendor
there can be logic to convert a single purchase price for a vendor into the subrecord
but not to update one of potentially many prices
you need to actually use the subrecord
a
Copy code
itemRecord.selectLine({
    sublistId: vendorSublist,
    line: i
});

var vendorSubrecord = itemRecord.getCurrentSublistSubrecord({
    sublistId: 'itemvendor',
    fieldId: 'itemvendorprice'
});
log.debug({ title: 'vendorSubrecord', details: vendorSubrecord });

vendorSubrecord.setValue({
    fieldId: 'vendorprice',
    value: tradePrice
});

itemRecord.commitLine({ sublistId: vendorSublist });
I think a step closer with this. the subrecord variable brings up this data:
Copy code
{
  "id": "156863",
  "type": "itemvendorprice",
  "isDynamic": true,
  "fields": {
    "nsapiCT": "1696013507138",
    "sys_id": "2609269057979792",
    "sys_parentid": "2609268150473793",
    "entryformquerystring": "item=5&e=T&vendor=3350973&id=156863",
    "_eml_nkey_": "123456_SB1~-4~3~N",
    "id": "156863",
    "type": "itemvendorprice"
  },
  "sublists": {
    "itemvendorpricelines": {
      "currentline": {
        "id": "",
        "sys_id": "-2609269057862991",
        "sys_parentid": "2609269057858487",
        "vendorcurrency": "",
        "vendorprice": "",
        "#": "2"
      },
      "line 1": {
        "id": "157013",
        "sys_id": "2609269057963661",
        "sys_parentid": "2609269057979792",
        "vendorcurrency": "1",
        "vendorprice": "1.00"
      }
    }
  }
}
Still cannot figure out how to update the price, however
b
the price is on the itemvendorpricelines sublist
with each line representing a different currency
same as the subrecord in the ui
it usually helps to know how to do the process in the ui