Hi There
I have a workflow action script that is designed to create a mail merge and send out emails with attachments
I am receiving the following error when I try to process more than approx 15 transactions:
Error: SSS_USAGE_LIMIT_EXCEEDED
{"type":"error.SuiteScriptError","name":"SSS_USAGE_LIMIT_EXCEEDED","message":"Script Execution Usage Limit Exceeded","stack":["createError(N/error)","onAction(/SuiteScripts/sdf_ignore/Send Remittance PFA Workflow Action.js:84)"],"cause":{"type":"internal error","code":"SSS_USAGE_LIMIT_EXCEEDED","details":"Script Execution Usage Limit Exceeded","userEvent":null,"stackTrace":["createError(N/error)","onAction(/SuiteScripts/sdf_ignore/Send Remittance PFA Workflow Action.js:84)"],"notifyOff":false},"id":"","notifyOff":false,"userFacing":false}
This works for the most part unless I exceed a certain number of transactions.
Do I need to use another script type? Or can I modify the following script to reduce its governance usage?
/**
*@NApiVersion 2.x
*@NScriptType WorkflowActionScript
*/
define([
"N/search",
"N/record",
"N/render",
"N/file",
"N/xml",
"N/email",
], function (search, record, render, file, xml, email) {
function onAction(context) {
var fileObj = [];
var record = context.newRecord;
log.debug("record", record);
var batchId = record.getValue({ fieldId: "name" });
var id = record.id;
log.debug("recordid", record.id);
var vendorpaymentSearchObj = search.create({
type: "vendorpayment",
filters: [
["type", "anyof", "VendPymt"],
"AND",
["custbody_9997_pfa_record", "anyof", id],
],
columns: [
search.createColumn({
name: "transactionnumber",
summary: "GROUP",
label: "Transaction Number",
}),
search.createColumn({
name: "formulatext",
summary: "GROUP",
formula: "{entity}",
label: "Vendor",
}),
search.createColumn({
name: "formulatext",
summary: "GROUP",
formula:
"CASE WHEN {vendor.custentity_2663_email_address_notif} IS NULL THEN {vendor.email} ELSE {vendor.custentity_2663_email_address_notif} END",
label: "Email",
}),
search.createColumn({
name: "total",
summary: "SUM",
label: "Amount (Transaction Total)",
}),
search.createColumn({
name: "currency",
summary: "GROUP",
label: "Currency",
}),
search.createColumn({
name: "trandate",
summary: "GROUP",
sort: search.Sort.ASC,
label: "Date",
}),
search.createColumn({
name: "internalid",
summary: "GROUP",
label: "internalid",
}),
search.createColumn({
name: "internalid",
join: "vendor",
summary: "GROUP",
label: "Internal ID",
}),
search.createColumn({
name: "internalid",
summary: "GROUP",
label: "Internal ID",
}),
],
});
var searchResultCount = vendorpaymentSearchObj.runPaged().count;
log.debug("vendorpaymentSearchObj result count", searchResultCount);
vendorpaymentSearchObj.run().each(function (result) {
var emailAddress = result.getValue(result.columns[2]);
var transactionNumber = result.getValue(result.columns[0]);
var amount = result.getValue(result.columns[3]);
var date = result.getValue(result.columns[5]);
var vendor = result.getValue(result.columns[1]);
var resultId = result.getValue(result.columns[6]);
var vendorId = result.getValue(result.columns[7]);
var transactionId = result.getValue(result.columns[8]);
log.debug(
"emailAddress: ",
emailAddress +
" transaction bumber: " +
transactionNumber +
" amount: " +
amount +
" date: " +
date +
" vendor: " +
vendor +
" resultId " +
resultId +
" transactionId " +
transactionId
);
var pdfFile = render.transaction({
entityId: parseInt(resultId),
printMode: render.PrintMode.PDF,
formId: 109,
inCustLocale: true,
});
pdfFile.folder = 1351;
var fileId = pdfFile.save();
var pdffile2 = file.load({ id: fileId });
// var fileObj = file.load({ id: parseInt(fileId) });
var mergeResult = render.mergeEmail({
templateId: 8,
// entity: {
// type: "employee",
// id: parseInt(recipient),
// },
entity: {
type: "vendor",
id: parseInt(vendorId),
},
recipient: {
type: "vendor",
id: parseInt(vendorId),
},
supportCaseId: null,
transactionId: parseInt(resultId),
customRecord: null,
});
var emailSubject = mergeResult.subject;
var emailBody = mergeResult.body;
//create a placeholder in the original HTML with an element called NLID. This will replace that with a value that is part of the script
emailSubject = emailSubject.replace("NLVENDOR", vendor);
// emailBody = emailBody.replace("NLDOCNUMBER", bankDetailsId);
var emailString = JSON.stringify(emailAddress);
email.send({
author: -5,
// recipients: 2020,
recipients: emailAddress,
subject: emailSubject,
body: emailBody,
attachments: [pdffile2],
relatedRecords: {
entity: parseInt(vendorId),
customRecord: {
id: parseInt(id),
recordType: 'customrecord_2663_file_admin', //an integer value
},
// CAUSES AN ERROR transactionId: 38326,
},
});
return true;
});
}
return {
onAction: onAction,
};
});
Another issue I am having with this script is the email.send method doesn't throw an error for the custom record but doesn't actually attach the email messages to the stated transaction type either. It doesn't allow me to attach the emails to a transaction at all (I get an 'unexpected error' if I do)
thanks