Is there a way to count the number of images in a Photo field (JavaScript API)?

Tap Forms – Organizer Database App for Mac, iPhone, and iPad Forums Using Tap Forms Is there a way to count the number of images in a Photo field (JavaScript API)?

Viewing 3 reply threads
  • Author
    Posts
  • April 5, 2019 at 9:42 AM #34297

    A Ibanez
    Participant

    I’m dealing with a rather sensitive API that times out very often for me. This API returns images and I want to add those images to my forms. To do this I’m iterating over all the records and adding them. But due to the timeouts, sometimes my images don’t get added.

    To go around this issue i’d like to count the number of images in a Photo field and if it’s > 0, skip it.

    So far I haven’t even been able to access the Photo field. tt.getFieldValue(photo_id_charactersform); returns undefined.

    I have a workaround for this but i’d like to avoid it if possible, as it requires me to manually keep track of how many images a record field has.

    April 5, 2019 at 11:11 AM #34300

    Brendan
    Keymaster

    You can count the number of photos in a Photo field like this:

    var photos = record.getFieldValue(photo_field_id);
    photos.length;
    

    That’ll return the number of photos in the Photo field.

    As long as the photo_field_id evaluates to a proper field ID and record is valid then it should work. For Photo fields, the getFieldValue() function will return an array. So now you just need to get the length of the array.

    April 5, 2019 at 1:08 PM #34302

    A Ibanez
    Participant

    Brendan,

    I got it now. It returns an array when there’s photos, and undefined when there’s nothing. I was expecting it to return an empty array in that case but I can adapt to this. Thanks.

    April 5, 2019 at 7:31 PM #34303

    Brendan
    Keymaster

    Ah I see… Ya you would need to add a check for undefined too then. I suppose I could modify my code to return an empty array if there’s no photos.

Viewing 3 reply threads

You must be logged in to reply to this topic.