title: Netsuite SuiteScript set custom segment value on sales order
body: <p>I can create a Netsuite sales order record using SuiteScript 2 and JavaScript. I am unable to find the field I need to set in order to set a custom segment value for my sales order. I am looking for help with this.</p>
  <p>My custom segment is called Revenue Channel and I can see it associated with a sales order. I can see my custom segments's ID (cseg1) and Record ID (customrecord_cseg1) on the custom segments page in Netsuite. When I view a Sales Order in Netsuite's UI, I can only see the Revenue Channel custom segment when I change my form to an alternate form, like my Standard Sales Order form.</p>
  <p>After I set this field in the UI, if I do a SuiteScript JavaScript search for this sales order, I am able to print (console log) every field on the sales order record, but my custom segment field does not show up. I am not sure how to find it so I can set its value when I create a new one programmatically.</p>
  <p>Does anyone know how to set a custom segment value for a sales order record programmatically using JavaScript?</p>
  <p>For example, this code below will print all of the fields of my sales order that I know has a custom segment (Revenue Channel) value set in the UI. I am unable to find this field on this sales order object.</p>
  <pre><code>require(['N/search', 'N/record'], function(search, record){
    var nsSalesOrderIds = []
    search
      .create({
        type: search.Type.SALES_ORDER,
        filters: [
          search.createFilter({
            name: 'internalid', // 'internalid' 'tranid'
            operator: search.Operator.IS,
            values: 123,
          }),
        ],
      })
      .run()
      .each(function (result) {
        console.log(result.toJSON())
        if (result.id) nsSalesOrderIds.push(result.id)
      })
      console.log(nsSalesOrderIds)
  
      var objRecord = record.load({
        type: search.Type.SALES_ORDER,
        id: nsSalesOrderIds[0],
        columns: 'itemid'
      });
  
      var objFields = objRecord.getFields();
  
      var emptyFields = []
      for (var i = 0; i &lt; objFields.sort().length; i++) {
        var val = objRecord.getValue({
          fieldId: objFields[i]
        });
        console.log(objFields[i] + ' = ' + val)
      }
  
      var sublistName = objRecord.getSublists();
      console.log(sublistName.sort())
  
      var itemFields = objRecord.getSublistFields({
          sublistId: 'item'
      });
      console.log(itemFields.sort())        
  })
  </code></pre>
  <p>This code below is how I can create a sales order, but I have not figured out how to save the custom segment for it.</p>
  <pre><code>  try {
      var salesOrder = record.create({
        type: record.Type.SALES_ORDER,
        isDynamic: true,
        defaultValues: {
          entity: 123,
        },
      });
  
      salesOrder.setValue({
        fieldId: 'location',
        value: 3123,
      })
  
      salesOrder.setText({
        fieldId: &quot;datecreated&quot;,
        text: &quot;7/18/2022&quot;,
      });
  
      // Setting custom segment here does 
      // not work but it does not error either.
      salesOrder.setText({
        fieldId: 'cseg1',
        text: 'My Custom Value',
        ignoreFieldChange: true
      })
  
      // This does not work either.
      salesOrder.setValue({
        fieldId: 'cseg1',
        value: 1,
      })
  
      // Set item specific
      salesOrder.selectNewLine({
        sublistId: 'item'
      });
  
      salesOrder.setCurrentSublistValue({
        sublistId: 'item',
        fieldId: 'item',
        value: 2121
      });
  
      salesOrder.setCurrentSublistValue({
        sublistId: 'item',
        fieldId: 'quantity',
        value: 1
      });
  
      salesOrder.commitLine({
        sublistId: 'item'
      });
  
      var newSalesOrderId = salesOrder.save();
      console.log('newSalesOrderId', newSalesOrderId)
    } catch (err) {
      console.log(err.toJSON())
    }
  })
  </code></pre>