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.
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.
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.
Does anyone know how to set a custom segment value for a sales order record programmatically using JavaScript?
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.
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 < 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())
})
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.
try {
var salesOrder = record.create({
type: record.Type.SALES_ORDER,
isDynamic: true,
defaultValues: {
entity: 123,
},
});
salesOrder.setValue({
fieldId: 'location',
value: 3123,
})
salesOrder.setText({
fieldId: "datecreated",
text: "7/18/2022",
});
// 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())
}
})
The custom segment value is saved on another form. In my example, that form is ID = 23. In order to get access to that 'cseg1' field, I need to include the form in with the defaultValues when I create my sales order. See my code below.
var salesOrder = record.create({
type: record.Type.SALES_ORDER,
isDynamic: true,
defaultValues: {
entity: 123,
customForm: 23,
},
})
// Either way works, you don't need both
salesOrder.setValue{
fieldId: 'cseg1',
value: 1,
})
// Either way works, you don't need both
salesOrder.setText({
fieldId: 'cseg1',
text: 'Some String',
})