I am writing suitescript 2.0 with Map/Reduce type.
I am fetching some list orders through API, I check for customer, if exist then create order otherwise first create customer and then add sales order with its items (which are new items). I am getting an error of You have entered an Invalid Field Value 5260365201455 for the following field: item
.
I am assuming (as I am new with suitescript), that while creating sales order following script will create its items too.
CODE -- UPDATED as per below answer
// updated function for creating item
function createChildItems(itemName) {
log.debug({ title: 'createChildItems', details: 'child item' });
var item = record.create({
type: record.Type.INVENTORY_ITEM,
isDynamic: true
});
item.setValue({ fieldId: 'itemid', value:itemName });
var itemId = item.save({
ignoreMandatoryFields: true
});
return itemId;
}
this above functions creates the item and return its internal id,
var salesOrder = record.create({
type: record.Type.SALES_ORDER,
isDynamic: true,
defaultValues: {
entity: customer.id
}
});
salesOrder.setValue({ fieldId: 'trandate', value: new Date(), ignoreFieldChange: true });
var subrec = salesOrder.getSubrecord({
fieldId: 'shippingaddress'
});
subrec.setValue({ fieldId: 'addr1', value: order.shipping_address.address1 });
subrec.setValue({ fieldId: 'city', value: order.shipping_address.city });
subrec.setValue({ fieldId: 'state', value: order.shipping_address.province });
subrec.setValue({ fieldId: 'zip', value: order.shipping_address.zip });
subrec.setValue({ fieldId: 'addressee', value: order.shipping_address.first_name });
subrec.setValue({ fieldId: 'attention', value: order.shipping_address.first_name });
salesOrder.selectNewLine({
sublistId: 'item'
});
var items = order.line_items; // this list of items are in array and all are new items, dos not exist in current netsuite
items.forEach(function (item) {
var internalID = createChildItems(item.name) // getting item internal ID here
salesOrder.setCurrentSublistValue({
sublistId: 'item',
fieldId: 'item',
value: internalID // item.id.toString() // also tried without toString(), and tried with value of existing item
});
salesOrder.setCurrentSublistValue({
sublistId: 'item',
fieldId: 'quantity',
value: item.quantity
});
salesOrder.setCurrentSublistValue({
sublistId: 'item',
fieldId: 'price',
value: item.price
});
salesOrder.setCurrentSublistValue({
sublistId: 'item',
fieldId: 'rate',
value: item.price
});
salesOrder.commitLine({
sublistId: 'item'
});
});
var id = salesOrder.save({
ignoreMandatoryFields: false
});
do I have to create these items before creating sales order.