- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎07-07-2022 07:17 AM
Hi All,
I am trying to copy the array of details I am receiving from JSON, while copying them to work notes field I am able to copy only the last iterated value from for loop. Please look in the below details.
Thanks in Advance.
var survey = parsedData.Survey;
var arrSurveyResult = parsedData.Survey.SurveyResult;
var userType = parsedData.Survey.userType;
var userName = parsedData.Survey.user;
if(Array.isArray(arrSurveyResult)){
for(var a=0; a< arrSurveyResult.length; a++){
var Question1 = arrSurveyResult[a].Question;
var Answer1 = arrSurveyResult[a].Answer;
}
rec.work_notes = userType+':'+userName +'\n'+'Post CallSurvey:'+'\n'+'\n'+ 'Question) '+Question1 +
'\n'+'Answer) '+Answer1;
rec.update();
}
Payload:
{
"Survey":{
"userType":"Host User",
"user":"Sushma",
"SurveyResult":[
{"Question":"How is the Tool?",
"Answer":"Tool was easy to use"
},
{"Question":"Did you like the tool?",
"Answer":"Yes"
},
{"Question":"Rate us",
"Answer":"4"
} ]
}
Required out put:
Data to be copied to Work notes field :
Host User:Sushma
Post CallSurvey:
Question) How is the Tool?
Answer) Tool was easy to use
Question) Did you like the tool?
Answer) Yes
Question) Rate us
Answer) 4
CC @Ankur Bawiskar
Solved! Go to Solution.

- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎07-07-2022 07:29 AM
Hello Sushma,
The most direct way is this:
var survey = parsedData.Survey;
var arrSurveyResult = parsedData.Survey.SurveyResult;
var userType = parsedData.Survey.userType;
var userName = parsedData.Survey.user;
var worknotes = '';
if (Array.isArray(arrSurveyResult)) {
for (var a = 0; a < arrSurveyResult.length; a++) {
worknotes = worknotes + userType + ':' + userName + '\n' + 'Post CallSurvey:' + '\n' + '\n' + 'Question) ' + arrSurveyResult[a].Question.toString() + '\n' + 'Answer) ' + arrSurveyResult[a].Answer.toString();
}
rec.work_notes = worknotes;
rec.update();
}
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎07-07-2022 07:27 AM
Hi
Try this
var survey = parsedData.Survey;
var arrSurveyResult = parsedData.Survey.SurveyResult;
var userType = parsedData.Survey.userType;
var userName = parsedData.Survey.user;
var worknotes1 = '';
if (Array.isArray(arrSurveyResult)) {
for (var a = 0; a < arrSurveyResult.length; a++) {
var Question1 = arrSurveyResult[a].Question;
var Answer1 = arrSurveyResult[a].Answer;
worknotes1 += +'\n' + '\n' + 'Question) ' + Question1 + '\n' + 'Answer) ' + Answer1;
}
rec.work_notes = userType + ':' + userName + '\n' + 'Post CallSurvey:' + '\n' + '\n' + worknotes1;
rec.update();
}
Mark it helpful if this helps you to understand. Accept solution if this give you the answer you're looking for
Kind Regards,
Rohila V
2022-25 ServiceNow Community MVP

- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎07-07-2022 07:29 AM
Hello Sushma,
The most direct way is this:
var survey = parsedData.Survey;
var arrSurveyResult = parsedData.Survey.SurveyResult;
var userType = parsedData.Survey.userType;
var userName = parsedData.Survey.user;
var worknotes = '';
if (Array.isArray(arrSurveyResult)) {
for (var a = 0; a < arrSurveyResult.length; a++) {
worknotes = worknotes + userType + ':' + userName + '\n' + 'Post CallSurvey:' + '\n' + '\n' + 'Question) ' + arrSurveyResult[a].Question.toString() + '\n' + 'Answer) ' + arrSurveyResult[a].Answer.toString();
}
rec.work_notes = worknotes;
rec.update();
}