Add New line item in Sales Order Using Suite Script 2.0 - netsuite

I want to add new line item in Sales Order based on the particular Inventory item I select. The new line item should be loaded with the existing item record details in it. For this, I used the below code, but its not working;
function recalc(type)
{
var itemId = nlapiGetCurrentLineItemValue('item', 'item'); //Get the Item ID
if(itemId == 16340) //Repair Cost
{
alert ("Hi");
//Insert item
nlapiSelectNewLineItem('item');
nlapiSetCurrentLineItemValue('item', 'item', 330); //Repair Cost
nlapiSetCurrentLineItemValue('item', 'quantity', 1);
nlapiSetCurrentLineItemValue('item', 'amount', '0.00');
nlapiCommitLineItem('item');
}
return true;
}
Can somebody help to rewrite the code in SS 2.0.

You can refer SuiteScript 1.0 to SuiteScript 2.0 API Map for NetSuite SuiteScript 1 to 2 API.
As for your question, you can use the following
function recalc(type) {
currentRecord.selectLine({ sublistId: 'item', line: LINE_NO });
var itemId = currentRecord.getCurrentSublistValue({ sublistId: 'item', fieldId: 'item' }); // Get the Item ID
if (itemId == 16340) {//Repair Cost
// Insert item
currentRecord.selectNewLine({ sublistId: 'item' });
currentRecord.setCurrentSublistValue({ sublistId: 'item', fieldId: 'item', value: 330 });//Repair Cost
currentRecord.setCurrentSublistValue({ sublistId: 'item', fieldId: 'quantity', value: 1 });//Repair Cost
currentRecord.setCurrentSublistValue({ sublistId: 'item', fieldId: 'amount', value: '0.00' });//Repair Cost
currentRecord.commitLine({ sublistId: 'item' });
}
return true;
}

Related

NetSuite setting sales order item price through client script

Is it possible to set the price of a sales order item in NetSuite SuiteScript 2.0?
I've got the following function that will set the quanity, amount and description. However, I message is displayed saying that the amount and price does NOT match. It seems that the price field defaults back to the price of the original inventory (or non-inventory in this case) item. Even attempting to set a hard code the value in the function does not work.
function updateSalesOrderItem(configuredItem) {
console.log("updateSalesOrderItem....")
var salesOrder = currentRecord.get();
var lineItem = salesOrder.selectLine({
sublistId: "item",
line: lastItemModified.rowNumber
});
console.log({"lineItem": lineItem});
var priceSubListValue = salesOrder.getCurrentSublistValue({
sublistId: "item",
fieldId: "price"
});
console.log({"current price": priceSubListValue});
console.log(" Setting the quantity...");
salesOrder.setCurrentSublistValue({
sublistId: "item",
fieldId: "quantity",
value: configuredItem.quantity
});
// Update the item price to suppress warning message. Before the item is configured, we don't know the price
// therefore it wouldn't match and the warning message is displayed
/*
var amt = parseFloat(configuredItem.amount);
var q = parseInt(configuredItem.quantity);
var p = amt/q;
var price = p.toFixed(2);
console.log({'price': price});
*/
salesOrder.setCurrentSublistValue({
sublistId: "item",
fieldId: "price",
value: "999.99"
});
salesOrder.setCurrentSublistValue({
sublistId: "item",
fieldId: "amount",
value: configuredItem.amount
});
console.log(" Setting the description...");
salesOrder.setCurrentSublistValue({
sublistId: "item",
fieldId: "description",
value: configuredItem.description
});
salesOrder.commitLine({sublistId: "item"});
}
set the price level to custom prior to setting the rate.
salesOrder.setCurrentSublistValue({
sublistId: "item",
fieldId: "price",
value: -1
});
salesOrder.setCurrentSublistValue({
sublistId: "item",
fieldId: "rate",
value: 999.00
});
If you are in a jCurve account and can't set a custom price level you may need to set set the item itself as having a variable price. That is available if you have Netsuite ecommerce available in your account.

Client script - Field change event - Bodyfield checkbox - set sublist value only changes one line

I've created a client script (field change) on a task record.
The Sublist is a custom child record. I want that if a bodyfield checkbox is set to true -> change all sublist line items...
In theory a " Mark all / select" all checkbox for the sublist.
The script is working for at least 1 line... If I use selectline "i" it will work with the LAST line of the sublist -> if I start with "0" it changes the value on the first line...
How can I solve this?
/**
*#NApiVersion 2.x
*#NScriptType ClientScript
*/
define(['N/error', 'N/currentRecord'],
function(error) {
function fieldChanged(context) {
var currentRecord = context.currentRecord;
var subList = currentRecord.getSublist({
sublistId: 'recmachcustrecord250'});
var numLines = currentRecord.getLineCount({
sublistId: 'recmachcustrecord250'});
var currIndex = currentRecord.getCurrentSublistIndex({
sublistId: 'recmachcustrecord250'});
for (var i = 0; i < numLines; i++) {
var checkbox = currentRecord.getValue({
fieldId: 'custevent28'});
currentRecord.selectLine({
sublistId: 'recmachcustrecord250',
line: 0});
if(context.fieldId == 'custevent28'){
if (checkbox == true) {
currentRecord.setCurrentSublistValue({
sublistId: 'recmachcustrecord250',
fieldId: 'custrecord265',
line: i,
value: true,
forceSyncSourcing:true
});
} else {
currentRecord.setCurrentSublistValue({
sublistId: 'recmachcustrecord250',
fieldId: 'custrecord265',
line: i,
value: false,
forceSyncSourcing:true
});
currentRecord.commitLine({
sublistId: 'recmachcustrecord250'});
}
}
}
}
return {
fieldChanged: fieldChanged
};
});
Found it...
set CurrentSublistValue is without "line" & with ignoreFieldchange...
if(context.fieldId == 'custevent28'){
if (checkbox == true) {
currentRecord.setCurrentSublistValue({
sublistId: 'recmachcustrecord250',
fieldId: 'custrecord265',
// line: i,
value: currentRecord.getValue({
fieldId: 'custevent28'}),
ignoreFieldChange: true
});
currentRecord.commitLine({
sublistId: 'recmachcustrecord250'});
} else {
currentRecord.setCurrentSublistValue({
sublistId: 'recmachcustrecord250',
fieldId: 'custrecord265',
// line: i,
value: currentRecord.getValue({
fieldId: 'custevent28'}),
ignoreFieldChange: true
});

How to fix: "INVALID_FLD_VALUE" on entity creating a Sales Order

Im creating a script to create a Sales Order on netSuite, but im getting the fallowing error:
"INVALID_FLD_VALUE","message":"You entered an invalid field value of 10807 for the following field: entity"
What im doing wrong?
There is the Code:
var salesOrder;
var customerid;
var salesOrder = record.create({
type: record.Type.SALES_ORDER,
isDynamic: true
});
var ENTITY_VALUE = 10807;
salesOrder.setValue({fieldId:'entity',value:ENTITY_VALUE})
salesOrder.selectNewLine({
sublistId: 'item'
});
salesOrder.setCurrentSublistValue({
sublistId: 'item',
fieldId: 'item',
value: 1175
});
salesOrder.setCurrentSublistValue({
sublistId: 'item',
fieldId: 'quantity',
value: 1
});
salesOrder.setCurrentSublistValue({
sublistId: 'item',
fieldId: 'rate',
value: objectJson.total_tickets_revenue
});
salesOrder.setCurrentSublistValue({
sublistId: 'item',
fieldId: 'amount',
value: objectJson.total_tickets_revenue
});
salesOrder.setCurrentSublistValue({
sublistId: 'item',
fieldId: 'taxcode',
value: 5
});
salesOrder.commitLine({ //writes the line entry into the loaded record
sublistId: 'item'
});
salesOrder.save({
ignoreMandatoryFields: true,
enableSourcing: false
})
I'm guessing entity 10807 is not a customer.
You'll save yourself some work if you do:
var salesOrder = record.transform({fromType:'customer', fromId:'10807', toType:'salesorder', isDynamic:true});
Please re-activiate entity ID 10807. It's inactive.
Are you using OneWorld or any location/ department restrictions?
Sometimes invalid value errors will be thrown when the role running the script doesn’t have access to the subsidiary that the referenced record belongs to

Suitescript - How to know the fields ID in sub record - Inventory transfer

I'm trying to create a restlet that can create a bin transfer.
My code is almost complete, I can create line item fields also.
The only thing that I can't do is to fill up the inventory detail because I don't know the fields in Subrecords. How can inpect the field id in the sub records line items columns?
define([ 'N/record', 'N/search'], function(r, s) {
function onSendRequst(context)
{
var rec = r.create({
type: context.recordtype,
isDynamic: true
});
rec.setValue( 'location', context.from_location_id );
rec.setValue( 'transferlocation', context.to_location_id );
rec.setValue( 'memo', context.memo );
for( var i = 0; i < context.inventory.length; i++)
{
var inv = context.inventory;
// Create sublist Record
rec.selectNewLine({
sublistId: 'inventory',
line: i+1
});
rec.setCurrentSublistValue({
sublistId: 'inventory',
fieldId: 'item',
value: inv[i].item_ndc
});
rec.setCurrentSublistValue({
sublistId: 'inventory',
fieldId: 'adjustqtyby',
value: inv[i].qty
});
rec.commitLine({
sublistId: 'inventory'
});
// **CREATE A SUB RECORD**
var itemInventorySubrecord = r.getSublistSubrecord({
sublistId: 'item',
fieldId: 'inventorydetail',
line: x
});
}
var recordId = rec.save({
enableSourcing: true,
ignoreMandatoryFields: true
});
return recordId;
}
return {
post: onSendRequst
}
});
You can find the field IDs for inventory detail under the Inventory Detail entry in the Records Browser. Subrecords are available in the SuiteScript Records Browser just like regular records.

itemfulfillment: sublist item field inventorydetail is not a subrecord field

I want to do a bin itemfulfillment in netsuite.
But I can't get an example that works. When I run the below code I am receiving this error:
Sublist item field inventorydetail is not a subrecord field
I need to know what is the correct subrecord name to create an itemfulfillment
Thanks!
var sales_internalid = '2465'; //saleorderid
var einternalid = '110'; //employeeid
var winternalid = '1'; //washsoueid
var sinternalid = '6'; //itemid
var quantity = 1;
var displayname ='iphone';
var shipgroup = 1;
var salesOrder= nlapiCreateRecord('salesorder', sales_internalid, {recordmode: 'dynamic'});
var obj = nlapiTransformRecord('salesorder', sales_internalid, 'itemfulfillment');
obj.selectLineItem('item',1);
obj.setCurrentLineItemValue('item', 'item', sinternalid );
obj.setCurrentLineItemValue('item', 'location', winternalid );
obj.setCurrentLineItemValue('item', 'quantity', 1);
var subrecord= obj.editCurrentLineItemSubrecord('item', 'inventorydetail');
subrecord.selectLineItem('inventoryassignment', 1);
subrecord.selectNewLineItem('inventoryassignment');
subrecord.setCurrentLineItemValue('inventoryassignment', 'inventorynumber', '1');
subrecord.setCurrentLineItemValue('inventoryassignment', 'quantity', '1');
subrecord.commitLineItem('inventoryassignment');
subrecord.commit();
obj.commitLineItem('item');
var fulfillmentOrderId = nlapiSubmitRecord(itemFulfillment, true);
I had this same issue when doing my first Inventory Transfer via code for a client. Turns out the Bin Number field is only a subrecord when the feature "Advanced Bin Management" is enabled. If the account you are working in is setup to just "Use Bins" then the Bin Number field on the line items of transactions is set via its text value. Example is in SS 2.0 but I believe it gets the point across:
newRec = nsRecord.create({
type: nsRecord.Type.INVENTORY_ADJUSTMENT,
isDynamic: true
});
// In dynamic mode must set the subsidiary first.
newRec.setValue({
fieldId: bodyFields.subsidiary,
value: locSub[datain.Location]
});
newRec.setValue({
fieldId: bodyFields.adjustment_Location,
value: datain.Location
});
newRec.setValue({
fieldId: bodyFields.date,
value: date
});
newRec.selectNewLine({
sublistId: columnFields.type
});
newRec.setCurrentSublistValue({
sublistId: columnFields.type,
fieldId: columnFields.item,
value: datain.Item
});
newRec.setCurrentSublistValue({
sublistId: columnFields.type,
fieldId: columnFields.adjust_Qty,
value: datain.Quantity.toString()
});
if (parseFloat(datain.Quantity) > 0) { // If qty is positive must set the Est Unit Cost.
itemVals = nsSearch.lookupFields({
type: nsSearch.Type.ITEM,
id: datain.Item,
columns: fields
});
cost = itemVals.averagecost || itemVals.lastpurchaseprice;
newRec.setCurrentSublistValue({
sublistId: columnFields.type,
fieldId: columnFields.est_Unit_Cost,
value: cost
});
}
//
// Format for binnumbers field is 'ValueText(qty)\rValueText(qty)\rValueText(qty)
// the only exception is in cases of negative qty
//
binText = datain.Bin ? datain.Bin + '(' + datain.Quantity + ')' : '';
newRec.setCurrentSublistValue({
sublistId: columnFields.type,
fieldId: columnFields.bin_Numbers,
value: binText
});
newRec.commitLine({
sublistId: columnFields.type
});
invRecResult.invRecId = newRec.save({
enableSourcing: true,
ignoreMandatoryFields: true
});

Resources