netsuiterestlet

netsuiterestlet,netsuite,restlet,suitescript,Netsuite,Restlet,Suitescript,可以检索、删除或创建的Netsuite RESTlet示例 有人能从理论上解释一下这个代码到底在做什么吗 参考: /** *@NApiVersion 2.x *@NScriptType Restlet */ 定义(['N/record','N/error'], 功能(记录、错误){ 函数doValidation(参数、参数名、方法名){ 对于(变量i=0;i

可以检索、删除或创建的Netsuite RESTlet示例

有人能从理论上解释一下这个代码到底在做什么吗

参考:

/**
*@NApiVersion 2.x
*@NScriptType Restlet
*/
定义(['N/record','N/error'],
功能(记录、错误){
函数doValidation(参数、参数名、方法名){
对于(变量i=0;i
示例代码创建了一个通用restlet,允许您对netsuite中的任何记录类型执行通用crud操作

  • 当使用http
    get
    动词调用此restlet时,它将返回指定的记录(由recordtype和id指定)
  • 当使用http
    post
    动词调用此restlet时,restlet将创建指定的记录(由recordtype和id指定)
  • 当使用http
    put
    动词调用此restlet时,restlet将更新指定的记录(由recordtype和id指定)
  • 当使用http
    delete
    动词调用此restlet时,restlet将删除指定的记录(由recordtype和id指定)
/**
 *@NApiVersion 2.x
 *@NScriptType Restlet
 */
define(['N/record', 'N/error'],
        function(record, error) {
    function doValidation(args, argNames, methodName) {
        for (var i = 0; i < args.length; i++)
            if (!args[i] && args[i] !== 0)
                throw error.create({
                    name: 'MISSING_REQ_ARG',
                    message: 'Missing a required argument: [' + argNames[i] + '] for method: ' + methodName
                });
    }
    // Get a standard NetSuite record
    function _get(context) {
        doValidation([context.recordtype, context.id], ['recordtype', 'id'], 'GET');
        return JSON.stringify(record.load({
            type: context.recordtype,
            id: context.id
        }));
    }
    // Delete a standard NetSuite record
    function _delete(context) {
        doValidation([context.recordtype, context.id], ['recordtype', 'id'], 'DELETE');
        record.delete({
            type: context.recordtype,
            id: context.id
        });
        return String(context.id);
    }
    // Create a NetSuite record from request params
    function post(context) {
        doValidation([context.recordtype], ['recordtype'], 'POST');
        var rec = record.create({
            type: context.recordtype
        });
        for (var fldName in context)
            if (context.hasOwnProperty(fldName))
                if (fldName !== 'recordtype')
                    rec.setValue(fldName, context[fldName]);
        var recordId = rec.save();
        return String(recordId);
    }
    // Upsert a NetSuite record from request param
    function put(context) {
        doValidation([context.recordtype, context.id], ['recordtype', 'id'], 'PUT');
        var rec = record.load({
            type: context.recordtype,
            id: context.id
        });
        for (var fldName in context)
            if (context.hasOwnProperty(fldName))
                if (fldName !== 'recordtype' && fldName !== 'id')
                    rec.setValue(fldName, context[fldName]);
        rec.save();
        return JSON.stringify(rec);
    }
    return {
        get: _get,
        delete: _delete,
        post: post,
        put: put
    };
})