Changing the REPRESENTS SUBSIDIARY field in Netsuite - 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).

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 Mass Update script runs sucessfully, but nothing gets updated

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!)

SuiteScript Updating Search Result Fields

I have a search in SuiteScript 2.0 that's working fine. But for each record the search brings back, I want to update a particular field (I use it elsewhere to determine that the record has been examined). It's potentially a very large result set, so I have to page it. Here's my code:
var sResult = mySearch.runPaged({pageSize: 10});
for (var pageIndex = 0; pageIndex < sResult.pageRanges.length; pageIndex++)
{
var searchPage = sResult.fetch({ index: pageRange.index });
searchPage.data.forEach(function (result)
{
var name = result.getValue({ name: "altname"})
result.setValue({
name: 'tracker',
value: new Date()
})
});
}
You can see where I have a call to result.setValue(), which is a non-existent function. But it shows where I want to update the 'tracker' field and what data I want to use.
Am I barking up the wrong tree entirely here? How do I update the field for each result returned?
As Simon says you can't directly update a search result, but you can use submitFields method.
This example is from NetSuite documentation:
var otherId = record.submitFields({
type: 'customrecord_book', //record Type
id: '4', // record Id
values: {
'custrecord_rating': '2'
}
});
This approach will save more governance than load and save the record.
AFAIK You can't directly update a search result and write the value back to the record, the record needs to be loaded first. The snippet doesn't say what the type of record it is you're searching for or want to load, but the general idea is (in place of your result.setValue line):
var loadedRecord = Record.load({type:'myrecordtype', id:result.id});
loadedRecord.setValue( {fieldId:'tracker', value: new Date() });
loadedRecord.save();
Keep in mind SuiteScript governance and the number of records your modifying. Load & Save too many and your script will terminate earlier than you expect.
Bad design: instead of using result.setValue inside the iteration, push those to an "update" array then after the data.forEach have another function that loops thru the update array and processes them there with record.submitFields()
Be careful of governance....

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 perform SubmitFields onto a custom record in SuiteScript 2.0?

To perform SubmitFields onto standard Netsuite Records (i.e. Purchase Orders) it is something like this:
var poId = context.key;
var id = record.submitFields({
type: record.Type.PURCHASE_ORDER,
id: poId,
values: {
custbody_someField: someValue
},
options: {
enableSourcing: false,
ignoreMandatoryFields : true
}
});
What is the type field for Custom Records? I tried the ID of the Custom Record, but it doesn't work:
e.g.
type: record.Type.customrecord_my_record_id
I don't know what the 'official' answer is. The fake enum types don't have any custom record references that I was able to find. Setting the type to the string that is the id of the custom record works for me. (No record.Type. prefix though)
... type: "customrecord_my_record_id", ...
That is true that the references are only for standard record types. You can alternatively get all enums into a variable and log it using
var recordTypesEnums = Object.keys(record.Type);
//you may log recordTypesEnums array

Resources