AWendel
AWendel

Reputation: 1

Suitescript set 'Create PO' to dropship

[NetSuite] I would like to have a Suitescript to Set the "Create PO" sublist Item field to 'DropShip' when a Sales Order is created via the user interface. I would also like to add another "if" to check a custom field.

I have found a SuiteAwners article about setting that field:

"Setting the Create PO field to null via SuiteScript" Answer Id: 35911 I have gotten the Suitescript 1.0 to work (I have got it to set the 'createpo' to 'DropShip', and I have not been able to and another if statement to check a custom field on the Sales Order)

Here is the Suitescript 1.0 Code:

function beforeSubmit(type)
{
  var count = nlapiGetLineItemCount('item');
  for (i=1; i<= count; i++)
  {
    var currentContext = nlapiGetContext();
    //setting of 'createpo' field only happens when the script is triggered via User Interface
    //add other if conditions here, if needed
    // 'createpo' values are Null, 'DropShip', or 'SpecOrd'
    if((currentContext.getExecutionContext() == 'userinterface'))
    {
      nlapiSetLineItemValue('item', 'createpo', i, 'DropShip');
    }
  }
}

I know that Suitescript 1.0 is deprecated so I would love to rewrite it in Suitescript 2.0. Fortunately, the SuiteAwnsers article has some code for that as well.

Here is the Suitescript 2.0 code

function beforeSubmit(type){
  var count = objRecord.selectLine({
    sublistId: 'item',
    line: i
  });
  
  for (i=1; i<= count; i++)    {
    var currentContext = runtime.executionContext();
    
    //setting of 'createpo' field only happens when the script is triggered via Web Services
    //add other if conditions here, if needed
    if((currentContext.getExecutionContext() == 'webservices'))        {
      objRecord.setSublistValue({
        sublistId: 'item',
        fieldId: 'createpo',
        line: i,
        value: null
      });
    }
  }
}

Unfortunately, when I got to Create a new script in Netsuite it only gave me "Select 1.0 Script Type " as an option not "Select 2.0 Script Type".

I know this is a bit of a long one so if you have made it this far round of applause! to sum it up there are two things I need help with

1. How can I add an "if" that checks a filed value on the sales order "before submit" on Suitescript 1.0

2. How Can I get the suitescript 2.0 to work (including the "if" that checks a filed value on the sales order "before submit")

Just so you all know I am really new to scripting so if you have solutions please give as much detail as possible

Upvotes: 0

Views: 936

Answers (1)

TinMonkey
TinMonkey

Reputation: 1842

First of all, you need to set the script version at the top of your script for it to be recognized as version 2:

 /**
 * @NApiVersion 2.x
 * @NScriptType ClientScript
 * @NModuleScope SameAccount
 */

Next, for your script:

  1. your count var is not getting a line count, it is selecting a line and you are passing an uninstantiated var i into it. You need to use record.getLineCount() instead:

    var count = objRecord.getLineCount({ sublistId: 'item' });

  2. Start your for loop at 0. In suitescript 2 all lists are 0 indexed.

    for (i = 0; i < count; i++) {

  3. Since you are referencing the runtime module, make sure it is included in your define statement.

The final script might look something like this:

/**
* @NApiVersion 2.x
* @NScriptType UserEventScript
* @NModuleScope SameAccount
*/

define( ['N/runtime'], function(Runtime) {

function beforeSubmit(context){
  // get record from context
  var objRecord = context.newRecord;

  // get item list line count
  var count = objRecord.getLineCount({
    sublistId: 'item'
  });

  // start for loop at 0. lines are 0 indexed
  for (i = 0; i < count; i++)    {
    var currentContext = Runtime.executionContext();
  
    //setting of 'createpo' field only happens when the script is triggered via Web Services
    //add other if conditions here, if needed
    if((currentContext.getExecutionContext() == 'webservices'))        {
      objRecord.setSublistValue({
        sublistId: 'item',
        fieldId: 'createpo',
        line: i,
        value: 'DropShip'
      });
    }
  }
}

return {
  beforeSubmit: beforeSubmit
};

});

Hopefully this will get you started at least. The question is a bit confusing but this should get you a functional 2.0 script to start working with.

Upvotes: 0

Related Questions