Transforming POST Virtual Data Resource Responses Using Javascript

VDRs allow you to transform data you're receiving from vendors (GET), as well as the format of data you send to the vendor (POST/PATCH/PUT). However, if you need to transform the response of a POST request, you can do that by adding Custom JS to your VDR. 

You will need 3 if-blocks to handle the 3 different use-cases your VDR will encounter:

  • GET data from the Vendor. 
  • POST data to the Vendor.
  • POST response from the vendor. 

The first two use-cases can be handled using the fromVendor boolean, which is set to true if Data is from the vendor and false if you're sending data to the vendor. However, the last use-case will require that you identify a unique field in the POST response, such as a createdAt field. By using this field you will be able to differentiate from the first two use-cases, to then handle the third. 

The following JS illustrates how you can have your POST responses to be transformed by the VDR, for the Hubspot element.

if (fromVendor){
 // This step is needed to ensure that nothing else is executed if you're performing a simple GET request. 
 done(transformedObject)
}

if (!fromVendor) {
  // This block is to make sure a POST request is sending data in the right format for the vendor (Hubspot). 
  var emailProperty = {
   "property":"email",
   "value": originalObject.email
  };
  var properties = [emailProperty]
  transformedObject.properties = properties
}

if ((originalObject.hasOwnProperty("properties")) && (fromVendor)) {
  //Finally, this block will determine if data is coming from the vendor and the response has the properties field (a Hubspot field unique to POST responses).
  //This block will run the POST response through the VDR. 
  transformedObject.email = originalObject.properties.email.value
 }
done(transformedObject)