Netsuite SS 2.0 - Populating Sublist data on Suitelet - netsuite

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++;
}

Related

Scripts to change values across different record types - Netsuite Suitescript

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)
}
});
}

What is the recommended method for running SuiteScripts on imported Web Services transactions?

I currently run a User Event script with a Before Submit function that populates custom column fields for every transaction. Because it's a User Event script, it doesn't fire on transactions imported through Web Services, like POS Invoices.
Edit: Per the comment below, this is a misunderstanding on my part. User Event scripts do run on Web Services.
What's the best kind of script to use if I want to modify custom fields on every imported transaction? Would a Workflow Action script triggered on After Record Submit work? Could that be problematic if I'm importing several transactions at once?
If there isn't a good way to do this when the transaction is imported, I'll probably just run a Scheduled or Map/Reduce script to update these records after the fact.
Below is the UE script. It looks at each line in a transaction, starting at the bottom, to add discount information to the line above it, if applicable.
/**
*#NApiVersion 2.0
*#NScriptType UserEventScript
*/
define([], function() {
return {
beforeSubmit : function(context) {
// get record and line count
var currentRecord = context.newRecord;
var count = currentRecord.getLineCount({
sublistId:'item'
});
// init discount amount
var discountAmount = 0;
// for each line, starting from the bottom
for(var i = (count-1); i >= 0; i--) {
// fetch data
var type = currentRecord.getSublistValue({
sublistId: 'item',
fieldId: 'itemtype',
line: i
});
var amount = currentRecord.getSublistValue({
sublistId: 'item',
fieldId: 'amount',
line: i
});
var quantity = currentRecord.getSublistValue({
sublistId: 'item',
fieldId: 'quantity',
line: i
});
if (type == 'Discount') {
// add to current discount amount if discount
discountAmount += amount;
} else {
// parse data
amount = parseFloat(amount);
quantity = parseInt(quantity);
discountAmount = parseFloat(discountAmount);
// set variables
calculatedAmount = (amount + discountAmount).toFixed(2);
calculatedRate = ((amount + discountAmount) / quantity).toFixed(2);
calculatedDiscount = (discountAmount).toFixed(2);
// update sublist
currentRecord.setSublistValue({
sublistId: 'item',
fieldId: 'custcol_calculated_amount',
line: i,
value: calculatedAmount
});
currentRecord.setSublistValue({
sublistId: 'item',
fieldId: 'custcol_calculated_rate',
line: i,
value: calculatedRate
});
currentRecord.setSublistValue({
sublistId: 'item',
fieldId: 'custcol_calculated_discount',
line: i,
value: calculatedDiscount
});
log.debug('discountAmount', discountAmount);
// reset current discount amount
discountAmount = 0;
}
}
}
}
})
Because it's a User Event script, it doesn't fire on transactions imported through Web Services
This is not how it works. Your assumption above is probably made based on confusing script type name which might be accidentally identified as it only triggers when the record is created from User Interface by the User. But, in reality, the documentation outlines, the User Event script triggers when:
...The client request can come from the user interface, SOAP web services, server–side SuiteScript calls, CSV imports, or XML. ....
So, probably, there is something in your script that just filters out the requests that go from WebServices. Can you share the code please to have more details on this?

How to set inventory details when creating item receipt with client script 2.0?

I tried to get sublist subrecord from line item of item receipt record, but its return null.
/**
*#NApiVersion 2.x
*#NScriptType ClientScript
*/
define(['N/error', 'N/currentRecord'],
function (error, currentRecord) {
function pageInit(context) {
var rec = currentRecord.get();
rec.selectLine({ sublistId: 'item', line: 0 });
var inventoryDetailSubRecord = rec.getCurrentSublistSubrecord({sublistId: 'item', fieldId: 'inventorydetail' });
// inventoryDetailSubRecord object comes up with null.
}
return {
pageInit: pageInit
};
}
);
inventoryDetailSubRecord object should come up with inventory detail subrecord.
Client scripts can read subrecords, but they can't write to them. getCurrentSublistSubrecord creates the sub-record if it doesn't exist.
From the docs;
A client script may not create subrecords on the current record and is limited to read-only access of existing subrecords on the current record. The client script may remove the subrecord from the current record.

How to retrieve values off of SuiteScript 2.0 Suitelet for POST?

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');

How to access Country list in Netsuite?

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

Resources