Suitescript RESTlet data get Error Empty JSON string - javascript

I have issue using POST Function, I want to transform Transfer Order to Item Receipt, But when I tried to execute the script, it failed with error "org.mozilla.javascript.EcmaError: SyntaxError: Empty JSON string (INVOCATION_WRAPPER$sys#24".
Anyone can help me about my issue ?
Here is my Suitescript Code :
function postData (receiptItem) {
doValidation([receiptItem.recordtype], ['recordtype'], 'POST');
if (receiptItem.recordtype == 'transferorder') {
var recordId = [];
var recStr = [];
var objRecord = record.transform({
fromType: record.Type.TRANSFER_ORDER,
fromId: 131, // transfer Order internalid
toType: record.Type.ITEM_RECEIPT,
defaultValues: {
customform: '433'}
});
var itemReceiptId = objRecord.save({
enableSourcing: false,
ignoreMandatoryField: false
});
recordId.push(itemReceiptId)
log.debug({
"title": "[success] recordId: ",
"details": recordId
});
var recLoad = record.load({
type: receiptItem.recordtype,
id: recordId.getValue('internalid')
});
recStr.push({
use_form: recLoad.getText('customform'),
tran_id: recLoad.getValue('tranid'),
tran_date: recLoad.getValue('trandate'),
tran_from: recLoad.getValue('transferlocation'),
tran_to: recLoad.getValue('location'),
tran_ord_id: recLoad.getvalue('createdfrom'),
tran_memo: recLoad.getValue('memo')
});
log.debug({
"title": "recStr",
"details": recStr
});
return recStr;
}
}
return {
post: postData
};
});

It's a syntax error, very likely some misplaced bracket.

Make sure you are posting valid JSON and that the content type is set on your request

You must send data as JSON. if empty or no data then send {} at least in POST as ContentType is Application/JSON. ECMA Standard. The error message clearly states that "Empty JSON string".
Then try this in Postman tool.

Related

What is wrong with my JSON output for a Slack Message payload?

I have set up what I think should be a working JSON output to send a message in slack but Slack keeps rejecting it.
I have tried multiple different message layout formats using the guides on slack's api site, but so far the only method that has successfully sent is a fully flat JSON with no block formatting.
function submitValuesToSlack(e) {
var name = e.values[1];
var caseNumber = e.values[2];
var problemDescription = e.values[3];
var question = e.values[4];
var completedChecklist = e.values[5];
var payload = [{
"channel": postChannel,
"username": postUser,
"icon_emoji": postIcon,
"link_names": 1,
"blocks": [
{
"type": "section",
"fields": [
{
"type": "mrkdwn",
"text": "*Name:*\n " + name
}
]
}]
}];
console.log(JSON.stringify(payload, null, "\t"));
var options = {
'method': 'post',
'payload': JSON.stringify(payload)
};
console.log(options)
var response = UrlFetchApp.fetch(slackIncomingWebhookUrl, options);
}
When I run this, I get the following output:
[
{
"channel":"#tech-support",
"username":"Form Response",
"icon_emoji":":mailbox_with_mail:",
"link_names":1,
"blocks":[
{
"type":"section",
"fields":[
{
"type":"mrkdwn",
"text":"*Name:*\n test"
}
]
}
]
}
]
Which I believe is correct, however slack api just rejects it with an HTTP 400 error "no text"
am I misunderstanding something about block formatting?
EDIT:
To Clarify, formatting works if I use this for my JSON instead of the more complex format:
{
"channel":"#tech-support",
"username":"Form Response",
"icon_emoji":":mailbox_with_mail:",
"link_names":1,
"text":"*Name:*\n test"
}
The reason you are getting the error no_text is because you do not have a valid message text property in your payload. You either need to have a text property as top line parameter (classic style - your example at the bottom) or a text block within a section block.
If you want to put to use blocks only (as you are asking) the section block is called text, not fields. fields is another type of section bock that has a different meaning.
So the correct syntax is:
[
{
"channel":"#tech-support",
"username":"Form Response",
"icon_emoji":":mailbox_with_mail:",
"link_names":1,
"blocks":[
{
"type":"section",
"text":[
{
"type":"mrkdwn",
"text":"*Name:*\n test"
}
]
}
]
}
]
Also see here for the official documentation on it.
Blocks are very powerful, but can be complicated at times. I would recommend to use the message builder to try out your messages and check out the examples in the docu.

Saving an object to DynamoDB instance via NodeJS

I feel like I'm missing something obvious, but the documentation is a bit confusing for DynamoDB, especially for NodeJS (I do see a lot for Java, however!).
I've done quite a bit of searching and only have found questions pertaining to the old SDK, so hopefully this isn't a duplicate question!
I'm trying to store a Javascript object into my DynamoDB instance. The error I'm getting and the code I'm using is outlined below.
Error:
Unable to add item. Error JSON: {
"message": "One or more parameter values were invalid:
Type mismatch for key File expected: S actual: M",
"code": "ValidationException",
"time": "2016-10-29T19:36:02.317Z",
"requestId": [removed],
"statusCode": 400,
"retryable": false,
"retryDelay": 0
}
Code:
var aws = require('aws-sdk');
var docClient = new aws.DynamoDB.DocumentClient({
"accessKeyId": AWS_ACCESS_KEY_ID,
"secretAccessKey": AWS_SECRET_ACCESS_KEY,
region: 'us-east-1',
endpoint: "https://dynamodb.us-east-1.amazonaws.com"
});
var tableName = AWS_TABLE_NAME;
var params = {
TableName: tableName,
Item: {
ProjID: projID,
File: {
name: fileName,
url: fileUrl
}
}
};
docClient.put(params, function (err, data) {
if (err) {
console.error("Unable to add item. Error JSON:", JSON.stringify(err, null, 2));
} else {
console.log("Added item:", JSON.stringify(data, null, 2));
}
callback(true);
});
Once again, I'm sure it's a fairly simple issue - I just haven't quite found any documentation to help me out that's updated.
Thanks in advance for any help you can give!
Is File an attribute in the schema of your table? If it is and it's defined as a String, then the error you are getting is just indicating that you are trying to put a map into a string attribute.
DynamoDB only supports String and Numeric keys so you'll have to revisit your design. Perhaps you can store the File information as a string for the key and then expanded as a map into a separate attribute if need be.

AngularJS (500 Internal Server Error) for http put

I want to update the record to the api.
In my firebug it shows:
500 internal server error,
XML Parsing Error: no element found Location: moz-nullprincipal:{f514f16e-f3d3-49f1-99e6-105b2b80f26c} Line Number 1, Column 1:
and also under put it shows:
There are no child objects
Here is my code
$scope.EditUser = function(){
$scope.ApplicationId = '7bg67898ewnbqjq65e';
$http.put("mydomain.com/api/Users/UpdateUser", {}, {
params: { _id: $scope.selectedItem._id, ApplicationId: $scope.ApplicationId, User_Name : $scope.selectedItem.UserName, IsActive: $scope.selectedItem.IsActive }
});
This is solved using this
$scope.EditUser = function(){
$scope.ApplicationId = '7bg67898ewnbqjq65e';
$http.put("mydomain.com/api/Users/UpdateUser", {
_id: $scope.selectedItem._id, ApplicationId: $scope.ApplicationId, User_Name : $scope.selectedItem.UserName, IsActive: $scope.selectedItem.IsActive
});
The shortcut method to perform PUT request is: $http.put(url, data, [config]);
#user3055606 your Answer is correct.

Not able to set text in input field from json response

I have a form in which I have to fill data after I have got a json object from httpGet in javascript.
$("#getDetails").click(function() {
$.get("/servlet",{
mID : 5
})
.done(function(data) {
$("#input1").val(data["some key"]);
$("#input2").val(data.name);
$("#input3").val("directvalue");
});
});
In the above 3 fields, only input3 gets filled with "directvalue".
Is there some problem in accessing json object or in setting value of input fields.
Note: the json object contains keys with spaces like "some key":"some value"
edit:
When I tried Object.keys(data)[index] to access the object field, I got Uncaught TypeError: Object.keys called on non-object
Try This
$("#getDetails").click(function () {
$.get("/servlet", {
mID: 5 })
.success(function (data) {
$("#input1").val(data.d.somekey);
$("#input2").val(data.d.name);
$("#input3").val("directvalue");
});
});
In the comments, you say it's returning this object:
{
"Name": "my name",
"my address": "23,round street",
"Description": "PM Speech at Red Fort on Indep Day 2014"
}
Try:
$("#input2").val(data.Name);
When I did console.out(data), it was printing a json object.
But when I tried Object.keys(data)[index] to access the object field, I got Uncaught TypeError: Object.keys called on non-object.
This means that dataitself was not a json object.
So I got a hint that server was giving a string in which json object was written. I had to simply parse the json object from the data string.
Following is the working code:
$("#getDetails").click(function() {
$.get("/servlet",{
mID : 5
})
.done(function(data) {
var dataObj=JSON.parse(data);
$("#input1").val(dataObj["some key"]);
$("#input2").val(dataObj.name);
});
});

Error when parsing array with OpenLayers and Protocol.HTTP()

I'm trying to send a JSON file to my Node.js app via Protocol.HTTP. The file is correctly sent, but I can't access to an array. Here is the relevant code :
Client side
(...)
var vectorProtocol = new OpenLayers.Protocol.HTTP({
url: '/getcoords',
format: vectorFormat,
readWithPOST: true,
params: {
"code_company": "8501",
"data_company": [
{
"origin": "2013P00109",
"type": "LJ",
"naf": "5610A",
},
{
"origin": "2013P00110",
"type": "FJ",
"naf": "5481"
}
]
}
});
(...)
Server side, I try to build an array with only the "origin" field of my array "data_company":
function getCoords(params, callback) {
var arrOrigin = params.data_company.map(function(d) {
return d.origin;
});
(...)
}
And I get this error :
TypeError: Object [object Object] has no method "map"
It seems that my "data_company" is not recognized as an array but as an object. I tried to JSON.parse(params) before but I get another error :
SyntaxError: Unexpected token o
Anyway, I'm stuck. Do you have any clue to help me to solve this ?
Thanks

Categories

Resources