This topic contains 9 replies, has 0 voices, and was last updated by Jordan Manningham 7 years, 11 months ago.

  • Author
    Posts
  • #5702

    Jordan Manningham

    Hey everyone,

    I’ve got a strange error in my save record client script. In my code a search is loaded, runs, and is saved afterwards using the same name and internal id. Normally it runs great and there are no problems. But occasionally the script will throw a NAME_ALREADY_IN_USE despite having been saved with the same name countless times before.

    I can post my code if needed. Just wondering if anyone has came across this before.

    Thanks in advance!

    Jordan
    This is a cached copy. Click here to see the original post.

  • #5703

    Shanthi Vedulla

    Hi Jordon, I haven’t come across anythng like this before. Would you be able to post the code so I could see what the script is doing. From what you mentioned, it seems to be a very inconsistent behavior.

    Were you able to get this resolved?

  • #5704

    Jordan Manningham

    Code:
    var newfilter1 = new nlobjSearchFilter(‘custrecord192’, null, ‘is’, nlapiGetFieldValue(‘tranid’));
    var newfilter2 = new nlobjSearchFilter(‘custrecord182’, null, ‘is’, 1); // Create search filter for Actual records, not budgets
    var newfilter3 = new nlobjSearchFilter(‘isinactive’, null, ‘is’, false); // Create search filter to exclude inactive records
    var loadedsearch = nlapiLoadSearch(‘customrecord134’, ‘customsearch665’); // Load search of Newly Created BvA Records
    loadedsearch.setFilters([]); // Set the filters of the search to NONE
    loadedsearch.setIsPublic(true); // Set the search to be public
    var newsearch2 = loadedsearch.saveSearch(‘Newly Created BvA Records’, ‘customsearch665’); // Save the search using the same name
    var loadedsearch2 = nlapiLoadSearch(‘customrecord134’, ‘customsearch665’); // Load the search again
    loadedsearch2.addFilter(newfilter1); // Add LMD filter
    loadedsearch2.addFilter(newfilter2); // Add Actual filter
    loadedsearch2.addFilter(newfilter3); // Add inactive filter
    loadedsearch2.setIsPublic(true); // Set the search to public
    var newsearch1 = loadedsearch2.saveSearch(‘Newly Created BvA Records’, ‘customsearch665’); // Save the search again using the same name
    var loadedsearch1 = nlapiLoadSearch(‘customrecord134’, ‘customsearch665’); // Load the search again
    var getData = loadedsearch1.runSearch(); // Runs the search
    var all_results1 = getData.getResults(0, 1000); // Return all results from the search
    At this point, if the search is loaded and saved correctly, it runs and some actions are done to a custom record. Then the search is saved again at the very end of the script.

    Code:
    var loadedsearch3 = nlapiLoadSearch(‘customrecord134’, ‘customsearch665’); // Load Newly Created BvA Record search
    loadedsearch3.setIsPublic(true); // Set the search to public
    var newsearch2 = loadedsearch3.saveSearch(‘Newly Created BvA Records’, ‘customsearch665’); // Save the search for future use
    return true;

  • #5705

    Shanthi Vedulla

    When an existing Search is loaded and Saved in the script, the title and the id of the Search doesn’t have to be supplied to the saveSearch function.

    Eg: var newSearch2 = loadedsearch3.saveSearch();

    The above line should save the loaded Search with the same id and title.

    Can you try this code and let me know if the issue is resolved?

  • #5706

    Jordan Manningham

    Ahh, not sure why that didn’t occur to me. For some reason I assumed those parameters were required.

    I’ve adjusted all the instances of saveSearch(title, id) to saveSearch(). I will let you know how this solution works.

    Thanks!

  • #5707

    david.smith

    I’m curious, why do you need to save the search from your code? Is the search used in the UI for something?

  • #5708

    Jordan Manningham

    The purpose – > Load the search, remove any filters, save. Load the search, add 3 filters (criteria), and save the search. Load the search a third time and run it with the filters. The reason I want to load it and save it so many times is because I noticed that it will save those 3 filters over and over again -> resulting in hundreds of the same criteria that significantly reduces system performance. This method I chose will ensure that the search does not get bogged down with tons of filters each time the script runs. I’m not the best at scripting and I’m aware this is not the most efficient process, but it works 99% of the time.

    Unfortunately, after making the above change from saveSearch(title, id) to saveSearch(), users still get the error. I’ve picked up on a trend – the error only happens when the user who executed the script is not the same as the user who previously executed the script.

    Not sure why this would happen.

  • #5709

    k_dunc

    Why don’t you just call your Saved Search, and add your filters ‘on the fly’ as you perform your search? I.e., don’t re-save your Saved Search each time, but basically make your Saved Search with the minimum criteria you want, then in your script, call your Saved Search, and add your extra filters that you need as part of the call to run the search?

  • #5710

    david.smith

    I still don’t understand why you are saving the search. If it is not being used in the UI there is no need to save the search. You’re just using extra governance that is not necessary (and causing errors). If you want to load the initial search from a saved search I can understand that. Then you just need to add your additional filters like k_dunc suggested.

    From what you have posted, I think it would be a better practice if you just created the search on the fly in your code using nlapiSearchRecord and adjust your filters each time you need to search for data.

  • #5711

    Jordan Manningham

    Thank you so much k_dunc david.smith Shanthi Vedulla. I’ve revised my script so that the search is not saved and loaded multiple times. The search is saved outside of the script with one filter. When the script loads it, 3 filters are added, and it runs – no saving. This definitely cut down on the waiting time for our accounting staff to invoice customers. Before, one line item on an invoice would take ~15 seconds to save, now it takes ~7 seconds!

You must be logged in to reply to this topic.