Element Builder - How to Preserve Data from the Vendor Response with a Set Root Key

When creating a resource for an element, you may come across a scenario where not all of your data from the vendor response makes it to the Cloud Elements response due to the use of a `response root key`. In this example, we're extending the Hubspot CRM element and building a resource to GET /companies. The vendor response when calling this resource returns this:

{ 
"companies": [ 
{ 
"additionalDomains": [], 
"companyId": 123456789, 
"isDeleted": false, 
"mergeAudits": [], 
"portalId": 1234567, 
"properties": {}, 
"stateChanges": [] 
}, 
{ 
"additionalDomains": [], 
"companyId": 123456789, 
"isDeleted": false, 
"mergeAudits": [], 
"portalId": 1234567, 
"properties": {}, 
"stateChanges": [] 
} 
], 
"has-more": false, 
"offset": 904803338 
}

Because the resource is looking for a single, parent key. You'll run into this error:

"Element was not setup properly. This element does not return a root array for its pluralized responses, so a 'rootKey' field is needed to be set on this resource" 

You may be wondering why you can't  just set the `response root key` to `companies`. However, doing that will resolve the error and get your list of companies in the response, but what if you need the other fields like `has-more` and `offset` for your paging needs. As you can see, those fields live outside of the `companies` array and will be excluded from the response.

There are two ways to resolve this issue:

  • Set the `response root key` to `companies` and in a post request hook, add the `offset` and `has-more` fields to the header. The post request hook will look something like this:
if(!response_headers || response_iserror) {
    done();
} else {
 let headers = {};
 headers['offset'] = response_body["offset"];
 headers['has-more'] = response_body["offset"];
 done({'response_headers': headers});
}

OR

  • In a post request hook, nest the raw vendor response inside an object like this:
if(!response_headers || response_iserror) {
     done();
} else {
 let body = {};
 body.parent = response_body;
 done({'response_body': body});
}

and set the `response root key` to `parent`.