0

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();
paul1
  • 1
  • The above gives you a lot of background (which I highly recommend you read), but the simplest adaptation of your code to make it work is to make `this.opportunityManager` an `async` function and `await api.getOpporrunity()` – Robin Zigmond Aug 25 '20 at 22:54
  • Hello Robin, thanks for your time. I tried to begin with what you said, `this.opportunityManager = async function (){ var transactions = await api.getOpportunity();` But it tells me that getOpportunity is undefined.. – paul1 Aug 26 '20 at 02:14

0 Answers0