I'm trying to set an address on a customer record. I've grasped that the address is a subrecord of a customer sublist and I believe I'm able to set fields on said subrecord, but I'm unable to get the changes to save. How can one set address information on a Customer using SuiteScript 2.0?
Current code:
customer.selectNewLine({
sublistId: 'addressbook'
});
var addressSubrecord = customer.getCurrentSublistSubrecord({
sublistId: 'addressbook',
fieldId: 'addressbookaddress'
});
subrecordAddressDetail.setValue({
fieldId: 'addr1',
value: 'Test Street'
});
subrecordAddressDetail.setValue({
fieldId: 'country',
value: 'US'
});
customer.commitLine({
sublistId: 'addressbook'
});
I've also tried adding customer.save() after .commitList, but I get the error Record has changed when I try to do so.
The part I was missing was that you need to save the parent record for any changes to a subrecord of a sublist to take effect. I was unable to save said parent record because I had made changes to that record and saved them previously which caused the Record has already changed error.
Generally addresses can be added to customer records by:
Selecting a line in the addressbook sublist.
Retrieving the address subrecord of the currently selected line.
Updating the subrecord.
Committing the selected line in the addressbook sublist.
Saving the parent record.
For example this will update the first address for a given customer or create it if it doesn't already exist:
function updateAddress(customer, address) {
var currentAddressCount = customer.getLineCount({
'sublistId': 'addressbook'
});
if (currentAddressCount === 0){
customer.selectNewLine({
sublistId: 'addressbook'
});
} else {
customer.selectLine({
sublistId: 'addressbook',
line: 0
});
}
var addressSubrecord = customer.getCurrentSublistSubrecord({
sublistId: 'addressbook',
fieldId: 'addressbookaddress'
});
// Set all required values here.
addressSubrecord.setValue({
fieldId: 'addr1',
value: address.line_one
})
customer.commitLine({
sublistId: 'addressbook'
});
customer.save()
}
I think you need to treat the address as a proper record. Therefore, after you retrieve it, set its values and then commit it separately. Something along these lines:
customer.selectNewLine({
sublistId: 'addressbook'
});
var addressSubrecord = customer.getCurrentSublistSubrecord({
sublistId: 'addressbook',
fieldId: 'addressbookaddress'
});
addressSubrecord.setValue({
fieldId: 'addr1',
value: 'Test Street'
});
addressSubrecord.setValue({
fieldId: 'country',
value: 'US'
});
addressSubrecord.save()
customer.commitLine({ //probably not necessary since address is already updated
sublistId: 'addressbook'
});
Related
Thanks for clicking in.
On the Customer record within the Address subtab I have a sublist with addresses.
On each line there's a button for edit. (See first picture)
Clicking edit will bring up a popup window with some fields that can not be seen in Field Explorer. It is one of these fields I would really like to retrieve the value from. (See second picture)
See the third picture to see how the record of the field I want to retrieve looks.
I've tried to both load the customer record and to use lookupFields. But the field I want can't be accessed through just the customer record. Looking at the field record it says that it is for record of type Address. But it says it's a subrecord in record browser, and I don't really know how to access it.
Do I need to create a search in order to get to it? Or is a better way to use SuiteQL?
I'm still a bit new to NetSuite and haven't really done anything with SuiteQL yet.
So any help and guidance is very much appreciated.
Address Sublist
Popup
Field
There are different options.
When you loaded the customer record, you should load the address sublistrecord to access that value.
define(['N/record'], function(record) {
var myRecord = record.load({
type: record.Type.CUSTOMER,
id: INTERNALID_OF_YOUR_CUSTOMER
})
for (var line = 0; line < myRecord.getLineCount({ sublistId: 'addressbook' }); line++) {
var address = myRecord.getSublistSubrecord({ sublistId: 'addressbook', line: line, fieldId: 'addressbookaddress' })
var accountNo = address.getValue({ fieldId: FIELDID_OF_YOUR_CUSTOM_FIELD })
}
})
Use the N/search module
define(['N/search'], function(search) {
var mySearch = search.create({
type: search.Type.CUSTOMER,
columns: [
'internalid',
'entityid',
'companyname',
{ join: 'Address', name: 'city' },
{ join: 'Address', name: FIELDID_OF_YOUR_CUSTOM_FIELD },
],
filters: [ 'internalid', search.Operator.IS, INTERNALID_OF_YOUR_CUSTOMER ]
})
mySearch.run().each(function(result) {
...DO SOMETHING
return true;
})
})
You need to load the subrecord for getting the values of address.You can use API recordObj.getCurrentSublistSubrecord.
Here is the code how i have used to get the values:-
1- First load the customer record in script.
var customerObj = record.load({
type: record.Type.CUSTOMER,
id: customerRecordId,
isDynamic:true
});
var addressSubrecord = customerObj.getCurrentSublistSubrecord({
sublistId: 'addressbook',
fieldId: 'addressbookaddress'
});
addressSubrecord.getValue({
fieldId: 'addr1',
value: linedata1.address1
});
addressSubrecord.getText({
fieldId: 'country'
});
addressSubrecord.getValue({
fieldId: 'addr2'
});
addressSubrecord.getValue({
fieldId: 'zip'
});
2-Put inside for loop if it is according to line using API and add line as well.
var numLines = objRecord.getLineCount({
sublistId: 'addressbook'
});
You can get all the data using their field id.
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.
I have tried everything to get this working but no matter what I do, I always get the following error from NetSuite:
error code: USER_ERROR error message: {"type":"error.SuiteScriptError","name":"USER_ERROR","message":"Please enter value(s) for: Serial/Lot Number","stack":["anonymous(N/serverRecordService)"...
This occurs when I try to commit the line.
What I am attempting to do (in a RESTlet) is programatically create a Work Order Completion (WOC) for an existing Work Order (WO). I have it working for the first operation. However, for the final operation, which requires an Inventory Detail, I am getting the error. Here is my code (I have removed error checking, etc for better clarity):
/**
* #NApiVersion 2.x
* #NScriptType restlet
*/
define(['N/runtime', 'N/error', 'N/record', 'N/search'], function(runtime, error, record, search) {
return {
get : function(datain) {
var wo = record.load({
type: record.Type.WORK_ORDER,
id: datain.woid
});
var woc = record.transform({
fromType: record.Type.WORK_ORDER,
fromId: datain.woid,
toType: record.Type.WORK_ORDER_COMPLETION,
isDynamic: true,
defaultValues: {
isbackflush: 'F',
}
});
woc.setText({
fieldId: "startoperation",
text: "20"
});
woc.setText({
fieldId: "endoperation",
text: "20"
});
woc.setText({
fieldId: "completedquantity",
text: "230"
});
var invdetail = woc.getSubrecord({
fieldId: "inventorydetail"
});
invdetail.selectNewLine({
sublistId: "inventoryassignment",
});
invdetail.setCurrentSublistValue({
sublistId: "inventoryassignment",
fieldId: "binnumber",
value: 29
});
invdetail.setCurrentSublistValue({
sublistId: "inventoryassignment",
fieldId: "quantity",
value: 230
});
invdetail.setCurrentSublistText({
sublistId: "inventoryassignment",
fieldId: "issueinventorynumber",
text: "abc"
});
invdetail.commitLine({
sublistId: "inventoryassignment",
});
woc.save();
var results = {
woc: woc
};
return JSON.stringify(results);
}
}
});
I have tried everything I can think of. Using setCurrentSublistValue() and setCurrentSublistText(), setting the value to an existing lot, new lot, etc, etc.
However, no matter what I do, NS always responds with the "Please enter value(s) for: Serial/Lot Number" error as if it is not set.
Thoughts, ideas, suggestions???
Thanks!
Try using receiptinventorynumber for your fieldId.
invdetail.setCurrentSublistValue({
sublistId: 'inventoryassignment',
fieldId: 'receiptinventorynumber',
value: 'lotNumber'
});
invdetail.commitLine({sublistId: 'inventoryassignment'});
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.
I keep getting the following error when trying to create a new Inbound Shipment record (new in Netsuite).
{"type":"error.SuiteScriptError","name":"USER_ERROR","message":"Please enter value(s) for: Item","stack":["anonymous(N/recordService)","<anonymous>(/SuiteScripts/KK_Sandbox_Scripts_SD/RestLets/InboundShipment.js:132)","<anonymous>(/SuiteScripts/KK_Sandbox_Scripts_SD/RestLets/InboundShipment.js:99)","doPost(/SuiteScripts/KK_Sandbox_Scripts_SD/RestLets/InboundShipment.js:34)"],"cause":{"type":"internal error","code":"USER_ERROR","details":"Please enter value(s) for: Item","userEvent":null,"stackTrace":["anonymous(N/recordService)","<anonymous>(/SuiteScripts/KK_Sandbox_Scripts_SD/RestLets/InboundShipment.js:132)","<anonymous>(/SuiteScripts/KK_Sandbox_Scripts_SD/RestLets/InboundShipment.js:99)","doPost(/SuiteScripts/KK_Sandbox_Scripts_SD/RestLets/InboundShipment.js:34)"],"notifyOff":false},"id":"","notifyOff":false}
Here is the code I have to set the record. I have verified that the sublist id is "items" and the fieldId is "itemid", but when I try to add the line, I get the above error.
function doPost(restletBody){
log.debug('Called from POST', restletBody);
var success = [],
errors = [];
restletBody.data.forEach(function(e)
{
try
{
log.debug('Called from POST', e.id);
//Create a new record
var rec =
r.create({
type: "inboundshipment",
isDynamic: true,
defaultValues: null
}).setValue({
fieldId: "externalid",
value: e.id,
ignoreFieldChange: false
});
var i = 1;
e.lineitems.items.forEach(function(item)
{
log.debug("Inside Line Items", item.itemid + " " + item.purchaseorder);
rec.selectNewLine({sublistId:"items"});
var field = rec.getSublistFields({
sublistId: 'items'
});
rec.setCurrentSublistValue({
sublistId: "items",
fieldId: "purchaseorder",
value: item.purchaseorder,
ignoreFieldChange: true
});
rec.setCurrentSublistValue({
sublistId: "items",
fieldId: "itemid",
value: item.itemid,
ignoreFieldChange: true
});
log.debug("Inside Line Items", "ITEM Added " + item.itemid );
rec.commitLine({sublistId:"items"});
log.debug("Inside Line Items", "Line Committed" );
++i;
});
rec.save();
}
catch(err)
{
var msg = '';
log.debug("There was an error", err);
}
})
return "";
}
I have tried changing the itemid to shipmentitem and id, and it doesn't work. Before, I was getting an error for PO, too, and it was because the name was wrong, but I fixed that (I believe) for the item.
One thing that might be affecting it (not sure) is that the PO list filters the item list. once the user selects the PO in the UI, it will filter the item list to only items on that PO. I have double-checked that the item I am trying to submit belongs to the PO I am trying to submit, but still receiving an error. Any ideas?
According to the NetSuite Record Browser, the field should be 'shipmentitem'.
rec.setCurrentSublistValue({
sublistId: "items",
fieldId: "shipmentitem",
value: item.itemid,
ignoreFieldChange: true
});
EDIT: Updated to reflect that the fieldname should be 'shipmentitem' as #shawleigh17 discovered so future users find the correct answer.
all you need is a PO line unique Id (line field id: lineuniquekey) of each from original PO/POs and transaction Id. This id you will set for shipmentitem sublist field of Inbound Shipment record.
objRecord.setCurrentSublistValue({
sublistId: 'items',
fieldId: 'purchaseorder',
value: objParam.purchaseOrderId,
forceSyncSourcing: true
});
objRecord.setCurrentSublistValue({
sublistId: 'items',
fieldId: 'shipmentitem',
value: objParam.lineUniqueIdFromPO,
forceSyncSourcing: true
});
and of course, do not forget to set quantity for quantityexpected