For loop is saving only the last iterated value.

Sushma Uday C
Tera Contributor

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 
1 ACCEPTED SOLUTION

AnirudhKumar
Mega Sage
Mega Sage

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();
}

View solution in original post

6 REPLIES 6

Voona Rohila
Kilo Patron
Kilo Patron

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

AnirudhKumar
Mega Sage
Mega Sage

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();
}