JSON.stringify not working in portal client script

William Busby
Tera Guru

I have a client script running within a portal widget which calls JSON.stringify to convert a javascript array to a JSON string. When the call is finished an empty string is returned. It generates an empty JSON string of '[[]]'. Shouldn't JSON.stringify work within a service portal widget client script? The code for my function is:

c.onSubmit = function() {
// ask the user for a string
// note embedded html in message
var h = '<h4>Record Submit Confirmation</h4>'
// Line feeds added to the following lines for presentation formatting.
var m = 'Submitting will write your changes to the database and set the record workflow state to Submitted. A notification will be sent to your mailbox when your submission is approved.'

spModal.open({
title: 'Are you sure you want to Submit?',
message: h + m,
buttons: [
{label:'✘ ${No}', cancel: true},
{label:'✔ ${Yes}', primary: true}
]
}).then(function() {
c.agree = 'yes';
}, function() {
c.agree = 'no';
})
//
// iterate through each line of data from the WBS table to extract the display_value
//
var objDisplay = []; // array to hold the display values
//var jsonDisplay = [];
var arrLines = $scope.data.list; // only reason to do this is to simplify references to the data

for (i = 0; i < arrLines.length; i++) {

var objTemp = [];
var obj = [];
for(var attribute in arrLines[i]) {
objTemp[attribute] = arrLines[i][attribute].display_value // store the display value of each attribute
//objTemp.push(attribute + ':' + '"' + arrLines[i][attribute].display_value + '"') // store the display value of each attribute
}
objDisplay.push(objTemp);
//console.log(objDisplay); // display_value for each line of data

}
console.log(JSON.stringify(objDisplay));
}
console.log(jsonDisplay);

1 ACCEPTED SOLUTION

Callum Ridley1
Tera Guru

Hi William,

There are a multiple reason why this might not be working, but the first one that comes to mind is that you are declaring the objTemp as an Array and then trying to add properties to it. To fix this it would simply be a case of changing:

var objTemp = []; 

to 

var objTemp = {};

I have further questions regarding what it is you're actually trying to do here as the code seems a bit all over the place.

One thing worth bearing in mind is that all the code you have written after spModal.open({...}).then(...) will be executed before the user has provided a response to the modal dialog window. Any code you want to execute after the response need to be inside of the 'then' promise function.

Hopefully that helps you out a little.

Callum

View solution in original post

1 REPLY 1

Callum Ridley1
Tera Guru

Hi William,

There are a multiple reason why this might not be working, but the first one that comes to mind is that you are declaring the objTemp as an Array and then trying to add properties to it. To fix this it would simply be a case of changing:

var objTemp = []; 

to 

var objTemp = {};

I have further questions regarding what it is you're actually trying to do here as the code seems a bit all over the place.

One thing worth bearing in mind is that all the code you have written after spModal.open({...}).then(...) will be executed before the user has provided a response to the modal dialog window. Any code you want to execute after the response need to be inside of the 'then' promise function.

Hopefully that helps you out a little.

Callum