Can you make the id field the external id in Netsuite? - 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 !!!

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.

Changing the REPRESENTS SUBSIDIARY field in Netsuite

I wish to change the subsidiary which is being represented by a certain entity, as I have used a dummy subsidiary before.
Is there a way to do this? I cannot change the field via the UI and I cannot find the field in the import functionality
You can find the "subsidiary" field in the UI by completing the following: go to a entity record in edit mode, un-layer all tabs (or add "&unlayered=T" to the url bar), use the browser Find feature (normally Ctrl + F) and search for subsidiary. If it does not come up this way identify the form used and open that for editing, un-layer all tabs, and use the find feature for subsidiary, elect to show or change the display settings then return to the record in edit mode to change.
OR via suitescript, if you have the field id (if native/standard NetSuite field id = subsidiary) you can run the following code in the browser console
require(['N/record'], function(record){
var Ids = [
//enter all entity internal ids here
];
for (var i=0; i<Ids.length; i++){
record.submitFields({
type: record.Type.______, //ref Suite Answer Id: 45161
id: Ids[i],
values: {
subsidiary: 1 //the internal id for the subsidiary you want to set
},
options: {
enableSourcing: false, //default is true
ignoreMandatoryFields : true //default is false
}
});
console.log('done', 'updated id: '+Ids[i]);
}
console.log('done', '# of records updated: '+i);
});
OR you can try Mass Updates (ref Suite Answer 8429), or CSV Imports (ref Suite Answer 10022).

NetSuite SuiteScript 2.0 Get Value of Choose Team field

On the standard Customer form there's a field named Choose Team. It's a list of the defined sales teams - you choose the team and it populates the Sales Team list with all the people in the team, and then empties itself. So to the user, you choose the sales team in the Choose Team field and then, a second later, it empties itself.
What I'm trying to do is get the value that the user selects in that field. I'm using a client script, running of the fieldChanged entry point, and successfully detecting a change to that field. But by the time I use getValue, the field has emptied itself and I get an empty return.
Here's my code:
function fieldChanged(context){
var currentRecord = context.currentRecord;
if(context.fieldId == 'salesgroup'){
var salesTeam = currentRecord.getValue({
fieldId: 'salesgroup'
});
log.debug({
title: 'Sales Team',
details: salesTeam
});
dialog.alert({
title: 'Sales Team',
message: 'Sales Team is ' + salesTeam
});
}
}
You can try getting the value during validateField instead of fieldChanged. Don't forget to return true at the end of validateField to let the change proceed.

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

Update Custom Address Field in Netsuite using SuiteScript 2.0

I created a new custom field in the 'US' address form called 'email', so I can have an email address associated with both the bill to and ship to on a sales order in Netsuite. I am trying to update this field using Suitescript 2.0, but cannot seem to save the changes. Can anyone give any insight?
var salesorder = record.load({
type: record.Type.SALES_ORDER,
isDynamic: true,
id: 6835
});
var shippingAddressSubrecord = salesorder.getSubrecord({fieldId : 'shippingaddress'});
var email = shippingAddressSubrecord.getValue({fieldId : 'custrecord_email_address' }); //returns correctly
shippingAddressSubrecord.setValue({fieldId : 'custrecord_email_address', value: 'test2#gmail.com', ignoreFieldChange: true });
var salesorderid = salesorder.save({enableSourcing: true, ignoreMandatoryFields: true});
This gives the error: "type":"error.SuiteScriptError","name":"OPERATION_IS_NOT_ALLOWED","message":"The subrecord line has already been committed or cancelled. The previous subrecord reference is no longer valid. You must get another reference to the subrecord in order to perform this operation.",
Depends, which script type is this? User Even scripts for example, loading/saving the record would result in an error.

Resources