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

Sushma Uday C
Tera Contributor

cc: @Ankur Bawiskar  Thanks in Advance

Aman Kumar S
Kilo Patron

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 🙂

Best Regards
Aman Kumar

Ankur Bawiskar
Tera Patron
Tera Patron

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

Regards,
Ankur
✨ Certified Technical Architect  ||  ✨ 9x ServiceNow MVP  ||  ✨ ServiceNow Community Leader

@Sushma Uday C 

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

Regards,
Ankur
✨ Certified Technical Architect  ||  ✨ 9x ServiceNow MVP  ||  ✨ ServiceNow Community Leader