首页 文章

对于用Netsuite编写的套件,无法弄清楚如何使用post方法

提问于
浏览
0

我正在尝试使用post方法进行简单的setscript程序,我对此非常陌生 .

在Netsuite我写了一个如下的套件 .

function restPost()
 {   
     var i = nlapiLoadRecord('department', 115); 

     var memo = nlapisetfieldvalue('custrecord225', ' ');// this is a customfield, which i want to populate the memo field, using rest client in firefox

     var recordId = nlapiSubmitRecord(i);

}

我创建了一个脚本记录并上传了这个套件,甚至复制了外部URL以将其粘贴到restclient中 .

在Restclient(firefox插件)中,粘贴了外部URL,我已经给出了方法post,header authorization given,content-type:application / json,并且在body中我放入了 {"memo":"mynamehere"};

在这个我得到的错误是

参数列表后面的消息“:”缺失

我甚至通过编写其他套件程序来尝试它,我得到的错误如下:

对象文字中的意外标记(null $ lib#3)空JSON字符串无效的数据格式 . 你应该返回TEXT .

我是编程世界的新手,所以任何帮助都会非常好 .

4 回答

  • 1

    我想你正在尝试为POST方法创建一个RESTlet . 以下是POST方法的示例代码 -

    function createRecord(datain)
    {
    var err = new Object();
    
    // Validate if mandatory record type is set in the request
    if (!datain.recordtype)
    {
        err.status = "failed";
        err.message= "missing recordtype";
        return err;
    }
    
    var record = nlapiCreateRecord(datain.recordtype);
    
    for (var fieldname in datain)
    {
     if (datain.hasOwnProperty(fieldname))
     {
         if (fieldname != 'recordtype' && fieldname != 'id')
         {
             var value = datain[fieldname];
             if (value && typeof value != 'object') // ignore other type of parameters
             {
                 record.setFieldValue(fieldname, value);
             }
         }
     }
    }
    var recordId = nlapiSubmitRecord(record);
    nlapiLogExecution('DEBUG','id='+recordId);
    
    var nlobj = nlapiLoadRecord(datain.recordtype,recordId);
    return nlobj;
    }
    

    因此,在部署此RESTlet之后,您可以通过传递以下示例JSON有效内容来调用此POST方法 -

    {"recordtype":"customer","entityid":"John Doe","companyname":"ABCTools Inc","subsidiary":"1","email":"jdoe@email.com"}
    

    对于授权,您必须按如下方式传递请求标头 -

    var headers = {
               "Authorization": "NLAuth nlauth_account=" + cred.account + ", nlauth_email=" + cred.email + 
                                ", nlauth_signature= " + cred.password + ", nlauth_role=" + cred.role,
               "Content-Type": "application/json"};
    
  • 1

    我能理解您的要求,Parsun和NetSuite-Expert发布的答案很好 . 您可以遵循该代码 . 这是一个通用代码,可以接受没有子记录的任何主记录 . 例如没有联系人或地址簿的客户 .

    我想建议代码中的一个小改动,我已经在我的解决方案中给出了它 .

    Changes Below

    var isExistRec = isExistingRecord(objDataIn);
                var record = (isExistRec) ? nlapiLoadRecord(objDataIn.recordtype, objDataIn.internalid, {
                    recordmode: 'dynamic'
                }) : nlapiCreateRecord(objDataIn.recordtype);
    

    //使用自定义函数检查Netsuite中是否存在记录

    function isExistingRecord(objDataIn) {
            if (objDataIn.internalid != null && objDataIn.internalid != '' && objDataIn.internalid.trim().length > 0)
                return true;
            else
                return false;
        }
    

    因此,每当您将JSON数据传递给REStlet时,请记住您必须将internalid,recordtype作为必需值传递 .

    谢谢
    弗雷德里克

  • 0

    我相信你会希望 return 来自你的功能 . 一个空对象应该没问题,或类似 {success : true} .

  • 0

    欢迎来到Netsuite Suitescripting @Vin :)

    我强烈建议您在NS帮助中心中查看 SuiteScript API OverviewSuiteScript API - Alphabetized Index ,这是了解Suitescripting基础知识的唯一且最明显的地方 .

    nlapiLoadRecord(type,id,initializeValues)从系统加载现有记录并返回包含该记录的所有字段数据的nlobjRecord对象 . 然后,您可以使用返回的记录对象上可用的方法从加载的记录中提取所需的信息 . 此API是核心API . 它在客户端和服务器上下文中均可用 .

    function restPost(dataIn) {   
              var record = nlapiLoadRecord('department', 115); // returns nlobjRecord
              record.setFieldValue('custrecord225', dataIn.memo); //  set the value in custom field
              var recordId = nlapiSubmitRecord(record);
              return recordId;
            }
    

相关问题