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.
Related
I have created a print button to print an advanced PDF from a sales order, so the client can get a commercial invoice generated. I have done this for other clients with success but I am running into an issue where the standard templates will print a pdf from the button but the customized ones will not. Where am I going wrong in the code here:
Suitelet:
function onRequest(context) {
var custom_id = context.request.parameters.custom_id;
var tempid = context.request.parameters.tempid;
log.debug("id", custom_id);
log.debug("id", context.request.parameters);
var pdfFileName = "Commercial Invoice";
var renderer = render.create();
var content = renderer.addRecord({
templateName: 'record',
record: record.load({
type: record.Type.SALES_ORDER,
id: custom_id
})
});
renderer.setTemplateByScriptId(tempid);
var pdfFile = renderer.renderAsPdf();
log.debug ("pdf", pdfFile)
context.response.writeFile(renderer.renderAsPdf(),true);
}
return {
onRequest: onRequest
}
});
Client Script:
function onButtonClick() {
var suiteletUrl = url.resolveScript({
scriptId: 'customscript_commercialinv_so_sl',
deploymentId: 'customdeploy_commercialinv_so_sl',
returnExternalUrl: true,
params: {
custom_id: currentRecord.get().id, tempid: "CUSTTMPL_109_4753601_SB1_795"
},
});
window.open(suiteletUrl,"_blank");
}
exports.onButtonClick = onButtonClick;
exports.pageInit = pageInit;
return exports;
});
My user script looks solid. I am getting this error when I direct the Client script to a custom template as referenced above:
{"type":"error.SuiteScriptError","name":"MISSING_PDF_PARAMETERS","message":"Missing parameters required to generate PDF","stack":["renderAsPdf(N/render)","onRequest(/SuiteScripts/MegWebb/cust_SOprinttemplate_SL.js:28)"],"cause":{"type":"internal error","code":"MISSING_PDF_PARAMETERS","details":"Missing parameters required to generate PDF","userEvent":null,"stackTrace":["renderAsPdf(N/render)","onRequest(/SuiteScripts/MegWebb/cust_SOprinttemplate_SL.js:28)"],"notifyOff":false},"id":"","notifyOff":false,"userFacing":false}
Any help would be much appreciated!
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.
I tried implementing netsuite pagination in suitescript 2.0, the prev button is working but instead of 20 search data it's return like whole. Next button is not working always throwing error INVALID_PAGE_RANGE
Here is the suitelet code
var loadSearch = search.load({
id: 'customsearch_inv123'
});
var i = 0;
var pagedData = loadSearch.runPaged({
pageSize: 20
});
pagedData.pageRanges.forEach(function (pageRange) {
var lastPageRange = pagedData.pageRanges[pagedData.pageRanges.length - 1];
var Page = pagedData.fetch({
index: lastPageRange.index
});
if (context.request.parameters.next) {
Page = Page.next();
}
if (context.request.parameters.prev) {
Page =Page.prev();
}
Page.data.forEach(function (result) {
var number = result.getValue({
name: 'inventorynumber'
});
if (number) {
updateSublist.setSublistValue({
id: 'custpage_number',
line: i,
value: number
});
}
i++;
});
});
// submit button
form.addSubmitButton({
label: 'Submit'
});
form.addButton({
id: '_next',
label: 'Next',
functionName: 'next'
});
form.addButton({
id: '_prev',
label: 'Prev',
functionName: 'prev'
});
form.clientScriptModulePath = 'SuiteScripts/client.js';
In client script i wrote the function next and prev, and redirected to the same page with next or prev parameters and based on that i called page.next and page.prev.
If you have implemented this please help me.
here is client code.
next: function () {
window.location.href = 'example.com' + '&next=t';
},
prev: function () {
window.location.href = 'example.com' + '&prev=t';
}
You should pass current/requested pageNumber from client script and then use it to fetch data for that specific page like below
var pagedData = loadSearch.runPaged({
pageSize: 20
});
requestedPageNumber = 2
var page = pagedData.fetch({ index: requestedPageNumber });
page.data.forEach(function (result) {
// result is the searchResult object
// YOUR CODE GOES HERE
var number = result.getValue({
name: 'inventorynumber'
});
})
What you did wrong was you were starting from the last index and then trying to fetch unexisting array index in case where user clicks next.
Note: previous button should be disabled/hidden where there are no pages so as to not run into the same error and the same could be done for the next button too.
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.
I used meteor sitemaps package to generate sitemap using below code:
Meteor.methods({
sitemapsGenerator: function (generateAnyway) {
var setting = Settings.findOne({ title: 'sitemapsLastGenerateDate'});
if (generateAnyway || (new Date(setting.lastModified.getTime()+(24*60*60*1000)) < new Date)) {
console.log("sitemap generator called");
var generalSitemapUrls = [];
for (var i = 0; i <= Math.round(MyPosts.find().count() / 10000); i++) {
sitemaps.add('/sitemap' + i + '.xml', (function (i) {
var out = [];
MyPosts.find({}, {
fields: {title: 1, postDate: 1},
sort: {postDate: 1}, limit: 10000, skip: i * 10000
}).forEach(function (post) {
console.log("sitemap called" + i);
out.push({
page: "/posts/" + post.title + "/" + post._id,
lastmod: post.postDate,
changefreq: 'weekly'
});
});
return out;
})(i));
}
Settings.update({title: "sitemapsLastGenerateDate"}, {$set: {lastModified: new Date} });
}
}
});
Now after 2 month my collection size was growing up and recently I got below error when try to generate sitemap:
MongoError: too much data for sort() with no index. add an index or
specify a smaller limit.
What is the solution to fix this error?
If this error fix by adding index to collection field how to do this in meteor?
on your collection object you use ensureIndex. Typically you do this on your server side, on startup.