I try to use an API from an ERP software. A developer had already made an integration of this API for our company, but in a google script for a google sheet, he retrieved all our inventory transactions.
I am a beginner and I am trying to grab the code to adapt it to our needs. This time it's for a site that will add additional information depending on a chosen contract.
The developer had used the UrlFetchApp class, and I found that it didn't work for my use. So I tried using fetch with response, but despite my many attempts, the result is still the same, undefined.
Can someone guide me on the right path?
Thank you for your help !
The function which is called in the html to launch the whole thing
function run() {
new opportunityManager();
}
The one who gives me the information
this.opportunityManager = function (){
var transactions = api.getOpportunity();
for (var i in transactions) {
var transaction = transactions[i];
var id = transaction.id;
}
if (id === undefined) {
alert("ca ne marche pas");
}
else {
alert(transaction.id);
}
}
And finally the group that executes the request
var CurrentAPI = function() {
this.url = 'https://api.current-rms.com/api/v1/';
this.key = '*****************';
this.subdomain = '***';
this.options = {
'method': 'get',
'validateHttpsCertificates': false,
'headers': {
'X-SUBDOMAIN': this.subdomain,
'X-AUTH-TOKEN': this.key
}
};
/* =======================================
* @function request / I think the problem comes from this one
* =======================================*/
this.request = function(params) {
fetch(this.url + params, this.options)
.then(function (response) {
var data = response.text();
return JSON.parse(data);
});
}
/* =======================================
* @function getOpportunity
* ======================================= */
this.getOpportunity = function() {
var transactions = [];
var url = 'opportunities?q[number_cont]=34399'/*+ opportunityNumber.innerHTML*/;
var data = this.request(url);
transactions = transactions.concat(data.opportunities);
return transactions;
}
}
// Instanciation
var api = new CurrentAPI();