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.
Related
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.
Have been a developer for years, but just getting into NetSuite scripting. I wanted to start simple and create a script that would update the COMMENT field for all items on a purchase order. I started off by following the tutorial found here.
My modified script is below - it should update the MEMO field at the PO header and then every line's COMMENT field. I followed the steps in the article to save the script, create a Mass Update and then run it. In my criteria I set: "Transaction Number/ID" and "has keyword 90999" (just so it would only run on one specific purchase order). I confirmed my criteria was correct by clicking "Preview" and it only returns that one PO.
When I run the Mass Update, it runs fine and says it has been run successfully on 1 record (which is good). The MEMO field at the PO header DOES get updated, but the COMMENT field for each line does not. Am I doing something wrong or missing something simple? Is the getLineCount call not the correct one to use?
Note that I was doing all this in our Sandbox environment in case that makes any difference
UPDATE:
I know the getLineCount call is returning the correct number because if I move the poRec.setValue call inside the for loop, it gets run. So something must be wrong with my poRec.setSublistValue call?
/**
*#NApiVersion 2.0
*#NScriptType MassUpdateScript
*/
define(['N/record'],
function (record) {
function each(params) {
// Need to LOAD each record and SAVE it for changes to take effect
// LOAD the PO
var poRec = record.load({
type: params.type,
id: params.id
});
//var mainDepartment = poRec.getValue('department');
var mainDepartment = 'Hello World';
poRec.setValue({
fieldId: 'memo',
value: mainDepartment
});
// get a count of the line items
var lineCount = poRec.getLineCount({
sublistId: 'item'
});
// go through each of the line items and set the department to the main level department
for (var i = 0; i < lineCount; i++) {
poRec.setSublistValue({
sublistId: 'item',
fieldId: 'comment',
line: i,
value: mainDepartment
});
}
// SAVE the PO
poRec.save();
}
return {
each: each
};
});
There is no 'comment' field on the item sublist. The field you probably mean is 'description' (which expresses on searches as the line level 'memo' field - Netsuite for the win!)
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 !!!
I'm working in NetSuite and I have a custom field on an estimate for the outside sales rep. When the record is submitted, I want the employee field under the Sales Team subtab to be set to the same value as the custom outside sales rep field. However, when I try to do this with a user event script, nothing happens. I am using the after submit function. Any suggestions? Here is what my code looks like:
function afterSubmit(scriptContext){
var record = scriptContext.newRecord;
var outsideSalesRep = record.getValue({
fieldId: 'custbody_bs_salesrep_outside'
});
record.setSublistValue({
sublistId: 'salesteam',
fieldId: 'employee',
line: 0,
value: outsideSalesRep
});
}
You have to do this in beforesubmit, not after (unless you're explicitly reloading the record and saving it again in your script). The record has already been written to the database in aftersubmit & the object returned by scriptContext.newRecord is essentialy readonly, that's why it's discarding the changes.
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.
};