how to fetch all values in single variable using java script

399 views Asked by At

i am retrieving the all values in a for loop but i want to insert those values in database using single variable.It possible to store all values to the single record.

var emailId;

//log.info("testing 1234 = "+tw.local.EntityProMasterList.listAllSelected);

for (var i = 0; i < tw.local.EntityProMasterList.listAllSelected.listLength; i++){
    emailId = tw.local.EntityProMasterList.listAllSelected[i];
    log.info("testing 1 = "+emailId.value);
}

log.info("testing 1 = "+emailId.value);

2

There are 2 answers

0
Anton Homjak On

You can user JSON.stringify() and save it as string:

var holder = {};
holder.var1 = "var1";
holder.var2 = "var2";
log.info("holder:"+JSON.stringify(holder));

The output will be:

holder:{"var1":"var1","var2":"var2"}

0
Drux On

I believe your question is - given a list of values, how can I insert those values into the database as separate entries. If this is correct there is a right way and a wrong way to do this.

The wrong way is to simply put all the SQL into a string and use one of the DB services in the system data toolkit to execute. Something like -

insert into table blah values(1, 2, 3, 4);

This would be wrong because you are open to SQL injection attacks. There is a different service for parameterized queries. This takes 1 or more SQL statements and a list of parameters to use in them. The details are documented in the service so I won't repeat them here. Basically you would modify your query to have the ? Character where you need the data from your array. You then create arrays of parameters that fill out the values on execution.