How to get contacts associated in a company if the Contact record has empty Company field in NetSuite - netsuite

I am creating a script that needs to get the contacts associated to a company. The script needs to work for both Customers and Vendors.
You can attach a Contact to a Customer or Vendor from the record screen and by doing this you can attach a Contact with an empty Company field.
I can use the sublist contactroles if it is a Customer record but that sublist is not available for the Vendor record.
Is there a way to get the contact records via search? So using the screenshot above can a search get the following contacts: Aiden Somerhalder, Alex Wolfe and Gerrom Test Contact.

I'd create a saved search of Vendors/Suppliers.
Add a filter to your result: contact : name is not empty
Add a column to your result: contact : name
Add a column to your result: contact : internalId
This should give you a set that your script can iterate through.

You should be able to create a vendor or customer search and the search should return the company/name or more details. Just select as a filter or column:
Contact fields... Company
Perhaps something like this (Running from within the record):
var filters = [];
var columns = [];
filters.push(new nlobjSearchFilter('internalidnumber', null, 'equalto', [nlapiGetRecordId()]));
columns.push(new nlobjSearchColumn('entityid', 'contact', null));
var results = nlapiSearchRecord(nlapiGetRecordType(), null, filters, columns);

Related

SuiteScript getting value from a saved search

I'm having an issue pulling the value of a column in SuiteScript v1.0. The search is looking at Cash Sales and is producing the results I want in the UI, but I am unable to get the value of one column in SuiteScript. I suspect it is because either the value comes from the 'Created From' doc, or because it is a drop down list. Any help would be greatly appreciated.
The search looks at Cash Sales where the Dept/Sales Channel (NS id department) doesn't match the Dept/Sales Channel of the Sales Order. The results are:
Type
Document Number
Created From : Dept/Sales Channel
In the UI, it is doing exactly what I hoped. However, when I loop thru the results in my v1.0 SuiteScript, I'm getting a null value for Dept/Sales Channel:
results.forEachResult(function(res){
var id = res.getId();
var docid = res.getValue('tranid');
var dept = res.getValue('channel');
nlapiLogExecution('DEBUG', 'Found result - '+docid+' ('+id+') - '+dept+'.');
docid and id are correct, but dept ends up being null. I've tried 'channel', 'deptartment' and column[3].value with no luck. What am I doing wrong?
Based on how you formatted this: "Created From : Dept/Sales Channel", I assume it is a joined column.
If it is, you need to do it this way:
var dept = res.getValue('department', "createdfrom");

NetSuite: Need to get value Of Landed Cost Category Line Field using Transaction Saved Search

I am trying to somewhat mimic NetSuite's Landed Cost feature using NetSuite's customization options (SuiteBuilder,SuiteScript etc.) and then further extend the functionality according to my requirements.
For this I need to in script, get value of "LANDED COST CATEGORY" line field of item sublist in the Transaction records (like Bill, Purchase Order etc.) using saved search.
But in a saved search I was unable to find any Column/scriptId which would give me value of LANDED COST CATEGORY line field. We ARE able to get this value using record.load().getValue() but I need this value from multiple transaction records and using this approach may cause performance issues. So, please can you tell how we can access this value using saved search.
I don't believe Netsuite exposes that field in saved searches at this time. This is the records browser in Netsuite listing all of the available search columns for Transaction searches. The internal id for that column is landedcostcategory, and that doesn't show up on the list.
However, if your goal is to get this information in SuiteScript, then you can use the 'N/query' module. Pull up one of your Purchase Orders, open the Javascript console (Ctrl+Shift+J) and try this:
require(['N/query'], (query) => {
const suiteqlQuery = `SELECT
transaction as transaction_id,
BUILTIN.DF(transaction) as transaction_name,
BUILTIN.DF(item) as item_name,
item as item_id,
landedcostcategory as landedcostcategory_id,
BUILTIN.DF(landedcostcategory) as landedcostcategory_name
FROM
transactionline
WHERE
transaction='<internal id of your PO here>'`;
const results = query.runSuiteQL({query: suiteqlQuery}).asMappedResults();
console.log(JSON.stringify(results, null, 2));
/*
Example output for results:
[
{
"transaction_id": "12345",
"transaction_name": "Purchase Order #PO123456",
"item_name": "My Favorite iPod",
"item_id": 1234,
"landedcostcategory_id": 1,
"landedcostcategory_name": "Duties & Tariffs"
}
]
*/
})

How do I do a joined lookup with search.lookupFields()?

I'm trying to get some information about an item, including the item's subsidiary's logo, which naturally requires joining the item to the subsidiary.
The documentation for search.lookupFields says:
You can use joined-field lookups with this method, with the following syntax:
join_id.field_name
So, I duly request the fields I want, including a join on subsidiary:
require(['N/search'], function(search) {
var item = search.lookupFields({
type: search.Type.ITEM,
id: 2086,
columns: ['itemid', 'displayname', 'subsidiary.logo'],
});
log.debug(item);
});
itemid and displayname are fine, but when I try to join another record I get this error:
{
"type":"error.SuiteScriptError",
"name":"SSS_INVALID_SRCH_COLUMN_JOIN",
"message":"An nlobjSearchColumn contains an invalid column join ID, or is not in proper syntax: logo.",
"stack":["doLookupFields(N/search/searchUtil.js)","<anonymous>(adhoc$-1$debugger.user:2)","<anonymous>(adhoc$-1$debugger.user:1)"],
"cause":{
"type":"internal error",
"code":"SSS_INVALID_SRCH_COLUMN_JOIN",
"details":"An nlobjSearchColumn contains an invalid column join ID, or is not in proper syntax: logo.",
"userEvent":null,
"stackTrace":["doLookupFields(N/search/searchUtil.js)","<anonymous>(adhoc$-1$debugger.user:2)","<anonymous>(adhoc$-1$debugger.user:1)"],
"notifyOff":false
},
"id":"",
"notifyOff":false,
"userFacing":false
}
This seems to happen no matter which record and field I try to join. What am I missing?
Although you can return results from multi-select fields, you cannot join to fields on records referenced by multi-select fields (which the subsidiary field on the item record is). Also, you cannot search the logo field on the subsidiary record (not listed in Search Columns under Subsidiary in the NetSuite Records Browser).
This means you have to load the Subsidiary record to get the logo field. In other words:
require(['N/record', 'N/search'], function(record, search) {
var item = search.lookupFields({
type: search.Type.ITEM,
id: 2086,
columns: ['itemid', 'displayname', 'subsidiary'],
});
var subID = item.subsidiary[0].value; //internal id of *first* subsidiary
var subRec = record.load({
type: record.Type.SUBSIDIARY,
id: subID
});
var logo = subRec.getText('logo'); //gets the file name - use getValue to get its ID instead
});
Note that if multiple subsidiaries are set on the item, this only gets the values for the first one. You could iterate through the item.subsidiary result to handle values for multiple subsidiaries if required.
I believe you can't access to the subsidiary record from a lookupfield, you should do a proper search.
https://system.netsuite.com/help/helpcenter/en_US/srbrowser/Browser2018_2/script/record/item.html
You can only join to tables allowed in the Item search object. Try looking for "Subsidiary..." in the Search Results tab within the UI. It's not there. Use the Schema Browser to determine what fields and joins are available.
You cannot think of a NetSuite search as you would any regular SQL search. You have to be cognizant of which fields and which joins can be utilized via the search object.
As people have mentioned, the subsidiary is not a join field available from the item record, one way to achieve what you are trying to do is:
Make a lookup to get the internal id of the subsidiary belonging to the desired item.
Then make a lookup to get the internal id of the logo image (file cabinet image) belonging to the previous subsidiary.
Make another lookup/load the image file to get the URL of the image/logo
You can try to combine the above steps in a single saved search but I think you might need to load the image file to get the URL.
This won't answer your question, but this may help out in the future. The records browser shows everything that you can search and join on, columns and filters, and field IDs. Very useful when building out searches.
NetSuite Records Browser - 2018.2

SuiteScript Access Custom Field in Sublist

I have added two fields to the contact sublist record of customers. Two boolean values. I checked the values to make sure that they would show but in code cannot access those values in SS 1 or SS 2 through a sublist line item, I am accessing like this "var statements = rec.getLineItemValue( 'contactroles', 'custentity_statements', "1" );". When I look in the object for the customer record and look in the "contactroles" sublist, I cannot see those columns. If I load the contact record in SS 1 I can see the columns. Any help with this would be great, I would like to do it in SS 2 but I am flexible, thanks
Not sure whether this helps, but you can retrieve the contact id, then load the contact record and check the field value. This is assuming the custom field showing in the contact sublist is a contact field. The documentation isn't too swift, but it looks as though not all sublist fields are supported for getFieldValue. This is in SS1, but if it does what you want we should be able to take the same idea and write it in SS2.
var rec = nlapiLoadRecord('customer','11499');
var conId = rec.getLineItemValue('contactroles', 'contact', 1);
var con = nlapiLoadRecord('contact',conId);
var statements = con.getFieldValue('custentity9');
console.log(statements); //T

NetSuite - how to create reference link

When the billing schedule runs it auto generates invoices from sales order. When this happens - how can I create a link on the sales order that will allow me to load the corresponding invoice in code?
I need this so I can grab couple of field values from the invoice but I can't access the invoice directly from another entity which seems only related to sales order.
EDIT 1:
var fil = [];
fil[0] = new nlobjSearchFilter('createdfrom', null, 'is', nlapiGetRecordId())
var col = [];
col[0] = new nlobjSearchColumn('internalid');
var invoices = nlapiSearchRecord('invoice', null, fil, col);
nlapiLogExecution('DEBUG', 'field val', invoices);
Throws invalid operator or not in proper syntax: createdfrom.
Though adding a link on the Sales Order is a viable solution, it's not your only option. Alternatively, you could do a search for invoices where the createdfrom field is the internal ID of your Sales Order. Something like in SuiteScript 1.0:
var invoices = nlapiSearchRecord('invoice', null,
[['createdfrom', 'is', nlapiGetRecordId()]],
[/* create search columns for the fields you need off the invoice */]
) || [];
or in 2.0:
var invoices = search.create({
"type": search.Type.INVOICE,
"filters": [['createdfrom', 'is', context.currentRecord.id]],
"columns": [/* create search columns for the fields you need off the invoice */]
}).run().each(processResult);
This will get you a list of all the Invoices created from your Sales Order (which is likely only 1).
If you believe you need a link to the Invoice on the Sales Order, you could add the custom body field, then create a User Event on the Invoice record that populates this new field with its createdfrom value on the Before Submit event. But then what happens if your Sales Order gets paid via multiple Invoices?

Resources