How to load invoice record in SuiteScript? - netsuite

require(['N/search','N/record'])
var search = require('N/search')
var record = require('N/record')
var invoice = record.load({
type: record.Type.INVOICE,
id: 444283
})
log.debug('invoice',invoice)
Hi everyone can someone heelp me figure out how to properly load invoice records, vendor bills/credits ? i try this simple record.load with this enum type and i get errors of:
"name":"INVALID_TRANS_TYP","message":"Transaction type specified is incorrect.",

That error would seem to indicate that the Transaction with Internal ID 444283 is not actually an Invoice. Verify your internal ID and transaction type match up.

Related

Netsuite email template - how to get child values from custentity field?

guys!
I'm writing an email template for the invoices. On the invoice form I have a "Project" (internalId: Job) field -> on the "Project" form I have a "custom entity" field with employee-type. I can get an employee's name with ${transaction.job.custentity5}. But I can't get access to related fields such as email, phone number and etc. The code ${transaction.job.custentity5.email} gives me nothing. The code ${transaction.job.custentity5.mobilephone} gives me a strange error like "field job.mobilephone not found" (netsuite hides custentity5 in this objects chain), but I see this field in employee's profile.
How do i get child values from custentity field?
Unfortunately, you can't go into such a deep level with the standard data provided.
You can however fetch the data with a search.lookupFields during a beforeLoad and set it as default value on a custom field of the form.
/**
* #NApiVersion 2.x
* #NScriptType UserEventScript
*/
define(['N/ui/serverWidget', 'N/search'], function(serverWidget, search) {
function beforeLoad(context) {
var invoice = context.newRecord;
var form = context.form;
var type = context.type;
var UserEventType = context.UserEventType;
// only execute during printing...
if (type != UserEventType.PRINT) return
var jobID = invoice.getValue({fieldId: 'job'});
// return when no job/project is set on the invoice...
if (!jobID) return
var job = search.lookupFields({
type: search.Type.JOB,
id: jobID,
columns: ["custentity5"]
})
// return when no employee is set on the project...
if (!(job.custentity5 && job.custentity5[0] && job.custentity5[0].value)) return
var employee = search.lookupFields({
type: search.Type.JOB,
id: job.custentity5[0].value,
columns: ["email", "phone"]
})
var field = form.addField({
id : 'custpage_custom_data_employee',
label: 'Employee',
type : serverWidget.FieldType.LONGTEXT
});
field.defaultValue = JSON.stringify(employee);
}
return {
beforeLoad: beforeLoad
};
})
You can access the data within the template through:
<#if record.custpage_custom_data_employee?has_content>
<#assign employee = record.custpage_custom_data_employee?eval />
</#if>
Netsuite doesn't drill down that far for you. Generally you get one step away from the primary record so you see a name using ${transaction.job.custentity5} because you'd see a name if you opened the associated job record.
How you can show the details from custentity5 depends on a bunch of factors like who is sending this email? Is is special enough to have its own button? Is the email being sent batch etc.
Options:
A before load user event script can check whether the record is being printed or emailed the standard way. That script can load and populate custom fields on the main record so you may be able to reference ${transaction.custpage_ent5.mobilephone} where you'd added the job's custom entity to the transaction
fully script your email using n/Render. You'll likely need to script everything but you can look up and add arbitrary records and datastructures to your renderer. This would be triggered by a custom button or batched script.

NetSuite SuiteScript 2.0 get Company Name

Is there any way to get the current company name when on a transaction entry page? Either some session object, global object, or some sort. Kind of like how you can get the user name from the runtime module? I see country name, but cannot seem to get the company name. Using a client script for this.
How I get the user name:
runtime.getCurrentUser().name
thank you
Here is the working code now, but trying to figure a way to get this on a client script. Possible to pass this value?
var companyInfo = config.load({
type: config.Type.COMPANY_INFORMATION
});
var compname = companyInfo.getValue({
fieldId: 'companyname'
});
log.debug(compname);
scriptContext.form.addButton({
id: "custpage_mybutton",
label: "View Documents",
functionName: "onButtonClick"
});
scriptContext.form.clientScriptModulePath = "SuiteScripts/dl_cs_vendorbill.js";
In server side scripts you can do this:
require(['N/config'], config=>{
const info = config.load({type:config.Type.COMPANY_INFORMATION});
log.debug({title:'Current company Info', details:info.getValue({fieldId:'companyname'}));
});
For a client script you could use that code in a paired user event script.
In the before phase of the UE you'd do the lookup and then add a hidden field to the form with the company name as a value. Your client script can then look that up like any normal getValue

Can you make the id field the external id in Netsuite?

I have the id field on a netsuite object. Can I change use the id column as the external id in Netsuite, something like in Salesforce?
You can definitely write a user event script to populate the internal id of any record to it's external ID field. The "externalid" field is exposed via SuiteScript or via csv import
The internal id is a unique auto-generated number which identifies a unique NetSuite record and hence the sequence would change if deletion/inactivation happens For eg.if there are records with internal Ids 1, 2 and 3 in system and 2 gets deleted, then IDs that remain in the system are 1 and 3. User does not have the ability to alter these internal Ids since they are system generated. Hence, they cannot be customized (acc to developer's preference) specially if you want to follow a specific convention.
External Id should contain a value which should be unique across multiple integrated systems.Eg. SFDC (Salesforce). The recommended way is to write an afer submit user event script to populate external ID on all the integrated records and keep these synced in both systems
Here is a sample code in SuiteScript 2.0 which populated external ids for subsidiary, account and department:
var recordType = context.newRecord.type;
log.debug({ title: 'Record Type', details: recordType })
var recordId = context.newRecord.id;
log.debug({ title: 'Record ID', details: recordId })
var rec = record.load({
type: recordType,
id: recordId
});
//Setting External ID on subsidiary
if (recordType == record.Type.SUBSIDIARY)
{
var subExID
=rec.setValue('externalid'(rec.getValue('tranprefix').substring(0, 3)))
log.debug({ title: 'TranPrefix', details: subExID })
}
//Setting External ID on account
if (recordType == record.Type.ACCOUNT)
{
var accExID = rec.setValue('externalid',
rec.getValue('acctnumber'))
log.debug({ title: 'External ID', details: accExID })
}
//Setting External ID on department
if (recordType == record.Type.DEPARTMENT)
{
var deptExID = rec.setValue('externalid', rec.getValue('custrecord_dept_code'))
log.debug({ title: 'External ID', details: deptExID })
}
Please let me know if this is helpful !!!

customer.copy : Please enter value(s) for: Customer ID exception is thrown

I try to create a copy of existing customer via script. To add it as a sub customer for other subsidiary. (eg US customer bought form amazon.CA)... anyway ...
I tried:
require('N/record', function (record) {
var customer = {
id: XXXXXX,
type: record.Type.CUSTOMER,
isDynamic: true /* I tried false too */
}
var new_customer = record.copy(customer);
var new_customer_data = {
subsidiary: XX,
parent: XXXXXX,
currency: XX
};
for (var key in new_customer_data) {
new_customer.setValue({
fieldId: key,
value: new_customer_data[key]
});
}
new_customer.save() /* ! this throws the exception "Please enter value(s) for: Customer ID" */
});
I checked the NS docs. but I found nothing related.
I could get the necessary fields (.getValue ... & and record.create ... ) but... record.copy is nicer :) and addresses remains attached to the new customer (when a copy is created trough UI).
thx!
update
After few hours of more researches I found that the issue is related to auto-generate numbers. More precisely, is related to 'Allow Override' option of order numbers for the customers. If it's set to false : the script works perfectly(I tested in sandbox). But if it's set to true it throws an exception. I did more tests. When the customer is copied (via script) the entityId is affect with the original customer entityId, that blocks the .save() action. If I set entityID to null it doesn't work either. When I set the entityId with any unique value .save() action works fine (but this is not a solution for me.. not yet :) ).
Maybe there is a record/context field that I should used to auto-generate a
new number e.g. "clone_rec.generate_new_CUSTOMER_ID = true " !? Some how Netsuite overrides that when a copy of customer is created via UI.
The name of the customer is unique and is also wiped out by the copy operation.
You need to enter the new company name (or first and last names for an individual). Something like:
var new_customer_data = {
subsidiary: XX,
parent: XXXXXX,
currency: XX,
companyname: 'some new company name',
autoname:true // this may not make a difference. Depends on how your account is configured.
};

How to get record by Id without recordType in netsuite?

I could get record by below code
record.load(recordType, id)
For above function, both args are required.
Any way to get record by id alone without recordType in netsuite suitescript 2.0?
That is not possible, you do have to get the recordType but in case your records are transactions such as: Sales Orders, Purchase Orders, Invoices, etc.
You can use the following code to get the recordType only having the id:
var recordType = search.lookupFields({
type: search.Type.TRANSACTION,
id: your_record_id,
columns: 'type'
});
then you can use the record.load for your needs.

Resources