Class: DataTable

DataTable

Represents a DataTable object. Contains methods to save and load the row values from a file.


new DataTable()

Default constructor

Source:

Methods


<static> load(string)

Loads the DataTable object from the specified file.

Parameters:
Name Type Description
string fileName

File path where to save the DataTable object. The path is relative to process.cwd()

Source:
Example

Loads the table from an xlsx file

var dt = DataTable.load('mydata.xlsx');

lookup(columnName, updater)

Method to convert all the existing values in a column.
Iterates through all the existing rows, and for every value in the specified column calls to the specified callback method.
Then, the returning value will be applied to the column.
The idea behind this functionality is that you can resolve the lookup data that you may have in a DataTable, before sending
those values to CRM.
For example, you may want to load a list of contacts, and you want to associate your contacts to existing parent accounts.
What you can do, is use the phone number on the contact to try to find the parent account of the contact.

Parameters:
Name Type Description
columnName string

Name of the column which values are going to be updated

updater DataTable~lookupCallback

Function that will process every record in the Table.

Source:
Examples

Lookup using simple values

var dt = new DataTable();
 dt.rows.push({val1:1,val2:2},
              {val1:2,val2:2});
 dt.lookup('val1',row=>++row.val1);
 console.log(dt.rows[0].val1); // prints out 2
 console.log(dt.rows[1].val1); // prints out 3

Find the parent account of a contact using the phone number

// create a contact using a data table and associate to the create account using the phone number
 var dtContacts = DataTable.load("MyContactsToLoad.json");

 // resolve the parentcustomerid field
 dtContacts.lookup("parentcustomerid",row=>{ return {id:crm.retrieve("account",{telephone1:row.telephone1}).accountid,type:"account"}});

 // create the record
 crm.create(dtContacts);

removeColumn(columnName)

Removes a column from the Table

Parameters:
Name Type Description
columnName string

Name of the column to remove

Source:
Example

Remove an existing column

var dt = new DataTable();
 dt.rows.push({val1:1,val2:2},
              {val1:2,val2:2});
 dt.removeColumn('val1');
 console.log(dt.rows[0].val1); // prints undefined
 console.log(dt.rows[1].val1); // prints undefined
 console.log(dt.rows[0].val2); // prints 2
 console.log(dt.rows[1].val2); // prints 2

rename(columnName, newName)

Renames an existing column in the Table

Parameters:
Name Type Description
columnName string

Name of the existing column to rename

newName string

New Name to apply to the column

Source:
Example

Rename an existing column

var dt = new DataTable();
 dt.rows.push({val1:1,val2:2},
              {val1:2,val2:2});
 dt.renameColumn('val1','val3');
 console.log(dt.rows[0].val1); // prints undefined
 console.log(dt.rows[1].val1); // prints undefined
 console.log(dt.rows[0].val2); // prints 2
 console.log(dt.rows[1].val2); // prints 2
 console.log(dt.rows[0].val3); // prints 1
 console.log(dt.rows[1].val3); // prints 2

save(DataTable, string)

Saves the specified datatable object to the specified file.
The format of the file depends on the extension provided.
The supported formats are json, xml and xlsx.

Parameters:
Name Type Description
DataTable dataTable

Table to save to the specified file.

string fileName

File path where to save the DataTable object. The path is relative to process.cwd()

Source:
Example

Saves the datatable to a .json file

var dt = new DataTable();
dt.save('mydata.json');

Type Definitions


lookupCallback(row)

Callback that receives a row of a data table and returns a value for a column

Parameters:
Name Type Description
row object

Object containing the values of a row

Source:
Returns:

The value to apply to a specific column of that particular row

Type
object