SuiteScript deploying script Fail to evaluate script - netsuite

I am making a usereventcript that computes for the rate using subfields from the sales order item list. trying to save and deploy the script launches an error Fail to evaluate script: {"type":"error.SuiteScriptModuleLoaderError","name":"UNEXPECTED_ERROR","message":"missing } after property list (SS_SCRIPT_FOR_METADATA#32)","stack":[]}
/**
*#NApiVersion 2.x
*#NScriptType UserEventScript
*/
define(
[
'N/record'
],
function (record) {
/**
* #param {UserEventContext.beforeSubmit} context
*/
function beforeSubmit(context) {
//get taxcode
var taxcode = context.newRecord.getValue({
fieldId: 'taxcode'
});
if(taxcode !== 0 || taxcode !== 4){
// Gets the Total Amount
var amount = context.getValue.getValue({
fieldId: "amount"
});
// Gets the quantity of an item selected
var quantity = context.newRecord.getValue({
fieldId: 'quantity'
});
var rate_ = amount/quantity;
var newRate = context.newRecord.setValue({
fieldId : 'rate'
value : ('rate',rate_)
});
}
}
return {
// beforeSubmit: beforeSubmit,
};
});

Your code is not syntactically valid. Please replace below code
var newRate = context.newRecord.setValue({
fieldId: 'rate'
value: ('rate', rate_)
});
with
var newRate = context.newRecord.setValue({
fieldId: 'rate',
value: ('rate', rate_)
});
You can try validating your code with JS syntax validator esprima. Although alot of IDE's now support validation.

Related

Suitescript 2.0 record.load either Lead or Prospect

I created a user event script that updates a lead custom field from a new phone call record on afterSubmit. It loads the customer from the id of the entity id on the phone call event. It works fine, but it loads a lead record only. How do I fine tune this script to get the entity type and then use that to determine which record type to load?
function afterSubmit(context) {
var phoneRec = context.newRecord;
var checkbox1 = phoneRec.getValue({
fieldId: 'custevent_field1'
});
var checkbox2 = phoneRec.getValue({
fieldId: 'custevent_field2'
});
var custId = phoneRec.getValue({
fieldId: 'company'
});
if(custId && checkbox1 == true) {
var loadedcust = record.load({
type : record.Type.LEAD,
id : custId});
loadedcust.setValue({
fieldId:'custentity_filed1',
value: true });
loadedcust.save();
}
if(custId && checkbox2 == true) {
var loadedcust = record.load({
type : record.Type.LEAD,
id : custId});
loadedcust.setValue({
fieldId:'custentity_field2',
value: true });
loadedcust.save();
}
}
return {afterSubmit}
Not sure how to pull more entity information from just the id value. Any insight will help.
Yes you should be able to use just customer.
Also unless your values are exclusive you can also do the following to make the return quicker:
define(['N/record',...], function(record,...){ // include record module in your define
function afterSubmit(context) {
var phoneRec = context.newRecord;
var checkbox1 = phoneRec.getValue({
fieldId: 'custevent_field1'
});
var checkbox2 = phoneRec.getValue({
fieldId: 'custevent_field2'
});
var custId = phoneRec.getValue({
fieldId: 'company'
});
var updates = {};
var anyUpdate = false;
if(custId && checkbox1 == true) {
updates.custentity_filed1 = true; //not field1?
anyUpdate = true;
}
if(custId && checkbox2 == true) {
updates.custentity_field2 = true;
anyUpdate = true;
}
if(anyUpdate){ //single update. No need to load record twice nor submit it twice.
record.submitFields({
type:'customer',
id:custId,
values:updates
})
}
}
return {afterSubmit}
}

Fetching the lineitem field from item table when adding the item as lineitem in SO - Netsuite

I am Learning netsuite,
I am trying to fetch the Customfield "custitem_celigo_sfnc_salesforce_id" from item, on sales order module when adding that item in SO line items
here is my sample
/**
*#NApiVersion 2.x
*#NScriptType ClientScript
*/
define(['N/record', 'N/search', 'N/url', 'N/https', 'N/runtime'], function(record, search, url, https, runtime) {
function fieldChanged(context) {
var currentRecord = context.currentRecord;
var sublistName = context.sublistId;
var sublistFieldName = context.fieldId;
var line = context.line;
var SFID = context.custitem_celigo_sfnc_salesforce_id;
var descriptionValue = currentRecord.getCurrentSublistValue({
sublistId: sublistName,
fieldId: "custitem_celigo_sfnc_salesforce_id"
})
alert(JSON.stringify(currentRecord));
alert(JSON.stringify(test));
return true;
}
var exports = {};
exports.fieldChanged = fieldChanged;
return exports;
});
It is not fetching the custom field , what is the way to do that.
Thanks in advance,
Try this one
var descriptionValue = currentRecord.getCurrentSublistValue({
sublistId: item,
fieldId: "custitem_celigo_sfnc_salesforce_id",
line:line
})

Netsuite Scheduled Script produces unexpected error I can't duplicate

I have a Scheduled SuiteScript 2.0 that uses a saved search to get a list of unpaid or partially paid invoices. The script iterates through each invoice and looks at the transaction lines. First I check to see if the transaction line is associated with a subscription since some transaction lines could be for something else. If I find a subscription ID, the idea is to load it and do additional things to that subscription because of its unpaid status. The problem is when the scheduled script runs, it's failing on the line that is trying to load the subscription record. Here is a snippet of the code, and then the error I receive:
/**
* #NApiVersion 2.x
* #NScriptType ScheduledScript
* #NAmdConfig /SuiteScripts/nsRequireConfig.json
*/
define(["require", "exports", "N/search", "N/record", "revlocal/utils/datefunctions", "N/log"], function (require, exports, search, record, dateUtils, log) {
Object.defineProperty(exports, "__esModule", { value: true });
exports.execute = function (context) {
if (context.type !== context.InvocationType.SCHEDULED) {
return;
}
var s = search.load({ id: "customsearch_openinvoices" });
var results = s.run().getRange(0, 1000);
for (var _i = 0, results_1 = results; _i < results_1.length; _i++) {
var r = results_1[_i];
var invoiceId = parseInt(r.getValue({ name: "internalid", summary: search.Summary.GROUP }), 10);
var dueDate = new Date(r.getValue({ name: "duedate", summary: search.Summary.GROUP }));
var invoice = record.load({ type: record.Type.INVOICE, id: invoiceId });
var count = invoice.getLineCount({ sublistId: "item" });
for (var i = 0; i < count; i++) {
try {
var linePaid = invoice.getSublistValue({ sublistId: "item", line: i, fieldId: "custcol_rl_paiddate" });
if (!linePaid) {
var subId = parseInt(invoice.getSublistValue({
sublistId: "item", line: i, fieldId: "subscription"
}), 10);
var sub = record.load({ type: record.Type.SUBSCRIPTION, id: subId });
// more code here that doesn't have anything to do with the problem
}
}
catch (error) {
log.error("Suspension on invoice failed: " + invoiceId, error);
continue;
}
}
}
};
});
And here's the error:
{"type":"error.SuiteScriptError","name":"UNEXPECTED_ERROR","message":"An
unexpected SuiteScript error has
occurred","stack":["loadRecord_impl(N/recordImpl)","(/SuiteScripts/revlocal/scheduled/setBillingSuspension.js:27)"],"cause":{"type":"internal
error","code":"UNEXPECTED_ERROR","details":"An unexpected SuiteScript
error has
occurred","userEvent":null,"stackTrace":["loadRecord_impl(N/recordImpl)","(/SuiteScripts/revlocal/scheduled/setBillingSuspension.js:27)"],"notifyOff":false},"id":"jwjlma0g1g7jovptug37j","notifyOff":false,"userFacing":false}
I have debugged the script and have loaded the subscription records that are failing with no problem so I can't duplicate the error except when the scheduled script runs. I've already spoke with Netsuite Support, and they didn't know what was wrong, which didn't surprise me. I'm hoping someone has had a similar experience and can help me out.
So it turns out that it was an Event Trigger script that was interfering with the scheduled script. Depending upon what's being done to records in a beforeLoad trigger could cause an error in the scheduled script. I removed the trigger, and have had success now. The work for the beforeLoad was work that could be done elsewhere, so the trigger was moved to afterSubmit.

Netsuite, update value and save record

I want to edit a record, then click "Save" and the field "custitem_con" to be updated with a new value and the record saved.
/**
*#NApiVersion 2.0
*#NScriptType ClientScript
*/
define(['N/currentRecord'],
function(currentRecord) {
function saveRecord (){
var objRecord = currentRecord.get();
var con = 'Success!...but record is not saved :(';
objRecord.setValue({
fieldId: 'custitem_con',
value: con,
});
}
return {
saveRecord: saveRecord
};});
However while the field custitem_con gets the value, the record is not saved, but remains in edit mode. How do I get the record saved?
In order to allow the record to be submitted, you need to return true from the saveRecord() function, thus:
/**
*#NApiVersion 2.0
*#NScriptType ClientScript
*/
define(['N/currentRecord'],
function(currentRecord) {
function saveRecord (){
var objRecord = currentRecord.get();
var con = 'Success!...but record is not saved :(';
objRecord.setValue({
fieldId: 'custitem_con',
value: con,
});
return true;
}
return {
saveRecord: saveRecord
};});

SuiteScript 2.0 setValue not allowed in the current subrecord

I'm trying to set a value for the sublist 'addressbookaddress'. But the script fail with error. However, I'm able to get the subrecord value.
Error:
Not supported on the current subrecord: CurrentSubrecord.setValue.
Executed code:
/**
*#NApiVersion 2.0
*#NScriptType ClientScript
*/
define(["N/currentRecord"], function(currentRecord){
/*
Copy phone number from vendor to address, when creating a new sublist entry
*/
var lineInit = function(context) {
var record = context.currentRecord;
var sublistId = context.sublistId;
var subrecord = record.getCurrentSublistSubrecord({
sublistId: sublistId,
fieldId: 'addressbookaddress'
});
if (!subrecord) {
return;
}
var address = subrecord.getValue({
fieldId: 'addr1'
});
subrecord.setValue({
fieldId: 'addr1',
value: 'test'
});
return;
}
return {
lineInit: lineInit,
}
});
Client scripts have read-only access to subrecords.
A client script can be deployed on the Address form. Using values from the entryformquerystring one can search for the parent record.

Resources