- 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:19 AM
cc:

- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎07-07-2022 07:26 AM
That is expected right?
Since in your for loop, you are just storing the values of the variables, once the loop terminates, it will only give you the value of last iteration.
You can probably add one string variable in the for loop, and start concatenating that in the for loop and use that string to add in the work notes.
Feel free to mark correct, If I answered your query.
Will be helpful for future visitors looking for similar questions 🙂
Aman Kumar
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎07-07-2022 07:26 AM
Hi,
update as this
var survey = parsedData.Survey;
var arrSurveyResult = parsedData.Survey.SurveyResult;
var userType = parsedData.Survey.userType;
var userName = parsedData.Survey.user;
var arr = [];
if(Array.isArray(arrSurveyResult)){
for(var a=0; a< arrSurveyResult.length; a++){
var Question1 = arrSurveyResult[a].Question;
var Answer1 = arrSurveyResult[a].Answer;
var str = userType+':'+userName +'\n'+'Post CallSurvey:'+'\n'+'\n'+ 'Question) '+Question1 +
'\n'+'Answer) '+Answer1;
arr.push(str);
}
rec.work_notes = arr.toString();
rec.update();
}
Regards
Ankur
Ankur
✨ Certified Technical Architect || ✨ 9x ServiceNow MVP || ✨ ServiceNow Community Leader
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎07-10-2022 11:22 PM
Thank you for marking my response as helpful.
If my response helped please mark it correct and close the thread so that it benefits future readers.
Regards
Ankur
Ankur
✨ Certified Technical Architect || ✨ 9x ServiceNow MVP || ✨ ServiceNow Community Leader