0
votes

Creating in new Sales Order in NetSuite, I want to use values in custom fields on the item record to retrieve and enter the items in the line item section of the transaction. Can this functionality by coded using SuiteScript 2.0?

3
Just so I'm clear on the question based on your comment to the answer below. You have a custom field (or maybe multiple fields) on the item. Now you want to add a custom transaction line field (or again, maybe multiple) of that same type. If the user enters a value in that custom field, the script would find the associated item and set the item for that line accordingly? Assuming that's the question, it's definitely doable. - sbkp
Yes, this is correct. The User has a transaction and wants to pull an item not by keying in the item's number (as is standard in NetSuite), but entering a string in a custom transaction line field which would reference another custom field on the Item record and eventually populate the transaction line. It should be a Client Script reacting dynamically to the value in the custom transaction line field. - Andre

3 Answers

1
votes

Based on your comment on Krypton's answer, I think what you want to do is:

  1. Create a custom transaction line field to take the user input
  2. Create a Client Script that has a method on the fieldChanged event and check if the field is your custom one
  3. If it is, use the value entered to perform an Item search against the custom item field, and retrieve the internal ID of the item
  4. Using the internal ID, set the line's Item field

Hope this helps!

2
votes

Yes, but you do not need scripting for this.

  1. Create your Item custom field.
  2. Create a Transaction Line custom field.
  3. In the Applies To tab check Sale Item
  4. In the Sourcing and Filtering tab, select Item for the Source List field, and then select the field you created in step one for the Source From field.

Now when you enter an item in a sales order, the value from the custom item field will be copied to the corresponding field in the sales order line.

0
votes

Try the following. It's incomplete in that it doesn't handle errors, multiple matches, or partial matches, but the essential parts of the script are there.

You could also use validateField instead of fieldChanged (same source, but add return true;). In that case, if you find the user entered bad data, you could return false.

/**
 *@NApiVersion 2.x
 *@NScriptType ClientScript
 */
define(['N/search'],
    function(search) {
        function setItemFromTestValue(currentRecord) {
            var testValue = currentRecord.getCurrentSublistValue({
                sublistId: 'item',
                fieldId: 'custcol_test'
            });

            if (!testValue) {
                return;
            }

            var itemSearch = search.create({
                type: search.Type.ITEM,
                filters: [{
                    name: 'custitem_test',
                    operator: 'is',
                    values: [testValue]
                }]
            });

            var items = [];

            itemSearch.run().each(function(result) {
                items.push(result.id);
                return true;
            });

            // how to handle multiple items found and no items found are left to you
            if (items.length) {
                // don't reset current item
                var currentItem = currentRecord.getCurrentSublistValue({
                    sublistId: 'item',
                    fieldId: 'item'
                });

                if (items[0] != currentItem) {
                    currentRecord.setCurrentSublistValue({
                        sublistId: 'item',
                        fieldId: 'item',
                        value: items[0]
                    });
                }
            }
        }

        function fieldChanged(context) {
            var sublistName = context.sublistId;
            var sublistFieldName = context.fieldId;
            if (sublistName === 'item' && sublistFieldName === 'custcol_test') {
                console.log('fieldChanged');
                setItemFromTestValue(context.currentRecord);
            }
        }

        return {
            fieldChanged: fieldChanged
        };
    });