Memos About Salesforce

Salesforceにハマってたこと!

ネットスイートガバナンス制限

ネットスイートには制限もある。

大体、Usage制限と処理時間制限、もちろん、API制限もあるが、

今回、Usage制限と処理時間制限について説明します。

バッチ処理の場合、定期スクリプトで実装されます。

定期スクリプトの場合、1回当たりの実行でUsageが10,000まで、処理時間が3600秒まですが、

再スケジュールでそのUsageと処理時間をリセットすることができます。

 

 

目次

 

 

  • Usage制限

 

 

  • 処理時間制限

 

 

  • 対策

 

 

 

Usage制限

 

定期スクリプトの場合、1回当たりの実行でUsageが10,000まで

Suitletスクリプトの場合、1回当たりの実行でUsageが10,00まで

 

処理時間制限

 

定期スクリプトの場合、1回当たりの実行で処理時間が3600秒まで

Suitletスクリプトの場合、1回当たりの実行でUsageが300秒まで

 

対策

再スケジュールでそのUsageと処理時間をリセットすることができます

なお、リカバリポイント作成にについて最大50MBの状態が保持されます。

よって、不要になったパラメータをnullにしたりしてスクリプトを軽くする必要があります。

 

もともとの文章

 

nlapiYieldScript()

Creates a recovery point and then reschedules the script.

The newly rescheduled script has its governance units reset,

and is then placed at the back of the scheduled script queue.

To summarize, nlapiYieldScript works as follows:

 

理解すると

 

nlapiYieldScript()は定期スクリプトの再スケジュールで使われます。

このAPIの実行で、

リカバリポイント作成

②新しい定期スクリプトを起動

③新しい定期スクリプトに①を渡す

 

って感じ

 

例にすると

 

function runScheduledScript(status, queueid)

{

var records = nlapiSearchRecord('customer', 15);

 

for( var i = 0; i < records.length; i++ )

{

handleCustomer(records[i].getRecordType(), records[i].getId());

 

if( (i % 5) == 0 ) setRecoveryPoint(); //every 5 customers, we want to set a recovery point so that, in case of an unexpected server failure, we resume from the current "i" index instead of 0

 

checkGovernance();

}

}

/*********************************************************************************************************/

function setRecoveryPoint()

{

var state = nlapiSetRecoveryPoint(); //100 point governance

if( state.status == 'SUCCESS' ) return; //we successfully create a new recovery point

if( state.status == 'RESUME' ) //a recovery point was previously set, we are resuming due to some unforeseen error

{

nlapiLogExecution("ERROR", "Resuming script because of " + state.reason+". Size = "+ state.size);

handleScriptRecovery();

}

else if ( state.status == 'FAILURE' ) //we failed to create a new recovery point

{

nlapiLogExecution("ERROR","Failed to create recovery point. Reason = "+state.reason + " / Size = "+ state.size);

handleRecoveryFailure(state);

}

}

 

/*********************************************************************************************************/

function checkGovernance()

{

var context = nlapiGetContext();

if( context.getRemainingUsage() < myGovernanceThreshold )

{

var state = nlapiYieldScript();

if( state.status == 'FAILURE'

{

nlapiLogExecution("ERROR","Failed to yield script, exiting: Reason = "+state.reason + " / Size = "+ state.size);

throw "Failed to yield script";

}

else if ( state.status == 'RESUME' )

{

nlapiLogExecution("AUDIT", "Resuming script because of " + state.reason+". Size = "+ state.size);

}

// state.status will never be SUCCESS because a success would imply a yield has occurred. The equivalent response would be yield

}

}

 

/*********************************************************************************************************/

function handleRecoverFailure(failure)

{

if( failure.reason == 'SS_MAJOR_RELEASE" ) throw "Major Update of NetSuite in progress, shutting down all processes";

if( failure.reason == 'SS_CANCELLED' ) throw "Script Cancelled due to UI interaction";

if( failure.reason == 'SS_EXCESSIVE_MEMORY_FOOTPRINT ) { cleanUpMemory(); setRecoveryPoint(); }//avoid infinite loop

if( failure.reason == 'SS_DISALLOWED_OBJECT_REFERENCE' ) throw "Could not set recovery point because of a reference to a non-recoverable object: "+ failure.information;

}

/*********************************************************************************************************/

function cleanUpMemory(){...set references to null, dump values seen in maps, etc}

 

/**nullにする一例*************************************************************************************/

 

var largeFile = nlapiLoadRecord('1234');

var pdf = nlapiXMLToPDF(largeFile);

largeFile = null;

nlapiYieldScript();