I am using SuiteScript 2.0 in Netsuite. The list of countries are available in
Setup > Company > Countries. As of current version, I have not found a way to get list of Countries and State/Province drop downs added in custom Suitelets.
How Can I access this list from Netsuite Suitescripts?
There could be a more direct way to do this, but you can begin creating a customer record, access the address object and get the select options that are available.
var custRec = record.create({ type: record.Type.CUSTOMER, isDynamic: true });
var custSubrec = custRec.getCurrentSublistSubrecord({ sublistId: 'addressbook', fieldId: 'addressbookaddress' });
var countryFieldObj = custSubrec.getField({ fieldId: 'country' });
var countryList = countryFieldObj.getSelectOptions();
There is no module or enumeration anywhere for this. SuiteScript uses the ISO-standard two-letter (ALPHA-2) country codes. http://www.nationsonline.org/oneworld/country_code_list.htm
Create a Saved Search for customer Record named customsearch_customercountry where criteria is empty and Results contains the fields you want (including country by example). The saved search will be loaded and include the filter using the customerId as variable taken or filled from your code previous sentences.
the script will be as:
var country_search = search.load({
id: "customsearch_customercountry",
});
var filterArray = [];
filterArray.push(["internalid", "is", customerId]);
country_search.filterExpression = filterArray;
var searchResult = country_search.run().getRange({
start: 0,
end: 1,
});
log.debug({
title: "Execute Search ",
details: "ResultSet length: " + searchResult.length,
});
if (searchResult.length > 0) {
var country = searchResult[0].getText({
name: "Country",
});
log.debug({
title: "Execute Search ",
details: "ResultSet found with country: " + country,
});
}
NOTE: you must use the .getText() function to retrieve the text value for the country Result. If you only need the Country code, use .getValue() instead of
Related
I am currently watching a lot of tutorials and doing a lot of research on SuiteScript 2.0
But before I get into this I want to ask a fundamental question.
Can I edit a field on a particular record type with a script that runs on a different record type.
For example
I have a custom field on my Inventory Item Page. (Item Record)
I want to run a script from Goods Receipt Note (Transaction Record)
So that when the goods receipt note is saved it amends a custom field on the item page.
Is this possible?
Thank you
Generally not a problem as long as the role the script is being run under has write access to the item.
The main thing is transactions can tend to have many lines and if you are updating your items one-by-one you can run into script governance issues.
function beforeSubmit(context) {
var new_rec = context.newRecord;
var item_count = new_rec.getLineCount({
sublistId: "item"
});
for (let i = 0; i < item_count; i++) {
var item_id = new_rec.getSublistValue({
sublistId: 'item',
fieldId: 'item',
line: i
});
var avgCost = search.lookupFields({
type: "inventoryitem",
id: item_id,
columns: ['averagecost']
}).averagecost;
var updateAvgCost = record.submitFields({
type: record.Type.INVENTORY_ITEM,
id: item_id,
values: {
custitem1: Number(avgCost).toFixed(2)
}
});
}
I am trying to update the date of a field if "item_id is 900",
define(['N/record', 'N/search'],
function(record, search){
function salesorderbeforeload(context){
var currentRecord = context.newRecord;
var sublistName="item";
var start_Date="custcol_atlas_contract_start_date";
var end_date="custcol_atlas_contract_end_date";
var lines = currentRecord.getLineCount({sublistId: 'item'});
log.debug({title:'lines',details:lines});
for (var i = 0; i < lines; i++) {
var itemId = (currentRecord.getSublistValue({
sublistId: "item",
fieldId: "item",
line: i
}));
var value=itemId;
if(value=="900")
{
currentRecord.setSublistValue({
sublistId: "item",
fieldId: "custcol_atlas_contract_start_date",
line: 0,
value: "2021-09-09T07:00:00.000Z"
});
}
}
}
return{
beforeLoad: salesorderbeforeload
};
}
);
The above code will fetch the line item id as value , and if the value is 900 it will set the date to the following
Evrything is working perfectly except for this code,
currentRecord.setSublistValue({
sublistId: "item",
fieldId: "custcol_atlas_contract_start_date",
line: 0,
value: "2021-09-09T07:00:00.000Z"
});
record=currentRecord.setSublistValue({
sublistId: "item",
fieldId: "quantity",
line: 0,
value: "3"
});
Even the quantity is not updating.
Thanks in advance
You cannot update existing records so that ultimately will be your issue.
You may be able to fix this in a before submit event when the record is saved or in a Client Event script.
As far as the code supplied though you are not advancing the line index. You should have:
currentRecord.setSublistValue({
sublistId: "item",
fieldId: "custcol_atlas_contract_start_date",
line: i, // NOT 0
value: "2021-09-09T07:00:00.000Z"
});
but after that is your custom field an actual date time or is it a text field?
if you are trying to store a date you'll need to parse your ISO formatted date string to an actual date. This is different from SS1 where dates had to be date formatted strings.
As far as item groups go the custom field needs to be configured to save the value for group items.
There is a checkbox, "Store with Item Groups" on the field definition
Did you try saving the record after all the updates?
You can try this approach where you load the record first, then check for your conditions, accordingly update the fields and then perform a record save.
var objRecord = record.load({
type: record.Type.SALES_ORDER,
id: 157,
isDynamic: true,
});
Then you can iterate over the line items. Use FOR LOOP
for(var j=0; j<numLines; j++) {
//fetch the item_id using getCurrentSublistValue
if(item_id == 900) {
//Perform the setSublistValue
}
}
And finally,
objRecord.save();
Give this a try, even I'm not sure of this approach. Let me know in case of any issues.
I have a Suitelet that returns a form page that a user can specify data on, and when it’s retrieved, the default values are populated based on the data the user entered the previous time.ie. Data is saved on and sourced from a Custom Record.
I decided to add an Inline Editor Sublist (don't know what I was sniffing).However I haven’t been able to populate data onto the Sublist when the Suitelet is retrieved.I am aware it was possible in SS 1.0, but I refuse to use 1.0 😉
Basically the question is: When adding an Inline Editor Sublist to a Suitelet, how can I then populate it with data?
Please create a Client script for your Suitelet and the client script as a reference in your suitelet as given example. In your client Script on pageInit Function run a saved search and populate the Sublist.
Client Script:
var rec = context.currentRecord;
var objSublistSearch = search.load({ id: 'customsearch11208' });
var filter = search.createFilter({ name: 'custrecordzab_s_customer', operator: search.Operator.ANYOF, values: customerid }); objSublistSearch.filters.push(filter);
var sublist_count = 0 objSublistSearch.run().each(function(result) {
var internalid = result.getValue('internalid');
rec.setCurrentSublistValue({ sublistId: 'custpage_contractsublist', fieldId: 'custpage_internalcontractid', value: internalid, line: sublistCount, ignoreFieldChange: true });
rec.commitLine({
sublistId: 'custpage_contractsublist'
});
sublist_count++;
}
I have a form being created under the GET method for a Suitelet I created. Then, in the POST portion of the code, I am attempting to retrieve a free-form text field value off of the relevant form. How can this be done? I attempted to get the value in such a way, but apparently it cannot be gotten off of the form object, however, I am not sure how this is done in SuiteScript as the tutorial I went through on SuiteScripts did not cover how to retrieve the Suitelet values in SuiteScript 2.0.
if (request.method == 'GET'){
var form = serverWidget.createForm({
title: 'Sales Order Update'
});
var financingPriceField = form.addField({
id: 'custpage_sdr_financing_price',
type: serverWidget.FieldType.TEXT,
label: 'Financing Price'
});
var submitButton = form.addSubmitButton({
label: 'Save SO Data'
});
response.writePage(form);
}
else // If POSTing
{
var salesOrder = record.load({
type: record.Type.SALES_ORDER,
id: 9976 // Using hard-coded id for testing only
isDynamic: true
});
// This portion of the code is failing to get any value
// When attempting to do so will result in a TypeError
// 'Cannot call method 'getValue' of undefined
var financingPrice = form.getValue('custpage_sdr_financing_price');
// Will save sales order and copy value to SO in code below, not shown in example
}
For SS1.0 you get the value from the request
request.getParameter('custpage_sdr_financing_price');
There is a js error When I use the removeSelectOption to remove all of the dropdown list.The code as follow:
function fieldChanged(scriptContext) {
if(scriptContext.fieldId == 'class'){
var currentRecord = scriptContext.currentRecord;
var brand_id = currentRecord.getValue({fieldId:'class'});
if(brand_id){
var itemList = [];
var itemField = currentRecord.getField({
fieldId: 'item'
});
itemField.removeSelectOption({
value: null,
});
search.create({
type: search.Type.INVENTORY_ITEM,
columns:[{name:'internalid'}],
filters:[
['custitem30',search.Operator.ANYOF,brand_id]
]
}).run().each(function(result){
item_id = result.getValue({
name:'internalid'
});
var in_item = record.load({
type: record.Type.SALES_ORDER,
id: item_id,
});
itemList.push(item_id);
itemField.insertSelectOption({
value: item_id,
text: in_item
});
console.log('item_id:' + item_id);
});
console.log('itemList:' + itemList);
}
}
}
And the api document in help center said -- To remove all options from the list, set this field to null, as follows:
field.removeSelectOption({
value: null,
});
If I want to remove all options from the dropdown list,what should I do?
Thanks.
Create a item saved search
2.Transaction form Item Filter field select that Saved Search.
As your question i assumed that you only show inventory item in item Field.
First of all you have get the field object
var field = currentRecord.getField({
fieldId: 'Your Field ID'
});
then use this below code
field.removeSelectOption({
value: null,
});
you cannot remove the field items from the list that are created from UI and have a source.
you can have a alert for it on field change