Mapping Arrays to Specific VDR Fields

Do you have an array in a vendor response and you want to map values from that array into a VDR string field? This guide can help you accomplish this requirement through two examples.

First scenario: The vendor response from an endpoint contains an array of values, and you want to map a single value to a string field. In this example, we will use the Hubspot CRM /engagements resource.

  1. Review your original object. Let's focus on the 'associations' object that has an array titled 'ownerIds' and the 'engagement' object that contains the engagement ID.
     {
       "associations": {
         "ownerIds": [
           3799137
         ]
       },
       "engagement": {
         "id": 6462048
       }
     }
  2. Define your base VDR with at least one field (in this case 'ID') and map it to your element instance (engagement.id). 
  3. Add custom JS to the Transformation that will first validate the existence of the 'associations' object and the 'ownerIds' array, and then extract the value from the array and assign it to a new string field titled 'externalReferenceId'.
    if (fromVendor) {
     if ("associations" in originalObject) {
      var hubAssociations = originalObject.associations;
      if ("ownerIds" in hubAssociations) {
       var arraySize = new Array(hubAssociations.ownerIds.length);
       var transformedField = 'externalReferenceId';
       for (var i = 0; i < arraySize.length; i++) {
        transformedObject[transformedField] = hubAssociations.ownerIds[i];
       }
      }
      else {
       done(transformedObject);
      }
     }
     else {
     done(transformedObject);
     }
    }
    done(transformedObject)
  4. Try out this VDR and confirm that the transformed response body is structured with your engagement ID and externalReferenceId as expected:
    [
      {
        "externalReferenceId": 3799137,
        "Id": "6462048"
      }
    ]

Second Scenario: The vendor response from an endpoint contains an array objects, and you want to map the values of a specific field/value pair to a new string field. In this example we will use the Autotask Helpdesk /Ticket resource.

  1. Review your original object. Let's focus on the 'userDefinedFields' object that has an array titled 'userDefinedField' and the object within this array named "Hosting Account Name".
     {
      "id": 7687,
      "ticketType": 1,
      "priority": 4,
      "userDefinedFields": {
        "userDefinedField": [
         {
           "name": "Hosting Account Name",
           "value": "TestCloud"
         },
         {
           "name": "Hosting Customer Number",
           "value": "111111"
         }
        ]
      }
     }
  2. First define your base VDR with at least one field (in this case 'id') and map it to your element instance (Ticket id). 
  3. Add custom JS to the Transformation that will first validate the existence of the 'userDefinedFields' object and the 'userDefinedField' array, and then extract the 'value' from the position of the array where name equals 'Hosting Account Name', and assign it to a new string field titled 'Account Name'.
    if (fromVendor) {
     if ("userDefinedFields" in originalObject) {
      var userDefinedFieldsObj = originalObject.userDefinedFields;
      if ("userDefinedField" in userDefinedFieldsObj) {
       var userDefinedFieldArray = userDefinedFieldsObj.userDefinedField;
       var arraySize = new Array(userDefinedFieldArray.length);
       var transformedField = 'Account Name';
       for (var i = 0; i < arraySize.length; i++) {
        if (userDefinedFieldArray[i].name === 'Hosting Account Name') {
         transformedObject[transformedField] = userDefinedFieldArray[i].value;
        }
       }
      }
      else {
       done(transformedObject);
      }
     }
     else {
      done(transformedObject);
     }
    }
    else {
     done(transformedObject);
    }
    done(transformedObject);
  4. Try out this VDR and confirm that the transformed response body is structured with your Ticket ID and Account Name as expected.
    [
      {
        "Account Name": "TestCloud",
        "id": "7687"
      }
    ]