0

I'm not sure how to loop through the custom fields when adding a dynamic field via the web script editor.

When I test I can see the fields are being returned in the console

enter image description here

Where the number of fields is different with each instance of our app.

This is the code I'm using to return the data.

return z.request(options)
  .then((response) => {
    response.throwForStatus();
    const results = z.JSON.parse(response.content)._embedded;
    return results;
  });

I assume I need to loop through each of the fields, pull out the ID and name and then put them back as an array of objects?

Something like this, only problem is nothing is being returned?

  return z.request(options)
  .then((response) => {
    response.throwForStatus();
    const results = z.JSON.parse(response.content).results._embedded;
    var cFields = [];
    for (var i = 0; i < results.length; i++) {
      cFields.push({'id': results.customFields[i].label});
    }
    return cFields;
  });

Any pointers?

Oliver
  • 11
  • 3

1 Answers1

0

I worked this out in the end. I think the problem was more because of my lack of coding knowledge. Not sure if this is the best answer but it worked.

  return z.request(options)
  .then((response) => {
    response.throwForStatus();
    const results = z.JSON.parse(response.content)._embedded;
    let customFields = [];
    for (let i = 0; i < results.customFields.length; i++) {
      let customFieldsObj = {};
      customFieldsObj['key'] = results.customFields[i].id;
      customFieldsObj['label'] = results.customFields[i].label;
      let helpText = results.customFields[i].type + ' Field';
      customFieldsObj['helpText'] = helpText.toUpperCase();
      customFields.push(customFieldsObj);
    }
    return customFields;
  });
Oliver
  • 11
  • 3