Roberto Carlos
06/22/2022, 6:36 PM/**
* Takes the subsource parameter and looks for the subsidiary ID corresponding to that soubsource. The field 'custrecord_lw_subsource' is defined to match the subsource parameter to the subsidiaries whose custrecord_lw_subsource == subsource.
* @param subsource
*/
function searchSubsidiaryIdBySubSource(subsource) {
try {
// Creating the search using the N/search module
let mySearch = search.create({
type: search.Type.SUBSIDIARY,
columns: ['internalid', 'name', 'custrecord_lw_subsource'],
filters: ['custrecord_lw_subsource', 'contains', subsource]
});
// Running the search and getting the results.
let myResultSet = mySearch.run();
// Up to 100 results are retrieved.
let resultRange = myResultSet.getRange({
start: 0,
end: 100
});
if (resultRange.length > 0) {
log.debug({
title: "Subsidiary found for " + subsource,
details: resultRange.length + " subsidiary(ies) found for " + subsource + ". The subsidiary with the ID " + resultRange[0].id + " was selected."
});
return resultRange[0].id;
} else return undefined;
}
catch (e) {
log.debug({
title: "Search error: " + e.name,
details: "An error occurred while searching the subsidiary: '" + subsource + "': " + e.message
});
// If an error occurs the subsidiary with the id 5 is given by default.
return "5";
}
}
Although I placed the code inside a try/catch clause, sometimes it's throwing an SSS_INVALID_SRCH_OPERATOR Error. It's strange because the error appears only for some values of the subsource. Moreover, I would expect the system to launch the errors through the catch sentence but it's thrown by another way. Can you suggest something about this strange behavior? I'm afraid of loosing data when I deploy the script to production...Marvin
06/22/2022, 9:18 PMsubsource
is set before running the search.
Something like this before the try/catch would work. (Depends on subsource
type)
if (isNaN(parseInt(subsource))) return;
battk
06/23/2022, 3:35 AMsubsource
is textbattk
06/23/2022, 3:36 AMif (!subsource) {
// throw error or return early here
}
Roberto Carlos
06/23/2022, 7:11 PMMarvin
06/23/2022, 7:51 PM'contains'
with search.Operator.CONTAINS
. Just in case the enum value is different.
Are you returning or throwing an error to make sure it doesn't go any further?Marvin
06/23/2022, 8:00 PM/**
* Takes the subsource parameter and looks for the subsidiary ID corresponding to that soubsource. The field 'custrecord_lw_subsource' is defined to match the subsource parameter to the subsidiaries whose custrecord_lw_subsource == subsource.
* @param subsource
*/
function searchSubsidiaryIdBySubSource(subsource) {
let subsidiary;
if (!subsource) return "5";
// Creating the search using the N/search module
search.create({
type: search.Type.SUBSIDIARY,
columns: ['internalid', 'name', 'custrecord_lw_subsource'],
filters: [{ name: 'custrecord_lw_subsource', operator: search.Operator.CONTAINS, values: subsource }]
})
.run()
.each(subsidiary_result => {
subsidiary = subsidiary_result.id;
});
return subsidiary;
}
Marvin
06/23/2022, 8:53 PM[['custrecord_lw_subsource', 'contains', subsource]]