Can't get values from object

Dotychczas
Mega Guru

Hello Experts,

I've created VirtualAgent main topic where I call topic block that performes survey. I'm trying to get answers from survey to use them in main topic. I managed to get object with answers and display it in the message but I have dificulties when it comes to using only value that is interesting for me - answer.

I've created this piece of code in message after survey:

(function execute() {
   var obj =  vaInputs.survey_answers.getValue();
   obj =  JSON.parse(obj);
   var ans1 =  Object.values(obj);  //obj['526c28d787585110d1f7cb790cbb3516'].value;
   return ans1;
})()

commented line works but I don't want to hardcode sys id's of questions in here. 

Object looks like this:

{"526c28d787585110d1f7cb790cbb3516":{"value":false,"Qtype":"boolean","displayValue":false},"f449355f871c5110d1f7cb790cbb3509":{"value":null,"Qtype":"scale","displayValue":null}}
1 ACCEPTED SOLUTION

Hitoshi Ozawa
Giga Sage
Giga Sage

Hi,

Not sure of the question. Following will print out the values.

var obj = {"526c28d787585110d1f7cb790cbb3516":{"value":false,"Qtype":"boolean","displayValue":false},"f449355f871c5110d1f7cb790cbb3509":{"value":null,"Qtype":"scale","displayValue":null}}

for (var sysId in obj) {
    var ans = obj[sysId].value;
    gs.info(ans);
}

Execution result:

*** Script: false
*** Script: null

View solution in original post

2 REPLIES 2

Tudor
Tera Guru

Hello,

Please first try this:

var myObject = { a: 'c', b: 'a', c: 'b' };
var keyNames = Object.keys(myObject);
console.log(keyNames); // Outputs ["a","b","c"]

and see whether you can get the sys_id.

Afterwards you will need to dot walk
myObject[keyNames].value
myObject[keyNames].Qtype
myObject[keyNames].displayValue

Hope this helps!

 Tudor

Hitoshi Ozawa
Giga Sage
Giga Sage

Hi,

Not sure of the question. Following will print out the values.

var obj = {"526c28d787585110d1f7cb790cbb3516":{"value":false,"Qtype":"boolean","displayValue":false},"f449355f871c5110d1f7cb790cbb3509":{"value":null,"Qtype":"scale","displayValue":null}}

for (var sysId in obj) {
    var ans = obj[sysId].value;
    gs.info(ans);
}

Execution result:

*** Script: false
*** Script: null