Skip to main content
cancel
Showing results for 
Search instead for 
Did you mean: 
juwa
Contributor
Contributor

SelectValues in Realtime using enigma.js

At first I want to say that I am a absolute JavaScript Beginner. 

My goal is to find a way setting a filter in my Qlik sense APP with an external Action. 

I thought about using einigma.js for pushing the "Select values" method to automatically getting applied in Realtime (without refreshing Browser!).

If there is any other way or solution I would be more than thankful for every hint. 

Here is what I tried till now using node.js:

const serverUri = 'localhost';
const clientCertFile = '.\\client.pem';
const clientCertKey = '.\\client_key.pem';
const enigma = require('./node_modules/enigma.js/enigma.min.js');
const WebSocket = require('ws');
const fs = require('fs');
const schema = require('./node_modules/enigma.js/schemas/12.170.2.json');
// match this schema to your Qlik Sense version.
const defaultCertPath = 'C:\\ProgramData\\Qlik\\Sense\\Repository\\Exported Certificates\\.Local Certificates';

const session = enigma.create({
schema,
url: `wss://${serverUri}:4747/app/engineData`,
createSocket: url => new WebSocket(url, {
rejectUnauthorized: false,
//ca: [fs.readFileSync('.\\root.pem')], // you can skip ca if you set rejectUnautzoried to false
key: fs.existsSync(clientCertKey)?fs.readFileSync(clientCertKey)
:fs.readFileSync(`${defaultCertPath}\\client_key.pem`),
cert: fs.existsSync(clientCertFile)?fs.readFileSync(clientCertFile)
:fs.readFileSync(`${defaultCertPath}\\client.pem`),
headers: { "X-Qlik-User": 'UserDirectory=INTERNAL;UserId=sa_engine' }
})
});

var addToApp = '4c85c23b-19f2-4bd1-952d-5627618ba044';

 

session.open().then(function (global) {
console.log('>> Session was opened successfully');
var app;
var origScript;
//console.log(global);
return global.engineVersion().then(ret => {
console.log('Engine Version: ', ret);
//return global.getDocList();
return global.openDoc(addToApp);
}).then(ret => {
app = ret;
return app.getScript();
}).then(script => {
origScript = script;
script = [];

//Here ist the main part:
script.push(`//header of generated script`);
script.push("var qlikapp = qlik.currApp()")
script.push("qlikapp.field('Kalender.Year').selectValues([2019],false,true);")

script.push(`//footer of generated script`);
script = script.join('\n'); // implode script array into new-line separated string

console.log('---begin of new script---\n');
console.log(script);
console.log('\n---end of new script---');
return app.setScript(script);
}).then(ret => {
console.log('>> App script changed, now reloading (pushing new data):');
return app.doReload(0, true); // true = Partial Reload
}).then(ret => {
console.log('>> Reload finished: ', ret);
return app.setScript(origScript);
}).then(ret => {
console.log('>> Load script reverted to original script. Now saving.');
return app.doSave();
}).catch(error => {
console.error('Error', error);
});
}).then(ret => {
session.close();
console.log('>> Done.');
}).catch(error => {
console.error('Error', error);
});

 

Node generates the following output, but nothing happens: 

>> Session was opened successfully
Engine Version: { qComponentVersion: '12.421.3' }
---begin of new script---

//header of generated script
var qlikapp= qlik.currApp()
qlikapp.field('Kalender.Year').selectValues([2019],false,true);
//footer of generated script

---end of new script---
>> App script changed, now reloading (pushing new data):
>> Reload finished: false
>> Load script reverted to original script. Now saving.
>> Done.

 

Would be more than happy to get any hint. Thanks!

Labels (4)
6 Replies
GuillaumeBt
Partner - Contributor
Partner - Contributor

Hi,

As I don't have the app, it's difficult to reproduce the issue.

But I think you could keep using object received from Enigma instead of using scripts. I think you will have a better feedback on the eventual issues. Also don't forget to use .catch() when you use a Promise 🙂

It will be something like this :

app.getField('Year')
.then(field => {
return field.selecValues([2019]);
})
.then((successful) => {
if(successful) {
console.log('Selection done');
} else {
console.error('Selection failed);
}
})
.catch(console.error);

 Here the documentation about the used function :

https://help.qlik.com/en-US/sense-developer/September2018/apis/EngineAPI/services-Doc-GetField.html

https://help.qlik.com/en-US/sense-developer/September2018/apis/EngineAPI/services-Field-SelectValues...

iharsh220
Contributor III
Contributor III

hello,

i dont understand what you exactly want

engine api only works on backend side.. it will not reflect on front end..

if you are selecting some field using engine api it will not select anything on front end..

 

Thanks Regards

Harsh Gohil

juwa
Contributor
Contributor
Author

Is there any other possibility to connect live to the Front End? 

iharsh220
Contributor III
Contributor III

Yes it is possible.. you have to write code for ticket also you have to add qlik session cookies in headers

And your port must be 4243 i can share you link tommrow..

iharsh220
Contributor III
Contributor III

var qsocks = require('qsocks');
var fs = require('fs');
var request = require('request');
const enigma = require('enigma.js');
const schema = require('enigma.js/schemas/12.20.0.json');
const SenseUtilities = require('enigma.js/sense-utilities');
const WebSocket = require('ws');

//Set our request defaults, ignore unauthorized cert warnings as default QS certs are self-signed.
var r = request.defaults({
rejectUnauthorized: false,
host: 'Qlik Url', // enter qlik Url
pfx: fs.readFileSync(__dirname+'\\Certificates\\client.pfx')
})

//Authenticate whatever user you want
var b = JSON.stringify({
"UserDirectory": 'UserDirectory', // enter your UserDirectory
"UserId": 'UserId', // enter your UserId
"Attributes": []
});

//Get ticket for user
r.post({
uri: 'https://Qlikurl:4243/qps/ticket?xrfkey=abcdefghijklmnop', // enter qlik Url :4243
body: b,
headers: {
'x-qlik-xrfkey': 'abcdefghijklmnop',
'content-type': 'application/json'
}
},
function(err, res, body) {

//Consume ticket, set cookie response in our upgrade header against the proxy.
var ticket = JSON.parse(body)['Ticket'];
r.get('https://Qlikurl/hub/?qlikTicket=' + ticket, function(error, response, body) {

var cookies = response.headers['set-cookie'];
//qsocks config, merges into standard https/http object headers.
//Set the session cookie correctly.
var config = {
host: 'Your Host', // your host Qlik Url
isSecure: true,
port: 443,
origin: 'http://localhost',
rejectUnauthorized: false,
headers: {
"Content-Type": "application/json",
"Cookie": cookies[0]
}
}
qsocks.Connect(config).then(function(global) {
global.openDoc("Put Your AppId").then(function(app){
app.getField("Country").then(function(field){
var selected=[
{
"qText": "Germany",
"qIsNumeric": false,
"qNumber": 0
}
]
field.selectValues(selected).then(function(selection){
console.log(selection);
});
//field.selectAll();
//field.clear();
})
})
})
})
})

 

check this out if it will work for you

Thanks Regards

Harsh Gohil

iharsh220
Contributor III
Contributor III

var qsocks = require('qsocks');
var fs = require('fs');
var request = require('request');
const enigma = require('enigma.js');
const schema = require('enigma.js/schemas/12.20.0.json');
const SenseUtilities = require('enigma.js/sense-utilities');
const WebSocket = require('ws');

//Set our request defaults, ignore unauthorized cert warnings as default QS certs are self-signed.
var r = request.defaults({
rejectUnauthorized: false,
host: 'Qlik Url', // enter qlik Url
pfx: fs.readFileSync(__dirname+'\\Certificates\\client.pfx')
})

//Authenticate whatever user you want
var b = JSON.stringify({
"UserDirectory": 'UserDirectory', // enter your UserDirectory
"UserId": 'UserId', // enter your UserId
"Attributes": []
});

//Get ticket for user
r.post({
uri: 'https://Qlikurl:4243/qps/ticket?xrfkey=abcdefghijklmnop', // enter qlik Url :4243
body: b,
headers: {
'x-qlik-xrfkey': 'abcdefghijklmnop',
'content-type': 'application/json'
}
},
function(err, res, body) {

//Consume ticket, set cookie response in our upgrade header against the proxy.
var ticket = JSON.parse(body)['Ticket'];
r.get('https://Qlikurl/hub/?qlikTicket=' + ticket, function(error, response, body) {

var cookies = response.headers['set-cookie'];
//qsocks config, merges into standard https/http object headers.
//Set the session cookie correctly.
var config = {
host: 'Your Host', // your host Qlik Url
isSecure: true,
port: 443,
origin: 'http://localhost',
rejectUnauthorized: false,
headers: {
"Content-Type": "application/json",
"Cookie": cookies[0]
}
}
qsocks.Connect(config).then(function(global) {
global.openDoc("Put Your AppId").then(function(app){
app.getField("Country").then(function(field){
var selected=[
{
"qText": "Germany",
"qIsNumeric": false,
"qNumber": 0
}
]
field.selectValues(selected).then(function(selection){
console.log(selection);
});
//field.selectAll();
//field.clear();
})
})
})
})
})

 

check this out bro. if it will work for you

Thanks Regards 
Harsh Gohil