Extract grid's records into an object - sencha-test

Say I have a grid and want to extract its records.
let record = {};
grid.rowAt(rowIndex)
.cellAt(cellIndex)
.focus()
.get('innerText')
.and(function (future) {
record.columnName = future.data.innerText;
});
How can I figure out how many columns and rows a grid has?
Based on the column's index how can I get the name of the respective header?

The following examples should help demonstrate how to get the row count, column count, get a column's title, and get the record object from a row in an Ext JS grid using Sencha Test:
describe('Grid', function() {
beforeAll(function() {
ST.grid('array-grid')
.rowAt(0); // Wait for data to be in the grid
});
it('should check the total number of rows and columns', function() {
ST.grid('array-grid')
.execute(function(grid) {
var values = {
recordCount: grid.getStore().getCount(), // Get the Store count (same as row count)
columnCount: grid.query('gridcolumn:visible').length // Get the number of visible columns
};
return values;
})
.and(function(future) {
var result = future.data.executeResult;
expect(result.recordCount).toEqual(100);
expect(result.columnCount).toEqual(6);
});
});
it('should get the column title for the fifth column in the grid', function() {
var columnTitle;
ST.component('array-grid gridcolumn:visible:nth-child(5)')
.get('text')
.and(function(future) {
// Do something with the column title
columnTitle = future.data.text;
expect(columnTitle).toEqual('Last Updated');
});
});
it('should get the record for the third grid row and check some of its values', function() {
ST.grid('array-grid')
.rowAt(2)
.getRecord()
.and(function(future) {
var record = future.data.record;
expect(record.name).toEqual('Dabvine');
expect(record.price).toEqual(29.94);
});
});
});

Related

How do you fetch a list of records and then add a checkbox next to corresponding record so to edit and save changes in suiteScript

I want to create a suitelet that could fetch some sales order then be able to edit those on click on submit button, so far I was I'm able to fetch records but I'm not able to add checkboxes and update records in netsuite - suitescript
var list=form.createList({title:"Sales order"});
list.addColumn({
id : 'column1',
type : serverWidget.FieldType.TEXT,
label : 'tranid',
align : serverWidget.LayoutJustification.LEFT
});list.addColumn({
id : 'column2',
type : serverWidget.FieldType.TEXT,
label : 'shipaddress',
align : serverWidget.LayoutJustification.LEFT
});list.addColumn({
id : 'column3',
type : serverWidget.FieldType.TEXT,
label : 'rate',
align : serverWidget.LayoutJustification.LEFT
});
salesorderSearchObj.run().each(function(result){
tranid= result.getValue({name: 'tranid'})
shipaddress= result.getValue({name: 'shipaddress'})
rate= result.getValue({name: 'rate'})
list.addRows({
rows : [{
column1 : tranid,
column2 : shipaddress,
column3 : rate
}]
});
Suite Answer Id 40768 has sample code in SuiteScript 1.0. An outline in 2.0 format is below. The full 1.0 sample from NetSuite is also below. The key is to for the GET req method create the page and display the information as a Sublist instead of a List. Then for all other req methods, get the parameters and take desired action.
2.0 partial outline
define(['N/ui/serverWidget'], function(serverWidget) {
function onRequest(context){
if(context.request.method === 'GET'){
//create page to display results and allow for user input
var form = serverWidget.createForm({
title : 'Simple Form'
});
var sublist = form.addSublist({
id : 'sublistid',
type : serverWidget.SublistType.INLINEEDITOR,
label : 'Inline Editor Sublist'
});
//Add checkbox and atleast internalid
var printField = sublist.addField({
id: 'custpage_rec_process',
label: 'Process',
type: serverWidget.FieldType.CHECKBOX
});
var idField = sublist.addField({
id: 'custpage_rec_id',
label: 'Internal ID',
type: serverWidget.FieldType.TEXT
});
//empty Array to hold Sales Order data
var TranIds = [];
//run search to get Sales Order data
...salesorderSearchObj.run().each(function(result){
//add search column names as columns in the sublist
//add each column value to the sublist
}
//add buttons to sublist and form
sublist.addMarkAllButtons();
sublist.addRefreshButton();
form.addResetButton({label: 'Reset'});
form.addSubmitButton({label: 'Create File'});
//display page for user input
context.response.writePage(form);
} else { //if the previously created/displayed form has been submitted display the following to the user
var req = context.request;
var params = JSON.stringify(context.request.parameters);//can log this to see exactly how the information comes through
//gather submitted data
//take desired action
//display response to user
context.response.writePage('Done');
}
} return {
onRequest: onRequest
}
});
1.0 full sample
function suitelet(request, response){
//Create the form that will be used by the POST and GET requests
var form = nlapiCreateForm('Delete Transactions');
//GET - Show a list of transactions from the search results so the user can select the ones to be deleted
if (request.getMethod() == 'GET' )
{
// Run an existing transaction search
var results = nlapiSearchRecord('transaction', 'customsearch_mass_deletion_results');
// Create a sublist to show the search results
var sublist = form.addSubList('custpage_transaction_list', 'list','Transactions');
// Create an array to store the transactions from the search results
var transactionArray = new Array();
if (results!=null)
{
// Add a checkbox column to the sublist to select the transactions that will be deleted.
sublist.addField('delete','checkbox', 'Delete');
// Add hidden columns for the Internal ID and for the Record type.
// These fields are necessary for the nlapiDeleteRecord function.
sublist.addField('internalid','text', 'Internal ID').setDisplayType('hidden');
sublist.addField('recordtype','text', 'Record Type').setDisplayType('hidden');
// Add a column for the Internal ID link
sublist.addField('internalidlink','text', 'Internal ID');
// Get the the search result columns
var columns = results[0].getAllColumns();
// Add the search columns to the sublist
for(var i=0; i< columns.length; i++)
{
sublist.addField(columns[i].getName() ,'text', columns[i].getName() );
}
// For each search results row, create a transaction object and attach it to the transactionArray
for(var i=0; i< results.length; i++)
{
var transaction = new Object();
// Set the Delete column to False
transaction['delete'] = 'F';
// Set the hidden internal ID field
transaction['internalid'] = results[i].getId();
// Set the hidden record type field
transaction['recordtype'] = results[i].getRecordType();
// Create a link so users can navigate from the list of transactions to a specific transaction
var url = nlapiResolveURL('RECORD', results[i].getRecordType() ,results[i].getId(), null);
internalIdLink = " " + results[i].getId() +" ";
// Set the link
transaction['internalidlink'] = internalIdLink;
// Copy the row values to the transaction object
for(var j=0; j< columns.length ; j++)
{
transaction[columns[j].getName()] = results[i].getValue(columns[j].getName());
}
// Attach the transaction object to the transaction array
transactionArray[i] = transaction;
}
}
// Initiate the sublist with the transactionArray
sublist.setLineItemValues(transactionArray);
sublist.addMarkAllButtons();
form.addSubmitButton('Submit' );
response.writePage( form );
}
//POST - Delete the selected transactions and show a confirmation message
else
{
// Check how many lines in the sublist
var count = request.getLineItemCount('custpage_transaction_list');
// This variable will keep track of how many records are deleted.
var num = 0;
//for each line in the sublist
for(var i=1; i< count+1; i++)
{
//get the value of the Delete checkbox
var deleteTransaction = request.getLineItemValue('custpage_transaction_list', 'delete', i);
// If it's checked, delete the transaction
if(deleteTransaction == 'T')
{
// Get the transaction internal ID
var internalId = request.getLineItemValue('custpage_transaction_list', 'internalid', i);
// Get the transaction type
var recordType = request.getLineItemValue('custpage_transaction_list', 'recordtype', i);
try
{
// Delete the transaction
nlapiDeleteRecord(recordType, internalId);
num++;
}
// Errors will be logged in the Execution Log
catch(ex)
{
nlapiLogExecution('ERROR', 'Error', 'Transaction ID '+ internalId +': '+ ex);
}
}
}
// Show how many records were deleted.
form.addField("custpage_transaction_total", "text").setDisplayType('inline').setDefaultValue(num + " transactions deleted");
response.writePage( form );
}
}

Copying data from one DB to another with node-sqlite - formatting the 'insert' statement

I'm writing a small utility to copy data from one sqlite database file to another. Both files have the same table structure - this is entirely about moving rows from one db to another.
My code right now:
let tables: Array<string> = [
"OneTable", "AnotherTable", "DataStoredHere", "Video"
]
tables.forEach((table) => {
console.log(`Copying ${table} table`);
sourceDB.each(`select * from ${table}`, (error, row) => {
console.log(row);
destDB.run(`insert into ${table} values (?)`, ...row) // this is the problem
})
})
row here is a js object, with all the keyed data from each table. I'm certain that there's a simple way to do this that doesn't involve escaping stringified data.
If your database driver has not blocked ATTACH, you can simply tell the database to copy everything:
ATTACH '/some/where/source.db' AS src;
INSERT INTO main.MyTable SELECT * FROM src.MyTable;
You could iterate over the row and setup the query with dynamically generated parameters and references.
let tables: Array<string> = [
"OneTable", "AnotherTable", "DataStoredHere", "Video"
]
tables.forEach((table) => {
console.log(`Copying ${table} table`);
sourceDB.each(`select * from ${table}`, (error, row) => {
console.log(row);
const keys = Object.keys(row); // ['column1', 'column2']
const columns = keys.toString(); // 'column1,column2'
let parameters = {};
let values = '';
// Generate values and named parameters
Object.keys(row).forEach((r) => {
var key = '$' + r;
// Generates '$column1,$column2'
values = values.concat(',', key);
// Generates { $column1: 'foo', $column2: 'bar' }
parameters[key] = row[r];
});
// SQL: insert into OneTable (column1,column2) values ($column1,$column2)
// Parameters: { $column1: 'foo', $column2: 'bar' }
destDB.run(`insert into ${table} (${columns}) values (${values})`, parameters);
})
})
Tried editing the answer by #Cl., but was rejected. So, adding on to the answer, here's the JS code to achieve the same:
let sqlite3 = require('sqlite3-promise').verbose();
let sourceDBPath = '/source/db/path/logic.db';
let tables = ["OneTable", "AnotherTable", "DataStoredHere", "Video"];
let destDB = new sqlite3.Database('/your/dest/logic.db');
await destDB.runAsync(`ATTACH '${sourceDBPath}' AS sourceDB`);
await Promise.all(tables.map(table => {
return new Promise(async (res, rej) => {
await destDB.runAsync(`
CREATE TABLE ${table} AS
SELECT * FROM sourceDB.${table}`
).catch(e=>{
console.error(e);
rej(e);
});
res('');
})
}));

Nested transactions with pg-promise

I am using NodeJS, PostgreSQL and the amazing pg-promise library. In my case, I want to execute three main queries:
Insert one tweet in the table 'tweets'.
In case there is hashtags in the tweet, insert them into another table 'hashtags'
Them link both tweet and hashtag in a third table 'hashtagmap' (many to many relational table)
Here is a sample of the request's body (JSON):
{
"id":"12344444",
"created_at":"1999-01-08 04:05:06 -8:00",
"userid":"#postman",
"tweet":"This is the first test from postman!",
"coordinates":"",
"favorite_count":"0",
"retweet_count":"2",
"hashtags":{
"0":{
"name":"test",
"relevancetraffic":"f",
"relevancedisaster":"f"
},
"1":{
"name":"postman",
"relevancetraffic":"f",
"relevancedisaster":"f"
},
"2":{
"name":"bestApp",
"relevancetraffic":"f",
"relevancedisaster":"f"
}
}
All the fields above should be included in the table "tweets" besides hashtags, that in turn should be included in the table "hashtags".
Here is the code I am using based on Nested transactions from pg-promise docs inside a NodeJS module. I guess I need nested transactions because I need to know both tweet_id and hashtag_id in order to link them in the hashtagmap table.
// Columns
var tweetCols = ['id','created_at','userid','tweet','coordinates','favorite_count','retweet_count'];
var hashtagCols = ['name','relevancetraffic','relevancedisaster'];
//pgp Column Sets
var cs_tweets = new pgp.helpers.ColumnSet(tweetCols, {table: 'tweets'});
var cs_hashtags = new pgp.helpers.ColumnSet(hashtagCols, {table:'hashtags'});
return{
// Transactions
add: body =>
rep.tx(t => {
return t.one(pgp.helpers.insert(body,cs_tweets)+" ON CONFLICT(id) DO UPDATE SET coordinates = "+body.coordinates+" RETURNING id")
.then(tweet => {
var queries = [];
for(var i = 0; i < body.hashtags.length; i++){
queries.push(
t.tx(t1 => {
return t1.one(pgp.helpers.insert(body.hashtags[i],cs_hashtags) + "ON CONFLICT(name) DO UPDATE SET fool ='f' RETURNING id")
.then(hash =>{
t1.tx(t2 =>{
return t2.none("INSERT INTO hashtagmap(tweetid,hashtagid) VALUES("+tweet.id+","+hash.id+") ON CONFLICT DO NOTHING");
});
});
}));
}
return t.batch(queries);
});
})
}
The problem is with this code I am being able to successfully insert the tweet but nothing happens then. I cannot insert the hashtags nor link the hashtag to the tweets.
Sorry but I am new to coding so I guess I didn't understood how to properly return from the transaction and how to perform this simple task. Hope you can help me.
Thank you in advance.
Jean
Improving on Jean Phelippe's own answer:
// Columns
var tweetCols = ['id', 'created_at', 'userid', 'tweet', 'coordinates', 'favorite_count', 'retweet_count'];
var hashtagCols = ['name', 'relevancetraffic', 'relevancedisaster'];
//pgp Column Sets
var cs_tweets = new pgp.helpers.ColumnSet(tweetCols, {table: 'tweets'});
var cs_hashtags = new pgp.helpers.ColumnSet(hashtagCols, {table: 'hashtags'});
return {
/* Tweets */
// Add a new tweet and update the corresponding hash tags
add: body =>
db.tx(t => {
return t.one(pgp.helpers.insert(body, cs_tweets) + ' ON CONFLICT(id) DO UPDATE SET coordinates = ' + body.coordinates + ' RETURNING id')
.then(tweet => {
var queries = Object.keys(body.hashtags).map((_, idx) => {
return t.one(pgp.helpers.insert(body.hashtags[i], cs_hashtags) + 'ON CONFLICT(name) DO UPDATE SET fool = $1 RETURNING id', 'f')
.then(hash => {
return t.none('INSERT INTO hashtagmap(tweetid, hashtagid) VALUES($1, $2) ON CONFLICT DO NOTHING', [+tweet.id, +hash.id]);
});
});
return t.batch(queries);
});
})
.then(data => {
// transaction was committed;
// data = [null, null,...] as per t.none('INSERT INTO hashtagmap...
})
.catch(error => {
// transaction rolled back
})
},
NOTES:
As per my notes earlier, you must chain all queries, or else you will end up with loose promises
Stay away from nested transactions, unless you understand exactly how they work in PostgreSQL (read this, and specifically the Limitations section).
Avoid manual query formatting, it is not safe, always rely on the library's query formatting.
Unless you are passing the result of transaction somewhere else, you should at least provide the .catch handler.
P.S. For the syntax like +tweet.id, it is the same as parseInt(tweet.id), just shorter, in case those are strings ;)
For those who will face similar problem, I will post the answer.
Firstly, my mistakes:
In the for loop : body.hashtag.length doesn't exist because I am dealing with an object (very basic mistake here). Changed to Object.keys(body.hashtags).length
Why using so many transactions? Following the answer by vitaly-t in: Interdependent Transactions with pg-promise I removed the extra transactions. It's not yet clear for me how you can open one transaction and use the result of one query into another in the same transaction.
Here is the final code:
// Columns
var tweetCols = ['id','created_at','userid','tweet','coordinates','favorite_count','retweet_count'];
var hashtagCols = ['name','relevancetraffic','relevancedisaster'];
//pgp Column Sets
var cs_tweets = new pgp.helpers.ColumnSet(tweetCols, {table: 'tweets'});
var cs_hashtags = new pgp.helpers.ColumnSet(hashtagCols, {table:'hashtags'});
return {
/* Tweets */
// Add a new tweet and update the corresponding hashtags
add: body =>
rep.tx(t => {
return t.one(pgp.helpers.insert(body,cs_tweets)+" ON CONFLICT(id) DO UPDATE SET coordinates = "+body.coordinates+" RETURNING id")
.then(tweet => {
var queries = [];
for(var i = 0; i < Object.keys(body.hashtags).length; i++){
queries.push(
t.one(pgp.helpers.insert(body.hashtags[i],cs_hashtags) + "ON CONFLICT(name) DO UPDATE SET fool ='f' RETURNING id")
.then(hash =>{
t.none("INSERT INTO hashtagmap(tweetid,hashtagid) VALUES("+tweet.id+","+hash.id+") ON CONFLICT DO NOTHING");
})
);
}
return t.batch(queries);
});
}),

Mongoose count skip limit

I am implementing pagination.
What I am doing is counting the items from collection first & after that count will return. I am then doing another find with skip and limit.
Now I want to assign count to the data returned by 2nd query which I am not able to do.
I tried toObject() but I am getting error "toObject() is not a function".
I don't want to use any library.
ErrorReportModel.find().count(function(err, count) {
if (err) {
console.log(err);
utile.internalError(res);
}
ErrorReportModel.find().sort({
date_create: -1
}).limit(20).skip(query.page * 20).lean().exec(function(err, data) {
if (err) {
console.log(err);
utile.internalError(res);
}
// i am doing this at the moment
var myData = {};
myData.data=data;
myData.count = count;
utile.response(res, myData);
});
});
I want to send the count to client side because I want to display page_number buttons depending on that count.

How can I implement a method, that returns collection & records count

I build rest api with sails.js.
I want implement pagination in my angularjs frontend.
To do this I should get a list of items along with the total count of items (to calculate pages count), that meet the criteria.
Unfortunately sails.js returns only data list without total records count.
I expect that the server response will look like this:
{
data: [...], // collection data
count: 193 // records count, that meet the criteria of the request.
}
How can I implement this?
you can use async.auto
async.auto(
count: functuon(callback){
Model.count(...).exec(callback);
},
data: function(callback){
Model.find(...).exec(callback);
}
},function(err,results){
console.log(results.count);
console.log(results.data);
});
I implimented a set of blueprints that will return a count in ether the header or the body
https://github.com/randallmeeker/SailsBluePrintActions/tree/master/pagination
here is an example
var query = Model.find()
.where( actionUtil.parseCriteria(req) )
.limit( actionUtil.parseLimit(req) )
.skip( actionUtil.parseSkip(req) )
.sort( actionUtil.parseSort(req) );
var metaInfo,
criteria = actionUtil.parseCriteria(req),
skip = actionUtil.parseSkip(req),
limit = actionUtil.parseLimit(req);
Model.count(criteria)
.exec(function(err, total){
if (err) return res.serverError(err);
metaInfo = {
start : skip,
end : skip + limit,
limit : limit,
total : total,
criteria: criteria
};
res.ok({info: metaInfo, items: matchingRecords});
});
You can use Model.count to count all data that meet the criteria.
Example:
// very important here to use the same `criteria` for `find` and `count`
var criteria = {foo: 'bar'};
Model.find(criteria).exec(function (err, found) {
Model.count(criteria).exec(function (error, count) {
console.log({ data: found, count: count });
});
});

Resources