netsuite 库存同步机制

应用调用:
nlapiServerCall('/app/accounting/transactions/inventory/validateInventoryNumbers.nl', 'getValidationScript', [params], eval);

实现过程:---------------------   
* @param url URL of request handler    
* @param methodName method name on remote object to call    
* @param methodParams an array of parameters to the method    
* @param asyncCallback a callback if this is to be an async request. Callback signature should be: callback(result, error)    
*/    
function nlapiServerCall(url, methodName, methodParams, asyncCallback)    
{    
return nsServerCall(url, methodName, methodParams, asyncCallback);    
}    
    
function nlapiLocalCall(func, timeoutinmillis)    
{    
nsapiAssertTrue(timeoutinmillis != null && !isNaN(parseInt(timeoutinmillis)), 'SSS_INVALID_ARGUMENT', 'timeoutinmillis: '+timeoutinmillis)    
var obj = new Object()    
obj.trigger = nsapiQueryScript("trigger")    
obj.scriptid = nsapiQueryScript("scriptid")    
if ( timeoutinmillis == -1 )    
return nsapiCallScript(obj.trigger, obj.scriptid, func)    
return setTimeout( function() { nsapiCallScript(obj.trigger, obj.scriptid, func); }, timeoutinmillis );    
}    
----------------------------------------
/**
* @param url URL of request handler
* @param methodName method name on remote object to call
* @param methodParams an array of parameters to the method
* @param asyncCallback a callback if this is to be an async request. Callback signature should be: callback(result, error)
*/
function nsServerCall(url, methodName, methodParams, asyncCallback)
{
var client = new NLJsonRpcClient(url);
return client.sendRequest(methodName, methodParams, asyncCallback);
}

NLJsonRpcClient = function (serverURL)
{
if (serverURL.indexOf("?") > 0)
serverURL = serverURL + "&jrpc=T";
else
serverURL = serverURL + "?jrpc=T";
this.serverURL = serverURL;
this.responseCallbackMap = {};
};
NLJsonRpcClient.requestId = 0;
NLJsonRpcClient.prototype =
{
sendRequest : function (methodName, args, callback)
{
var jsonRpcReq = {
id : NLJsonRpcClient.requestId++,
method : "remoteObject." + methodName,
params : args || []
};
if (callback != null)
this.responseCallbackMap[jsonRpcReq.id] = callback;
var request = new NLXMLHttpRequest();
if (callback != null)
request.setResponseHandler(this.handleResponseAsync.bindAsEventListener(this));
var response = request.requestURL(this.serverURL, toJSON(jsonRpcReq), null, callback != null ? true : false);
if (callback == null)
{
var jsonRpcResp = this.getJsonRpcResponse(response);
if (jsonRpcResp.error)
throw new NLXMLResponseError(jsonRpcResp.error.code, jsonRpcResp.error.trace, jsonRpcResp.error.msg);
response = jsonRpcResp.result;
}
return response;
},

getJsonRpcResponse : function (nlXMLResponseObj)
{
var jsonRpcResp = nlXMLResponseObj.getBody();
if (jsonRpcResp != null)
jsonRpcResp = jsonRpcResp.replace(/^\s*<!--[\s\S]*?-->\s*$/gm, '');
eval("jsonRpcResp = " + jsonRpcResp + ";");
return jsonRpcResp;
},

handleResponseAsync : function (nlXMLResponseObj)
{
var jsonRpcResp = this.getJsonRpcResponse(nlXMLResponseObj);
var callback = this.responseCallbackMap[jsonRpcResp.id];
this.responseCallbackMap[jsonRpcResp.id] = null;
callback(jsonRpcResp.result, jsonRpcResp.error);
}
}

function toJSON(o)
{
if (o == null)
return "null";
else if(o.constructor == String || o.constructor.name == "String")
return escapeJSONString(o);
else if(o.constructor == Number || o.constructor.name == "Number")
return o.toString();
else if(o.constructor == Boolean || o.constructor.name == "Boolean")
return o.toString();
else if(o.constructor == Date || o.constructor.name == "Date")
return '{javaClass: "java.util.Date", time: ' + o.valueOf() +'}';
else if((o.constructor == Array || o.constructor.name == "Array"))
{
var v = [];
for (var i = 0; i < o.length; i++) v.push(toJSON(o[i]));
return "[" + v.join(", ") + "]";
}
else
{
var v = [];
for(attr in o)
{
if(o[attr] == null) v.push("\"" + attr + "\": null");
else if(typeof o[attr] == "function"); /* skip */
else v.push(escapeJSONString(attr) + ": " + toJSON(o[attr]));
}
return "{" + v.join(", ") + "}";
}
}

纠正错误,欢迎探讨:
打开微信-发现-扫一扫
原文地址:https://www.cnblogs.com/backuper/p/1552463.html