skip to Main Content

I am trying to pass data from Formidable Forms to Brevo with an API. Here is my code so far:

{ "updateEnabled": true,
    "email": "[376]",
    "emailBlacklisted": false, 
    "smsBlacklisted": false,
    "attributes": {
        "FIRSTNAME": "[374]",  
        "LASTNAME": "[375]",   
        "GENRE": ["[385]"]
         
    },
    "listIds": [2]   
}

It works perfectly until I add in genres. The id 385 is the answers for a multiple-selection question in Formidable Forms and has the same answers as the GENRE attribute in Brevo. Formidable Forms defaults to a comma separated format but Brevo requires an array of strings. Currently, it will pass the data only if the first option in the form is selected. If anything else is selected, the other data transfers but the genre is left blank. Any ideas on how I can get it to pass all of the genre data in an array?

I tried the code listed above and I also tried the below (adding .split to help make it an array).

let selectedGenres = formData[385]; 
let genreArray = selectedGenres.split(',').map(item => item.trim());

let apiRequest = {
  "updateEnabled": true,
  "email": "[376]",
  "emailBlacklisted": false,
  "smsBlacklisted": false,
  "attributes": {
    "FIRSTNAME": "[374]",
    "LASTNAME": "[375]",
    "GENRE": genreArray 
  },
  "listIds": [2]
};

2

Answers


  1. If the form data doesn’t already have [] around each ID, you need to add it.

    let genreArray = selectedGenres.split(',').map(item => '[' + item.trim() + ']');
    
    Login or Signup to reply.
  2. i’d suggest using string.match(regex_pattern) like below:

    var my_id_string = 'example1,example2,example3'
    var my_regex = /(?:,|n|^)("(?:(?:"")*[^"]*)*"|[^",n]*|(?:n|$))/g
    var matches = my_id_string.match(my_regex);
    

    the regex is a bit nutty, but it should handle ‘normal’ csv and odd cases like double quotes, double commas, starting with a comma and more

    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search