ExploreConsulting / netsuite-fasttrack-toolkit-ss2

NFT for SuiteScript 2.X
74 stars 20 forks source link

Add support for rescheduleIfNeeded to take a callback function for the parameter object #86

Open steveklett opened 10 months ago

steveklett commented 10 months ago

I've encountered this before and just encountered it again. Pretty typical LazySearch use case in a Scheduled Script where I want the script to reschedule itself. In my case I'm passing along process state via the script parameters to support execution resuming from where it left off. The problem is that this state data is updated after the call to rescheduleIfNeeded.

Here's an example:

let lastTransactionIID = 0

export function _makeRescheduleParam(lastProcessedAgingIID: number): object {
  return {
    custscript_gadd_generation_run_id: _getSettings().generationRunID,
    custscript_gadd_last_proc_item_age_iid: lastProcessedAgingIID
  }
}

Seq(LazySearch.from(NV._getPreviousActiveAgingDetailSearch()))
  .takeWhile(rescheduleIfNeeded(governanceRemains(), NV._makeRescheduleParam(lastTransactionIID)))
  .forEach(result => {
    record.submitFields({
      type: 'customrecord_inventoryagingdetail',
      id: result.id,
      values: {
        isinactive: true
      }
    })
    lastTransactionIID = result.id
  })

If we could pass a function we could have something like this:

// ...

Seq(LazySearch.from(NV._getPreviousActiveAgingDetailSearch()))
  .takeWhile(rescheduleIfNeeded(governanceRemains(), () => NV._makeRescheduleParam(lastTransactionIID)))
  .forEach(result => {
    record.submitFields({
      type: 'customrecord_inventoryagingdetail',
      id: result.id,
      values: {
        isinactive: true
      }
    })
    lastTransactionIID = result.id
// ...

There's probably a more graceful way to accomplish, but I think you get the idea.