Kozer H
Kozer H

Reputation: 37

Deleting more than 1000 records in SuiteScript 2.X

I am trying to write a SuiteScript 2.1 script to delete 5000+ records. NetSuite limits us to 1000 record changes at a time and I have attempted to get around this with a Do While loop that I have used previously. However, I cannot figure out how to loop over the array of IDs for the deletion itself. I've tried a counter to do this but it keeps failing to iterate at times. This is my latest code, it does not work

        var idArray = [];
        var recordLocation = 0;
        var counter = 0;
        
        do{
            
            
            var s = compPriceSearch.run();
            var deleteSlice = s.getRange({start: recordLocation, end: recordLocation + 1000});  
            deleteSlice.forEach(function(slice){
                log.debug("Grabbing records IDs");
                id = slice.getValue({
                    name: 'id', 
                    
                });   
                idArray.push(id); 
                // log.debug('The ID is: ', id); 
                log.debug('the counter is: ', counter);
                  
                recordLocation++
                
                
            });
        }while (deleteSlice.length >= 1000){
            log.debug("Number of records to be deleted: ", deleteSlice.length);
            var deleteRecordPromise = record.delete({
                type: 'customrecord_comp_price',
                id: idArray[counter]
            });
            counter++
            return idArray;
            
            
        }

If I just use a basic for loop I can delete 1000 records, however it then fails as there are over 1000 records needed to delete. Looking for a solution to this and appreciate any input.

Upvotes: 0

Views: 807

Answers (1)

Nadav Julius
Nadav Julius

Reputation: 311

Why don't you use a Map/Reduce script?

Build a search with all of the records you want to delete. The initial function will pass the search results one by one to the Map faze. Then you can delete them one at a time in the Map Faze without running into usage limit and/or batch limit issues.

One small note: The search results will return up to 4k results so use the code you have now to create a array with all 5k results and then return that array from the initial function into the map stage.

Upvotes: 1

Related Questions