I was able to achieve it as below . Problem with inserting and then selecting is it does not guarantee the right id because of parallel transactions. So I had to do the below
var sql4 = "WITH upd AS (INSERT INTO SamTempOptout (sOldEmail,iBlId,iRId,iOptOut) values ('"+ vOldEmail + "'," + vblId + "," +vrId + "," + vOptOut + ") RETURNING iTempOptoutId) SELECT iTempOptoutId FROM upd ";
document.write("sql4 = " + sql4 );
ctx.vars.TempOptoutId = String(sqlGetInt(sql4))