Getting an error when I'm creating my scripted rec...
# suitescript
g
Getting an error when I'm creating my scripted record... Fail to evaluate script: {"type":"error.SuiteScriptModuleLoaderError","name":"UNEXPECTED_ERROR","message":"missing ; before statement (SS_SCRIPT_FOR_METADATA#27)","stack":[]} Script:
/**
* @NApiVersion 2.0
* @NScriptType Suitelet
*/
define(['N/search'], function(search) {
function onRequest(context) {
// Array of saved search IDs to delete
var savedSearchIds = [
3431,
3411,
];
// Loop through the saved search IDs and delete each one
for (var i = 0; i < savedSearchIds.length; i++) {
search.delete({
id: savedSearchIds[i]
});
}
context.response.write('Saved searches deleted successfully.');
}
return {
onRequest: onRequest
};
});
n
Not jumping out at me apart from :
var savedSearchIds = [
3431,
3411,
];
I'd be inclined to write:
var savedSearchIds = [3431,3411];
you have a trailing comma too in your array ๐Ÿ˜„
n
Yeah same here. Your syntax is technically correct. But i agree with Neil, its probably the extra comma in your array
g
hmm so removed the trailing comma and it uploaded however when try to run, getting error - {"type":"error.SuiteScriptError","name":"UNEXPECTED_ERROR","message":null,"stack":["doDelete(N/searchUtil)","onRequest(/SuiteScripts/del_savedsearch.js:12)"],"cause":{"type":"internal error","code":"UNEXPECTED_ERROR","details":null,"userEvent":null,"stackTrace":["doDelete(N/searchUtil)","onRequest(/SuiteScripts/del_savedsearch.js:12)"],"notifyOff":false},"id":"ba901db2-613d-471a-844b-f5729d61f9fa-2d323032332e30322e3134","notifyOff":false,"userFacing":false}
n
Are any of those three searches for custom records? If the search type is not a standard netsuite search type then the search.delete() function requires the type parameter. Thatโ€™s the only thing I can think of
๐Ÿ‘ 1
g
not custom records... just transaction saved searches... when you get a moment, could either of you try and run the script at your end and see if it works for you? can't think of anything else at this point..
n
Yeah ill try it out
well it worked for me. Ill paste my code below and maybe you can just copy it. I used SDF to build the template so maybe something in there is different enough to make it work
Copy code
/**
 * @NApiVersion 2.1
 * @NScriptType Suitelet
 */
define(["N/search"], /**
 * @param{search} search
 */ (search) => {
  /**
   * Defines the Suitelet script trigger point.
   * @param {Object} scriptContext
   * @param {ServerRequest} scriptContext.request - Incoming request
   * @param {ServerResponse} scriptContext.response - Suitelet response
   * @since 2015.2
   */
  const onRequest = (scriptContext) => {
    // Array of saved search IDs to delete
    var savedSearchIds = [5781, 5780];
    // Loop through the saved search IDs and delete each one
    for (var i = 0; i < savedSearchIds.length; i++) {
      search.delete({
        id: savedSearchIds[i],
      });
    }
    scriptContext.response.write("Saved searches deleted successfully.");
  };

  return { onRequest };
});
Friendly note: I changed the saved search ID's. So be careful or you might accidentally delete the wrong searches
g
thanks Nathan, that did work however only when I changed the saved search ID's I wanted to delete... question - does the saved search need to be public for it to be deleted?
n
No it shouldnt. The two searches i tested with werent public. You may check too that the script is running as administrator. Theres a chance its a weird permission error that youre getting
g
hmm so I executed as administrator and tried to delete 1 search created by another user and it wasn't public, result was it failed and then as soon as I made it public and ran again, it got deleted...
๐Ÿ˜• 2
n
I guess that makes sense in a weird way. I wonder if in your code you could update the search to be public before deleting it.
Copy code
let savedSearch = search.load({
  id: 1234
});

savedSearch.isPublic = true;

savedSearch.save();

search.delete({id: 123});