How to make sublist field mandatory? - netsuite

This is my pageInit function
function poTransPageInit(type) {
// alert('page init');
var field= nlapiGetLineItemField('item', 'field');
field.isMandatory = true;
}
what I do wrong here?

It is true that the isMandatory method is not available in Client Scripts. As a work around you could get the field's value and check the length.
function validateLine(type){
if(type == 'sublistInternalID'){ //sublistInternalID = sublist internal id, i.e. 'item'
//get the sublist field value of mandatory column
var name = nlapiGetCurrentLineItemValue('line', 'fieldId'); //line = line #, fieldId = internal id of field
//if value length is greater than 0, then there is a value
if(name.length > 0){
return true;
} else {
alert('Please enter a value for Name field');
return false;
}
}
}

field.isMandatory is SuiteScript 2.0. In SuiteScript 1.0, you would use field.setMandatory(true), but apparently that function is not available in client scripts.
You could try moving this logic to a User Event script.

Related

Fill duedate field based on value of terms field

Is it possible to fill the duedate field based on the terms field? For example, I have a date in the duedate field, but I want to extend it based on the terms field. How can I do it? Through Suitescript or workflow?
The code is incomplete because I don’t know if I’m on the right path.
(NB: It is a user event)
function beforeLoad(context) {
}
function beforeSubmit(context) {
}
function afterSubmit(context) {
var dataEntrega = context.currentRecord
currentRecordRecord.getValue({
fieldId: 'duedate'
})
var dataCondicoes = context.currentRecord
currentRecord.getValue({
fieldId: 'terms'
})
}
return {
beforeLoad: beforeLoad,
beforeSubmit: beforeSubmit,
afterSubmit: afterSubmit
}
Why do you need to script this? I believe the due date is calculated based on the terms out of the box, no need for scripting
The following code should work. Depending on your needs I recommend adding some limitations to when this logic is executed. For example you can only execute based on if the transaction/record mode is create/copy (decide if you want to include edit or not). You can also check the status of the transaction, and only execute if the status is not partially paid/paid in full...
function afterSubmit(context) {
//load record
var curRec = context.newRecord; //can substitute "context.oldRecord", or "currentRecord.get();"
//get current due date
var transDueDate = curRec.getValue({fieldId: 'duedate'});
//get the terms, this will likely come as an internal id. use getText if you want the text.
var transTerms = curRec.getValue({fieldId: 'terms'});
//empty string to hold terms as a number of days
var addtlDays;
//transform the internal id to terms as a number of days
switch (transTerms){
case 1: // Ex: 1 = internal id for term "Net 15"
addtlDays = 15;
break;
case 2: // Ex: 2 = internal id for term "Net 30"
addtlDays = 30;
break;
//add additional case statements as needed
default:
addtlDays = 0;
}
//calculuate the new due date
var d = new Date(transDueDate);
var newDueDate = d.setDate(d.getDate() + addtlDays);
//set the new due date
curRec.setValue({
fieldId: 'duedate',
value: newDueDate,
ignoreFieldChange: true //optional, default is false
});
}

Field changed function on Item Fulfillment for inventory detail

I'm trying to copy the serial # from inventory detail on a Item Fulfillment line to another field for further processing, for which I'm trying to do a field changed function via Client Script but not able to figure out the field trigger.
This is what I have so far...
function populatePIF(type, name)
{
if(name == 'inventorydetail')
{
nlapiLogExecution('DEBUG', 'TEST', 'STATUS')
try
{
var inventorydetail = nlapiViewCurrentLineItemSubrecord('item', 'inventorydetail');
if(inventorydetail)
{
var PIF = inventorydetail.getCurrentLineItemText('inventoryassignment', 'issueinventorynumber');
nlapiSetCurrentLineItemValue('item', 'custcol_serial_no', PIF);
return true;
}
return true;
}
catch(e)
{
nlapiLogExecution('DEBUG', 'Exeception Caught', e);
}
return true;
}
}
Any ideas?
In my own recent experience, there is no trigger on the inventorydetail "field" as it's not really a field.
Line validation is also not triggered as far as I can tell.
Only option is an afterSubmit User event script. This will be more reliable.

How to set a column field on a Journal entry to false

I am trying to set a mandatory column field(through UI at field level) to false on a Journal Entry record.
I need to use User Event script because the journal entries are system generated and the same custom field should be mandatory for other transactions.
I have tried using a user event before load and setMandatory(false) but it is not working.
Here is the code I am using:
function removeMandatory(type)
{
if(type == 'create')
{
for( var i =1 ; i < nlapiGetLineItemCount('line') ; i++)
{
var customField = nlapiGetLineItemField('line', 'custcol_test_mandatory', i);
customField.setMandatory(false);
}
}
}
Any help is appreciated, Thanks
Thanks #Rusty Shackles:
I have actually worked it out using User Event Before Load function. Here is the code:
function notMandatoryBeforeLoad(type)
{
var context = nlapiGetContext();
if(type == 'create' && context.getExecutionContext() == 'scheduled')
{
var LineMandatoryField = nlapiGetLineItemField('line','custcol_test_mandatory');
if (LineMandatoryField)
{
LineMandatoryField.setMandatory(false);
}
}
}
I believe field objects are read only if not done in a Suitelet.
What you can do is use a client side script that executes on field change/validate field and check if the "mandatory" field as a value.

How to pass values for multi select custom fields using RESTlet in Netsuite?

I can pass values for select, text box and etc but not for multi select. I can update values for multi select. But i can't create a record by passing values for multi select.
This is the code :
$datastring = array(
"gu_action"=> "create",
"recordtype"=>"vendor",
"companyname"=>"Jerald Vend",
'subsidiary'=>1,
'custentity36'=>1
);
custentity36 is multiselect control. It's label is Course
when i pass single value , It works fine.
when i try to pass multiple values for multi select like the below code , i am getting error like "Please enter value(s) for: Course"
$datastring = array(
"gu_action"=> "create",
"recordtype"=>"vendor",
"companyname"=>"Jerald Vend",
'subsidiary'=>1,
'custentity36'=>array(1,3)
);
The Code is : https://gist.githubusercontent.com/ganeshprabhus/a3ebd67712913df3de29/raw/3a6df6a3af8642fceacb3a4b8e519ad96a054e69/ns_script.js
The value you pass is in correct format. In this case the RESTlet code should have the compatibility of handling the multiselect filed. The field set value api that used in the RESTlet should be
nlapiSetFieldValues()
This is the api can be used to set multiselect field value. As per the github refernce you shared. under the create_record function
/********************** Creation *********************************/
function create_record(datain) {
var err = new Object();
// Validate if mandatory record type is set in the request
if (!datain.recordtype) {
err.status = "Failed";
err.message = "Missing recordtype";
return err;
}
var record = nlapiCreateRecord(datain.recordtype);
for ( var fieldname in datain) {
if (datain.hasOwnProperty(fieldname)) {
if (fieldname != 'recordtype' && fieldname != 'id') {
var value = datain[fieldname];
// ignore other type of parameters
if (value && typeof value != 'object') {
record.setFieldValue(fieldname, value);
}
} //recordtype and id checking ends
}
} //for ends
var recordId = nlapiSubmitRecord(record);
nlapiLogExecution('DEBUG', 'id=' + recordId);
var nlobj = nlapiLoadRecord(datain.recordtype, recordId);
return nlobj;
}
The quoted code should be
record.setFieldValues(fieldname,value) // fieldname : custentity36 , value : 1,3

Validate In-Line Edits in Netsuite

I need to validate inline editing in NetSuite.
I already have a Client Script in place that works great when editing the record normally.
I tried adding a User Event script that on the before save function that validates the record, but it appears this is ignored with inline editing.
Has anybody ran into this before?
Any insight you can provide would be helpful. Thanks!
Edits:
The relevant code from the UE script:
function beforeSubmit(type){
if (type == "create" || type == "edit" || type == "xedit") {
var status = nlapiGetContext().getSetting("SCRIPT", "...");
var amount = Number(nlapiGetContext().getSetting("SCRIPT", "..."));
var nr = nlapiGetNewRecord();
var entitystatus = nr.getFieldValue("entitystatus");
var projectedtotal = Number(nr.getFieldValue("projectedtotal"));
if (entitystatus == status && projectedtotal >= amount) {
var statusText = nr.getFieldText("entitystatus");
var message = "ERROR...";
throw nlapiCreateError("...", message, true);
}
}
}
This applies to the opportunity record.
The field being validated is Projected Total with id projectedtotal.
My mistake, I misunderstood how xedit handled nlapiGetNewRecord(). Calling nlapiGetNewRecord when in xedit only returns the edited fields, not the entire record. Thus, the if statement was never true in xedit mode, because either the amount or the status would be null (it was very unlikely the user would edit both at the same time, and validation relies on both these fields' values).
I edited the code to lookup the field value if it is not present in the new record. Now everything works as expected!
Thanks everyone for the help!
For reference, the corrected code is below.
function beforeSubmit(type){
if (type == "create" || type == "edit" || type == "xedit") {
var status = nlapiGetContext().getSetting("SCRIPT", "...");
var amount = Number(nlapiGetContext().getSetting("SCRIPT", "..."));
var nr = nlapiGetNewRecord();
//Attempt to get values normally
var entitystatus = nr.getFieldValue("entitystatus");
var projectedtotal = Number(nr.getFieldValue("projectedtotal"));
var id = nr.getId();
//If values were null, it's likely they were not edited and
//thus not present in nr. Look them up.
if(!entitystatus){
entitystatus = nlapiLookupField("opportunity", id, "entitystatus");
}
if(!projectedtotal){
projectedtotal = Number(nlapiLookupField("opportunity", id, "projectedtotal"));
}
if (entitystatus == status && projectedtotal >= amount) {
var message = "ERROR...";
throw nlapiCreateError("101", message, true);
}
}
}
In your user event are you checking the value of the type parameter. For inline editing, the value of the type is 'xedit'.

Resources