HOW TO: Return tranid for NetSuite imports using SuiteScript hooks
Within all of Celigo's import steps, you have the option to response map returned api data to your record in Celigo. Typically, most apis will respond back with an id of the record created/updated or with the entire object of the created/updated record. In the case of NetSuite imports, the only available field to response map is the id of the record created (disregarding errors, ignored flag, and status code). However, there are some cases where you may want to get additional fields from the new record so that those fields can also be sent downstream. In the below example, it walks you through how to return the tranid (aka document number) for transactions being created and updated in NetSuite by utilizing postSubmit SuiteScript hooks.
To set this up, follow these steps:
- Create your import step in Celigo, add a step after the import step, then come back to the first import step and setup response mapping rules. In this case, I've mapped _json and id to the record.
- Add the script below in NetSuite under Documents>Files>SuiteScripts and record the internal id of the script after uploading.
- Go back to Celigo and add the script function name and script internal id to the NetSuite export preSend script fields.
- Lastly, you should get an output that looks like this:
/**
*@NApiVersion 2.x
*/
define(['N/search', 'N/record'], function(search, recordObj) {
function getTranId(options) {
var response = [];
for (var i = 0; i < options.responseData.length; i++) {
var clone = options.responseData[i];
response.push({
statusCode: clone.statusCode || 200,
id: clone.id,
errors: clone.errors || [],
ignored: clone.ignored || false,
_json: {
tranid: ''
}
});
}
logAudit('PostSubmit Response', JSON.stringify(response));
for (var i = 0; i < response.length; i++) {
var record = response[i];
try {
if (record.statusCode !== 200 || record.ignored) {
continue;
}
var transactionId = record.id;
// Create and run the search
var tranIdSearch = search.create({
type: search.Type.TRANSACTION,
filters: [
['internalid', search.Operator.IS, transactionId]
],
columns: [
'tranid'
]
});
var searchResult = tranIdSearch.run().getRange({
start: 0,
end: 1
});
if (searchResult.length > 0) {
var tranId = searchResult[0].getValue({
name: 'tranid'
});
logAudit('Transaction ID for Record ' + transactionId, tranId);
record._json.tranid = tranId;
} else {
logAudit('No results found for Record', transactionId);
}
} catch (e) {
logError('ERROR', e.name + ' ' + e.message);
record.statusCode = 422;
record.errors.push({
code: e.name,
message: e.message
});
}
}
return response;
}
function logError(title, message) {
log.error({
title: title,
details: message
});
}
function logAudit(title, message) {
log.audit({
title: title,
details: message
});
}
return {
getTranId: getTranId
}
});
Please sign in to leave a comment.
Comments
0 comments