- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
05-15-2023 10:44 AM
Hello, I hope someone can help me.
I'm creating a business rule to get values from a REST Message. Let's say that I want "DescriptionSummary" value, so I have the following:
I'm getting undefined for the value I want to pull.
This is my business rule script:
(function executeRule(current, previous /*null when async*/ ) {
// Add your code here
try {
var r = new sn_ws.RESTMessageV2('Windchill CN', 'Default GET');
r.setStringParameterNoEscape('customvalue', 'ChangeNotices?$filter=Number%20eq%20%272369991%27');
var response = r.execute();
var responseBody = response.getBody();
var httpStatus = response.getStatusCode();
gs.addInfoMessage("http status: " + httpStatus);
var parsed = JSON.stringify(responseBody);
var field3 = parsed.Name;
gs.addInfoMessage(" field3--" + field3);
this.json = new global.JSON().decode(responseBody);
gs.addInfoMessage("Test: " + this.json.DescriptionSummary);
// if (response.getStatusCode() != 200) {
// gs.info('Bad Request');
// }
var headers = response.getAllHeaders();
for(var i in headers){
gs.addInfoMessage(headers[i].name + ': ' + headers[i].value);
}
gs.addInfoMessage(" Response--" + response);
gs.addInfoMessage(" Response Body--" + responseBody);
//var obj = JSON.parse(responseBody);
//var obj = gs.xmlToJSON(responseBody);
//var xmlDoc = new XMLDocument2();
//xmlDoc.parseXML(responseBody);
//var field3 = xmlDoc.getNodeText("//DescriptionSummary");
//gs.addInfoMessage(" Response--" + responseBody);
//gs.addInfoMessage(" field3--" + field3);
current.update();
}
catch(ex) {
var message = ex.message;
}
})(current, previous);
I tried multiple ways to get the values but no luck.
Thanks!
Solved! Go to Solution.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
05-15-2023 12:20 PM - edited 05-15-2023 12:56 PM
Could you paste a redacted version of the response body and share the exact value you are trying to access from the response?
It looks like the name value you are trying to access is actually located at parsed['value'][0]['Name'] based on what I can see from the screenshots you shared.
Thank you
edit:
As @Bharath Kumar A below mentioned, you need to use JSON.parse instead of JSON.stringify. I missed this when reviewing your code. Good catch @Bharath Kumar A!
However, you still will need to include the [0] after parsed.value as the type of parsed.value is an array. So the full code sniped should be
var parsed = JSON.parse(responseBody);
var field3 = parsed.value[0].Name;
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
05-15-2023 11:51 AM
JSON.stringify() returns a human-readable string, not an obect. So I would expect this to return undefined:
var parsed = JSON.stringify(responseBody);
var field3 = parsed.Name;
gs.addInfoMessage(" field3--" + field3);
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
05-15-2023 12:20 PM - edited 05-15-2023 12:56 PM
Could you paste a redacted version of the response body and share the exact value you are trying to access from the response?
It looks like the name value you are trying to access is actually located at parsed['value'][0]['Name'] based on what I can see from the screenshots you shared.
Thank you
edit:
As @Bharath Kumar A below mentioned, you need to use JSON.parse instead of JSON.stringify. I missed this when reviewing your code. Good catch @Bharath Kumar A!
However, you still will need to include the [0] after parsed.value as the type of parsed.value is an array. So the full code sniped should be
var parsed = JSON.parse(responseBody);
var field3 = parsed.value[0].Name;
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
05-15-2023 01:01 PM
Yes. I didn't observe [[ in your response. Please mark it as helpful.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
05-15-2023 01:18 PM
Thank you so much! It is showing now the value.