Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
Exemplo de datasert com insert Fluig
var init = {
datasetName: 'dsInsertConferenciaMateriais',
fluigService: 'TotvsRM',
primaryKey: [
'success'
],
columns: [
'success',
'error'
]
}
function defineStructure() {
for (var currentColumn = 0; currentColumn < init.columns.length; currentColumn++) {
addColumn(init.columns[currentColumn], DatasetFieldType.STRING)
}
setKey(init.primaryKey)
}
function createStructure() {
var dataset = DatasetBuilder.newDataset()
for (var index = 0; index < init.columns.length; index++) {
dataset.addColumn(init.columns[index])
}
return dataset
}
function onSync(lastSyncDate) {
var dataset = createStructure()
var query = createDataset()
if (!query.values) {
return query
}
var primaryKeyCodes = []
for (var currentRow = 0; currentRow < query.values.length; currentRow++) {
var primaryKey = ''
for (var index = 0; index < init.primaryKey.length; index++) {
primaryKey += query.getValue(currentRow, init.primaryKey[index])
}
primaryKeyCodes[primaryKey] = true
var row = new Array()
for (var currentColumn = 0; currentColumn < init.columns.length; currentColumn++) {
var value = query.getValue(currentRow, init.columns[currentColumn])
row.push((value && value.trim() !== '') ? value : '')
}
dataset.addOrUpdateRow(row)
}
query = DatasetFactory.getDataset(init.datasetName, null, null, null)
if (query && query.values) {
for (var currentRow = 0; currentRow < query.values.length; currentRow++) {
var primaryKey = ''
for(var index = 0; index < init.primaryKey.length; index++) {
primaryKey += query.getValue(currentRow, init.primaryKey[index])
}
if (primaryKeyCodes[primaryKey] === undefined) {
var row = new Array()
for (var currentColumn = 0; currentColumn < init.columns.length; currentColumn++) {
row.push(query.getValue(currentRow, init.columns[currentColumn]))
}
dataset.deleteRow(row)
}
}
}
return dataset
}
function onMobileSync(user) {
var result = {
'fields': init.columns,
'constraints': new Array(),
'sortFields': new Array()
}
return result
}
function createDataset(fields, constraints, sortFields) {
var dataset = createStructure()
try {
var NumeroMov = ""
var obsRecebimento = ""
var UsuarioCode = ""
var CodColigada = ""
if (constraints) {
for (var index in constraints) {
if (constraints[index].getFieldName().toLowerCase() == 'NumeroMov'.toLowerCase()) {
NumeroMov = constraints[index].getInitialValue()
}
else if (constraints[index].getFieldName().toLowerCase() == 'CodColigada'.toLowerCase()) {
CodColigada = constraints[index].getInitialValue()
}
else if (constraints[index].getFieldName().toLowerCase() == 'obsRecebimento'.toLowerCase()) {
obsRecebimento = constraints[index].getInitialValue()
}
else if (constraints[index].getFieldName().toLowerCase() == 'UsuarioCode'.toLowerCase()) {
UsuarioCode = constraints[index].getInitialValue()
}
}
}
var query = "INSERT INTO ZMDRECEBIMENTOMATERIAIS (NUMMOVIMENTO, OBSERVACAO, CODUSUARIO, CODCOLIGADA, RECCREATEDBY, RECCREATEDON) VALUES ('" + NumeroMov + "','" + obsRecebimento + "', dbo.fsbi_userfluig('"+ UsuarioCode +"'),'" + CodColigada + "',dbo.fsbi_userfluig('"+ UsuarioCode +"'), GETDATE())";
log.info(query)
console.log(query)
var initialContext = new javax.naming.InitialContext()
var dataSource = initialContext.lookup('/jdbc/TotvsRM')
var connection = dataSource.getConnection()
var statement = connection.prepareStatement(query)
var response = statement.executeUpdate()
var row = new Array()
row.push('true')
row.push('')
dataset.addRow(row)
return true;
}
catch (exception) {
var row = new Array()
row.push('false')
row.push('Error to execute dataset "' + init.datasetName + '": ' + exception.message)
dataset.addRow(row)
log.info('Error to execute dataset "' + init.datasetName + '": ' + exception.message)
}
finally {
if (statement != null) {
statement.close()
}
if (connection != null) {
connection.close()
}
}
return dataset
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment