简体   繁体   中英

add button and update sales order using netsuite

I am new to netsuite,i need to update sales order by adding an item to it using a button click i had write the code for button using user event script and code for updating sales order using client script.but my client script is not working

/**
 * @NApiVersion 2.x
 * @NScriptType ClientScript
 * @NModuleScope SameAccount
 */
define(['N/currentRecord','N/record'],

function(record) {

     function buttonclick() {

            try{
                var CurrRecord = currentRecord.get();
                var recIdSO= CurrRecord.id;
                var salesOrder = record.load({
                        type: record.Type.SALES_ORDER,
                        id:recIdSO ,
                        isDynamic: true
                      });
                 log.debug({
                     title: 'recordid',
                     details: 'Id: ' + recIdSO
                 }); 

                    var line=salesOrder.selectNewLine({
                     sublistId: 'item'

                 });

                    salesOrder.setCurrentSublistValue({
                            sublistId : 'item',
                            fieldId : 'item',
                            value :510 ,
                            ignoreFieldChange: true
                        });

                    salesOrder.setCurrentSublistValue({
                        sublistId : 'item',
                        fieldId : 'amount',
                        value :100 ,
                        ignoreFieldChange: true
                    });

                    salesOrder.commitLine({
                         sublistId: 'item'
                     });
                     var recId = salesOrder.save();
                     log.debug({
                         title: 'Record updated successfully',
                         details: 'Id: ' + recId
                     });

            }catch (e) {

                         log.error({
                             title: e.name,
                             details: e.message
                         });
                     }


        }





    /**
     * Function to be executed after page is initialized.
     *
     * @param {Object} scriptContext
     * @param {Record} scriptContext.currentRecord - Current form record
     * @param {string} scriptContext.mode - The mode in which the record is being accessed (create, copy, or edit)
     *
     * @since 2015.2
     *
    function pageInit(scriptContext) {

    }

    /**
     * Function to be executed when field is changed.
     *
     * @param {Object} scriptContext
     * @param {Record} scriptContext.currentRecord - Current form record
     * @param {string} scriptContext.sublistId - Sublist name
     * @param {string} scriptContext.fieldId - Field name
     * @param {number} scriptContext.lineNum - Line number. Will be undefined if not a sublist or matrix field
     * @param {number} scriptContext.columnNum - Line number. Will be undefined if not a matrix field
     *
     * @since 2015.2
     *
    function fieldChanged(scriptContext) {

    }

    /**
     * Function to be executed when field is slaved.
     *
     * @param {Object} scriptContext
     * @param {Record} scriptContext.currentRecord - Current form record
     * @param {string} scriptContext.sublistId - Sublist name
     * @param {string} scriptContext.fieldId - Field name
     *
     * @since 2015.2
     *
    function postSourcing(scriptContext) {

    }

    /**
     * Function to be executed after sublist is inserted, removed, or edited.
     *
     * @param {Object} scriptContext
     * @param {Record} scriptContext.currentRecord - Current form record
     * @param {string} scriptContext.sublistId - Sublist name
     *
     * @since 2015.2
     *
    function sublistChanged(scriptContext) {

    }

    /**
     * Function to be executed after line is selected.
     *
     * @param {Object} scriptContext
     * @param {Record} scriptContext.currentRecord - Current form record
     * @param {string} scriptContext.sublistId - Sublist name
     *
     * @since 2015.2
     *
    function lineInit(scriptContext) {

    }

    /**
     * Validation function to be executed when field is changed.
     *
     * @param {Object} scriptContext
     * @param {Record} scriptContext.currentRecord - Current form record
     * @param {string} scriptContext.sublistId - Sublist name
     * @param {string} scriptContext.fieldId - Field name
     * @param {number} scriptContext.lineNum - Line number. Will be undefined if not a sublist or matrix field
     * @param {number} scriptContext.columnNum - Line number. Will be undefined if not a matrix field
     *
     * @returns {boolean} Return true if field is valid
     *
     * @since 2015.2
     *
    function validateField(scriptContext) {

    }

    /**
     * Validation function to be executed when sublist line is committed.
     *
     * @param {Object} scriptContext
     * @param {Record} scriptContext.currentRecord - Current form record
     * @param {string} scriptContext.sublistId - Sublist name
     *
     * @returns {boolean} Return true if sublist line is valid
     *
     * @since 2015.2
     *
    function validateLine(scriptContext) {

    }

    /**
     * Validation function to be executed when sublist line is inserted.
     *
     * @param {Object} scriptContext
     * @param {Record} scriptContext.currentRecord - Current form record
     * @param {string} scriptContext.sublistId - Sublist name
     *
     * @returns {boolean} Return true if sublist line is valid
     *
     * @since 2015.2
     *
    function validateInsert(scriptContext) {

    }

    /**
     * Validation function to be executed when record is deleted.
     *
     * @param {Object} scriptContext
     * @param {Record} scriptContext.currentRecord - Current form record
     * @param {string} scriptContext.sublistId - Sublist name
     *
     * @returns {boolean} Return true if sublist line is valid
     *
     * @since 2015.2
     *
    function validateDelete(scriptContext) {

    }

    /**
     * Validation function to be executed when record is saved.
     *
     * @param {Object} scriptContext
     * @param {Record} scriptContext.currentRecord - Current form record
     * @returns {boolean} Return true if record is valid
     *
     * @since 2015.2
     */
    function saveRecord(scriptContext) {




    }

 return {

        saveRecord: saveRecord,
        buttonclick:buttonclick
    };

});

Your current example is almost correct.

You need a couple of things:

  1. first add fireSlavingSync:true as a property for all your setCurrentLineItemValue methods. Otherwise Netsuite might not have finished updating the item's field by the time you get to your commitLine call.
  2. I'd also tend to set ignoreFieldChange:false .
  3. you probably need to set a price level on the line item. Depending on your version of Netsuite you should be able to just set a custom price level:

    salesOrder.setCurrentSublistValue({ sublistId : 'item', fieldId : 'price', value :-1 , ignoreFieldChange: false, fireSlavingSync:true });

AFAIK, if you add buttons from client script, they are only available in EDIT mode and you are trying to load same record and update it, this would result in record has been changed error message on UI(Also, NetSuite doesn't recommend this). The better way would be to use workflows to update records. In workflow you could add button using the workflow itself and on button click fire your workflow action script to update line item on the Order. Here is sample you can refer to.

/**
 * @NApiVersion 2.x
 * @NScriptType ClientScript
 * @NModuleScope SameAccount
 */
define(['N/currentRecord', 'N/record'],

  function (currentRecord, record) {

    function pageInit(scriptContext) {

    }

    function buttonclick() {
      try {
        var salesOrder = currentRecord.get();
        console.log(salesOrder)
        var recIdSO = salesOrder.id;
        /*              var salesOrder = record.load({
                        type: record.Type.SALES_ORDER,
                        id:recIdSO ,
                        isDynamic: true
                      });*/
        /*              console.log('recordid'+ recIdSO);
        */               /*log.debug({
                           title: 'recordid',
                           details: 'Id: ' + recIdSO
                       }); */

        var line = salesOrder.selectNewLine({
          sublistId: 'item'

        });
        console.log('line' + line);

        salesOrder.setCurrentSublistValue({
          sublistId: 'item',
          fieldId: 'item',
          value: 510,
          ignoreFieldChange: true
        });

        salesOrder.setCurrentSublistValue({
          sublistId: 'item',
          fieldId: 'amount',
          value: 100,
          ignoreFieldChange: true
        });
        salesOrder.setCurrentSublistValue({
          sublistId: 'item',
          fieldId: 'taxcode',
          value: -160,
          ignoreFieldChange: true
        });

        salesOrder.commitLine({
          sublistId: 'item'
        });
        var recId = salesOrder.save();
        log.debug({
          title: 'Record updated successfully',
          details: 'Id: ' + recId
        });

      } catch (e) {

        console.log('recordid' + recIdSO);
        /*log.error({
            title: e.name,
            details: e.message
        });*/
      }
    }

    return {
      pageInit: pageInit,
      buttonclick: buttonclick
    };
  });

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM