I've never had an issue loading a saved search bef...
# suitescript
m
I've never had an issue loading a saved search before. A search a txt file surely shouldn't burn up the governance?!
v
I doubt it's the actual search. It's more likely to be the iterator, but you haven't shared your code, so we have no idea
m
Ah, I hope so! Here' full code: anything look odd? const sourceTagStart = "<AddtlRmtInf>"; const sourceTagEnd = "</AddtlRmtInf>"; const destinationTagStart = "<AddtlNtryInf>"; const destinationTagEnd = "</AddtlNtryInf>"; const fileIds = getFileIDFromSearch(search); function execute(context) { log.debug({title: "File IDs", details: fileIds}); fileIds.forEach(fileId => { const myFile = file.load({ id: fileIds, }); log.debug({title: "File ID", details: myFile}); let fileContent = myFile.getContents(); let targetValue; let startIndex = 0; while (startIndex < fileContent.length) { log.debug({title: "Current Target Value", details: targetValue}); const acSvcrRefStart = fileContent.indexOf("<AcctSvcrRef>", startIndex); const acSvcrRefEnd = fileContent.indexOf("</AcctSvcrRef>", startIndex); if (acSvcrRefStart === -1 || acSvcrRefEnd === -1) { break; // Reached the end of content or invalid structure } const acSvcrRefValue = fileContent.substring(acSvcrRefStart + 13, acSvcrRefEnd); // Extract value between tags const rmtInfStart = fileContent.indexOf(sourceTagStart, acSvcrRefEnd); if (acSvcrRefValue === "Cash Pooling" && rmtInfStart !== -1) { targetValue = fileContent.substring(rmtInfStart + sourceTagStart.length, fileContent.indexOf(sourceTagEnd, rmtInfStart)); log.debug({title: "Found Cash Pool Segment inside AcctSvcrRef> tag", details: acSvcrRefValue}); log.debug({title: "Current Target Value", details: targetValue}); const endIndex = fileContent.indexOf(destinationTagEnd, rmtInfStart); if (endIndex !== -1) { const existingAddtlNtryInf = fileContent.substring(rmtInfStart, endIndex + destinationTagEnd.length); log.debug({title: "Existing Tag Value", details: existingAddtlNtryInf}); const updatedAddtlNtryInf = existingAddtlNtryInf.replace(destinationTagEnd,
| ${targetValue}${destinationTagEnd}
); log.debug({title: "updatedAddtlNtryInf", details: updatedAddtlNtryInf}); fileContent = fileContent.substring(0, rmtInfStart) + updatedAddtlNtryInf + fileContent.substring(endIndex + destinationTagEnd.length); log.debug("Updated Tag Value"); } else { log.debug("Couldn't find closing tag for the AddtlNtryInf following Cash Pooling"); } break; } startIndex = acSvcrRefEnd + "</AcctSvcrRef>".length; // Move to next AcctSvcrRef } if (!targetValue) { log.debug({title: "No matching AddtlRmtInf found for Cash Pooling"}); return; } // Save the updated file const updatedFile = file.create({ name: myFile.name, fileType: myFile.fileType, contents: fileContent, folder: myFile.folder, }); const updatedFileId = updatedFile.save(); log.debug({title: "Updated File ID", details: updatedFileId}); }); } function getFileIDFromSearch(search) { log.debug("Here 1"); const savedSearchId = 'customsearch_my_files_lookup'; log.debug(savedSearchId); const fileSearch = search.load({ id: savedSearchId }); log.debug("Here 2"); const filesToModify = []; fileSearch.run().each(function (result) { // Get the file ID from the search result const fileId = result.getValue({ name: "internalid", join: "file", }); log.debug({title: "File ID", details: fileId}); const fileName = result.getValue({ name: "name", join: "file" }); log.debug({title: "File Name", details: fileName}); const fileToModify = checkFile(fileName); log.debug({title: "File Requires Modification", details: fileToModify}); if (fileToModify) { filesToModify.push(fileId); } return true; }); return filesToModify; } function checkFile(fileName) { const lowerCaseFileName = fileName.toLowerCase(); return lowerCaseFileName.includes('ldn'); // Return true if the file name contains 'ldn' }
v
lol, I recognise those XML tags
m
Ha. Not nice!
e
Hm I'd expect that to throw a much different error. You're calling
getFileIDFromSearch
from outside the entry point, and that typically generates an error indicating that you can't do that
Struggling to recall/locate the text of the error
ah, this is the error I'd actually expect to see. Unless your
execute
function is not what is assigned to the entry point. Since I cannot see the whole module, I am making several assumptions based solely on the name of that function
šŸ‘ 1
a
The odds are that your code is not failing where you think it is failing; you are loading a search, and for each result, you are calling
checkFile(fileName)
, then guessing here (because the code you shared is not complete), you are iterating over those file IDs with a `forEach`: • each file
load
is going to consume governance as well as each
getContent
call. •
break
is not valid in a
forEach
and/or it would not do what you think it should.
šŸ‘ 1
šŸ‘šŸ» 1
m
Thanks all, you input helped a lot. The issue indeed wasn't where I thought it was and I've made few changes and it now seems to run the search properly.