Tuesday, November 27, 2018

Error SS_DISALLOWED_OBJECT_REFERENCE error when using nlapiYieldScript

In use cases wherein a scheduled script needs to process a number (hundreds to thousands) of records, nlapiYieldScript is recommended to be used. If this script though is transforming transactions (example: from Sales Orders to Item Fulfillment), nlapiYieldScript will fail if any of the transformation failed. In this scenario, the error should be caught within a try-catch block and should be handled properly. Here is a sample:

try{
     if (context.getRemainingUsage() <= 100){
          var stateMain = nlapiYieldScript(); 
          if( stateMain.status == 'FAILURE'){ 
               nlapiLogExecution("debug","Failed to yield script (do-while), exiting: Reason = "+ stateMain.reason + " / Size = "+ stateMain.size); 
               throw "Failed to yield script"; 
          } 
          else if ( stateMain.status == 'RESUME' ){ 
               nlapiLogExecution("debug", "Resuming script (do-while) because of " + stateMain.reason+". Size = "+ stateMain.size); 
          } 
     }
}
catch(e){
     if (e instanceof nlobjError) {
          nlapiLogExecution('ERROR', 'System Error', e.getCode() + '\n' + e.getDetails());
     }
     else{
          nlapiLogExecution('ERROR', 'Unexpected Error', e.toString());
     }
}

*As of Version 2013 Release 2, the use of nlapiYieldScript() in scripts with an nlobjError object in their stack trace is now supported.

No comments:

Post a Comment