1
votes

I am trying to create a data connector for HubSpot and have been banging my head against the wall trying to figure this out. As far as I can tell, I'm doing everything correctly but the request object in the getData call is missing one of my fields I've specified in my schema.

When I check the logs, getSchema is returning both fields but for some reason, I only appear to be getting one. I've included the code and logs below:

function getData(request) {
  console.log("ORIGINAL FIELDS FOR GETDATA(): " + JSON.stringify(request.fields));

  switch (request.configParams.apiEndPoint) {
    case "sources":
      var url = API_URL + "/analytics/v2/reports/totals/summarize/daily?start=" + startYYYYMMDD + "&end=" + endYYYYMMDD;   
      break;
    case "stages":
      console.info("Fetching for Stages API");
      var url = API_URL + "/deals/v1/pipelines/default";
      getPipelineData(request, url);
      break;
  }
}

function getPipelineData(request, endPoint) {
  var header_row = [];

  console.log("FIELDS: " + JSON.stringify(request));

  request.fields.forEach(function(field) {
    for (var i = 0; i < mySchema.length; i++) {
      console.log("for loop i: " + i);
      console.log("mySchema[i]: " + JSON.stringify(mySchema[i]));

      if (mySchema[i].name === field.name) {
        console.log("Matched " + mySchema[i].name + " to " + field.name);

        header_row.push(mySchema[i]);
      } else {
        console.error("Did not match " + mySchema[i].name + " to " + field.name);
      }
    }
  });

You can see in the log on about row 5 that getSchema is returning 2 fields, "label" and "stageId." Yet, the next line down in the log is showing the object being used by getData only has one field, "label."

I have NOT, for the life of me, been able to figure out where I'm going wrong here and could really use some help!

Here's a link to the log output: https://www.screencast.com/t/rpD7oz5ZuDdv

2
Based on those log timestamps, 3 minutes went by between those 2 lines. Your code here doesn't show how you invoke these functions with the given schema, or the given request. Note that globals in Apps Script are not persistent - they are reinitialized every time a project instance spools up - so you can't assign to them from a function and then assume that will be the value when you next read from them. - tehhowch

2 Answers

1
votes

getSchema() is called during the data source initialization/config. It is called for all fields and is done usually once when you are creating the data source.

getData() is called whenever you create/view chart elements on a dashboard and is called for only the specific fields in that chart element. This list of fields will be passed in request.fields.

This workflow diagram might be of help.

From your log, line 3 and 4 happened when you were creating the data source. Line 5 happened when you added a chart element to your dashboard (and that chart element had only the label field).

0
votes

Try to add to your schema fields a group attribute. While iterating through your fields in your getData function and before calling the API you can simply filter by the group when you are checking the type of the field in order to make the correct call. I drafted the functions to the api call. but the general idea is this.

 function getData(request) {
     var fieldsSchema = this.getFields()
     var requestedFields = request.fields.map(function (field) {
         for (var i = 0; i < fieldsSchema.length; i++) {
             if (fieldsSchema[i].name == field.name) {
                 return fieldsSchema[i];
             }
         }
     });

     var datatype = requestedFields.reduce(function (type, field) {
         if (!type) {
             return field['group'];
         }
         if (type !== field['group']) {
             console.log('You can only choose fields in the same group.')
         }
         return type;
     }, undefined);

     config.datatype = datatype;
     switch (config.datatype) {
         case 'sources':
             return this.getSourcesFromApi(request, config, requestedFields);
         case 'stage':
             return this.getStagesFromApi(request, config, requestedFields);
         default:
             console.log('Fields in the group not supported')
     }
 }


 function getSchema(){
    return {schema: this.getFields()};
 }

 // get fields could be something like this
 function getFields() {
     return [{
             "name": "date",
             "label": "Date",
             "description": "The date that this was created",
             "dataType": "STRING",
             "group": "stages"
         },
         {
             "name": "name",
             "label": "name",
             "dataType": "STRING",
             "group": "stages"
         }
     ]

 }