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.
Related
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.
I developed an extension where you can define a connection id for newsletter 2 go.
I need to pass this id to the script skin\frontend\base\default\fekete\Newsletter2Go\js\utils.js
Template:
My first approach was to pass it to the script by adding a get parameter to it.
{... Hint: I removed the rest code which is not relevant for this question ... }
(window,document,"script","skin/frontend/base/default/fekete/Newsletter2Go/js/utils.js?id=<?php echo $id ?>","n2g");
I checked if this worked like this:
alert(findGetParameter("id"));
function findGetParameter(parameterName) {
var result = null,
tmp = [];
var items = location.search.substr(1).split("&");
for (var index = 0; index < items.length; index++) {
tmp = items[index].split("=");
if (tmp[0] === parameterName) result = decodeURIComponent(tmp[1]);
}
return result;
}
But it alerts null.
How can I pass the id to the script, without having to move the whole script to the template?
I solved it by creating a new extension. I developed a controller and then just made a XHR Request to my route, which just returns the needed value.
I am trying to search Account data using SuiteScript 1.0 in netsuite, but I am getting "SS_INVALID_SRCH_COL Details: An nlobjSearchColumn contains an invalid column, or is not in proper syntax:" error. I am using Admin role for my login. following the code sample that causing error
function getMasterData(datain)
{
try
{
var recordtype = 'account';
var c;
var strcolumns = nlapiCreateRecord(recordtype).getAllFields();
var searchcolumns = [];
for (c = 0; strcolumns.length && c < strcolumns.length; c += 1)
{
searchcolumns.push(new nlobjSearchColumn(strcolumns[c]));
}
var data = nlapiSearchRecord(recordtype, null, null, searchcolumns);
return data;
}
catch (ex)
{
nlapiLogExecution('debug', 'getMasterData', ex);
}
}
I know the reason: getAllFields() returns all the fields (searchable/nonsearchable) but nlapiSearchRecord works only for searchable fields. My query is how I can filter searchable fields in the all fields returned by getAllFields() function.
Can please anybody help me regarding this. Thanks in advance.
There's no way to know if a field can be used in a search but you can get the supported fields from the Records Browser and create an array with them.
Note that you can get a record with all fields and values using nlapiLoadRecord.
I have a userevent script to change the Field in Contract record from PO record. The Script is running fine. But whenever I edit a contract record and try to submit it : It throws the error "Another user has updated this record since you began editing it. Please close the record and open it again to make your changes".
May I know the reason behind this ?
/*---------------------------------------------------------------------------------------------------------------
Description : Whenever the PO vendor is changed(due to Split vendor) that should replace the same in Contract page record automatically.
Script type : User Event Script
Script id : customscript452
Version : 1.0
Applied to : Contract
----------------------------------------------------------------------------------------------------------------*/
function srchfield()
{
var stRecordid = nlapiGetRecordId(); //returns the contract id
if(stRecordid== undefined || stRecordid== null || stRecordid==' ')
{
}
else
{
var stRecordtype = nlapiGetRecordType(); //returns the contract record type = jobs
var stRecord = nlapiLoadRecord(nlapiGetRecordType(), stRecordid);
nlapiLogExecution('debug','Load Object',stRecord);
var stContractID = stRecord.getFieldValue('entityid'); //returns the value of the field contractid whose fieldid is = entityid
nlapiLogExecution('debug','stContractID',stContractID);
var stCompanyName = stRecord.getFieldValue('companyname'); //returns the value of the field company name whose fieldid is = companyname
nlapiLogExecution('debug','stCompanyName',stCompanyName);
var stConcatenate = stContractID+" : "+stCompanyName; //Concatenate the two Fields to get the result which needs to be found in PO
var arrFilters = new Array(); // This is Array Filters all the Purchase Order Record Search
arrFilters.push(new nlobjSearchFilter('type', null, 'anyof',
[
'PurchOrd'
]));
arrFilters.push(new nlobjSearchFilter('mainline', null, 'is', 'T')); //This is to exclude line level results
arrFilters.push(new nlobjSearchFilter('custbodycontract', null, 'is', stRecordid)); //This is Filters in Contracts Search
var arrColumns = new Array();
arrColumns.push(new nlobjSearchColumn('entity')); //This is Search Column Field in Records
var arrSearchresults = nlapiSearchRecord('purchaseorder', null, arrFilters, arrColumns); //This is Filters in Search Result Purchase Order
if(arrSearchresults== undefined || arrSearchresults== null || arrSearchresults==' ')
{
}
else
{
var length = arrSearchresults.length;
}
if(length== undefined || length== null || length==' ')
{
}
else
{
for (var i = 0; arrSearchresults != null && i < arrSearchresults.length; i++)
{
var objResult = arrSearchresults[i];
var stRecId = objResult.getId();
var stRecType = objResult.getRecordType();
var stCntrctName = objResult.getValue('entity'); //This is Value are Get Purchase Order Records and Field for Vendor = entity
}
}
//var record = nlapiLoadRecord(nlapiGetRecordType(), stRecordid, stCntrctName);
if (stCntrctName =='custentityranking_vendor_name')
{
}
else
{
var stChangeName = stRecord.setFieldValue('custentityranking_vendor_name', stCntrctName); //This is Value are the Set in Main Vendor Field = custentityranking_vendor_name
nlapiSubmitRecord(stRecord, null, null); // Submit the Field Value in Record Type
}
}
}
The User Event script executes as the Contract record is being saved to the database. At the same time, you are loading a second copy of the record from the database and trying to submit the copy as well. This is causing the error you're seeing.
You fix this by just using nlapiSetFieldValue to set the appropriate field on the Contract.
I might also recommend getting more familiar with JavaScript by going through the JavaScript Guide over at MDN. In particular, take a look at the Boolean description so that you know how JavaScript evaluates Boolean expressions. This will help you greatly reduce the amount of code you've written here, as many of your conditionals are unnecessary.
What userevent do you have? It is happening depending on what type of user event and API you are using. Looking at your code, you are trying to load contract record that is already updated at the database. So you might consider below to address your issue. Hope, it helps.
If it is a before submit, you don't need to load the record where the script is deployed.
Just use nlapiGet* and nlapiSet* to get and set values. You also don't need to use nlapiSubmitRecord to reflect the change. With before submit, it executes before the record is being saved to the database. So your changes will still be reflected.
Then if it is after submit, it will be executed after the record has been saved to the database, Thus you might use the following API depending on your needs. Actually, this is the best practice to make sure the solution .
nlapiGetNewRecord - only use this if the script only needs to retrieve info from header and sublists. And nothing to set.
nlapiLookupField - use this if the script only needs to get value/s at the header and nothing from the line.
nlapiSubmitField - the script don't need to load and submit record if the changes only on header. Just use this API.
nlapiLoadRecord and nlapiSubmitRecord- use the former if the script will have changes at the line and then use the latter api to commit it on the database.
Being a user event script code, The code you showed is very not good considering performance.
Here is the sample you can merge
var stRecordid = nlapiGetRecordId(); //returns the contract id
// Every record has an internal id associated with it. No need to add condition explicitly to check if its null
var stRecordtype = nlapiGetRecordType();
var fields = ['entityid','companyname'];
var columns = nlapiLookupField(stRecordtype, stRecordid, fields);
var stContractID = columns.entityid;
var stCompanyName = columns.companyname;
nlapiLogExecution('debug','stContractID/stCompanyName',stContractID+'/'+stCompanyName);
var stConcatenate = stContractID+" : "+stCompanyName; //Concatenate the two Fields to get the result which needs to be found in PO
//
//your code of search
//you can improve that code also by using nlapilook up
nlapiSubmitField(stRecordtype, stRecordid, 'custentityranking_vendor_name', 'name to be updated');
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'.