Zac*_*han 2 javascript rest netsuite
我被困在一个可能很多新的 SuiteScript黑客都会遇到的问题上。
正如SuiteScript p.的官方文档所写的那样。243,有这个 JS 用于使用 GET 方法检索记录。
// Get a standard NetSuite record
function getRecord(datain) {
return nlapiLoadRecord(datain.recordtype, datain.id); // e.g recordtype="customer", id="769"
}
// http://rest.na1.netsuite.com/app/site/hosting/restlet.nl?script=22&deploy=1&recordtype=customer&id=769
Run Code Online (Sandbox Code Playgroud)
但是,当我在 NetSuite 端尝试EXACT片段时,datain.recordtype未定义。(并且返回应该只返回文本,顺便说一句。)
幸运的是,我自己找到了解决方案。在下面检查我的答案。
function getRecord(datain) {
return nlapiLoadRecord(datain.recordtype, datain.id); // e.g recordtype="customer", id="769"
}
// http://rest.na1.netsuite.com/app/site/hosting/restlet.nl?script=22&deploy=1&recordtype=customer&id=769
Run Code Online (Sandbox Code Playgroud)
—
SuiteScriptdatain不是作为对象或 JSON填充,而是作为字符串填充(因为我仍然忽略的原因。)
您要做的只是在解析它之前,然后使用点符号访问 JSON。
function getRecord(datain) {
var data = JSON.parse(datain); // <- this
return "This record is a " + data.recordtype + " and the ID is " + data.id;
}
// http://rest.na1.netsuite.com/app/site/hosting/restlet.nl?script=22&deploy=1&recordtype=customer&id=769
Run Code Online (Sandbox Code Playgroud)
我已经更改了解决方案中的 return 语句,因为当我尝试返回不是文本的内容时,SuiteScript 给了我错误。
正如egrubaugh360所说,在您的查询脚本中指定Content-Typeis application/json(调用您的 SuiteScript 脚本的那个)
所以如果你像我一样处理 Node.js,它会给出这样的东西:
var options = {
headers: {
'Authorization': "<insert your NLAuth Authentification method here>",
"Content-Type" : "application/json" // <- this
}
}
https.request(options, function(results) {
// do something with results.
}
Run Code Online (Sandbox Code Playgroud)
希望这会帮助某人。
| 归档时间: |
|
| 查看次数: |
3366 次 |
| 最近记录: |