get attachment file name with script

Viewing 6 reply threads
  • Author
    Posts
  • November 6, 2018 at 7:34 AM #31654

    Guillaume Kuster
    Participant

    Hey there,

    I’m trying to get an attachment’s filename as a string and then parse it using regex to populate new records. When trying to get the value of the field:

    var attachments = 'fld-e4657ea48c5a4ff6b82ee2a3c43c96a8';
    var fileObject = record.getFieldValue(attachments);
    console.log(fileObject);

    this outputs

    
    [object Object]
    

    How can get to the properties of that object to get the attachment’s file name? If the form I’ll be using, there will only be one file attachment per record.

    The purpose of all this is that I’m trying to convert a file base invoice archive where file names were following a convention of type YYYY-MM-DD - vendor - amount.

    Thanks!

    -Guillaume

    November 6, 2018 at 2:49 PM #31667

    Brendan
    Keymaster

    Hi Guillaume,

    I haven’t opened up the File Attachment field to the Script editor yet.

    Did you get that field ID by changing the field type to something else and then changing it to File Attachment? Because for me File Attachment fields don’t show up in the list of available fields.

    In any case, it should be an array of objects where each object is a dictionary.

    I’ll have to do some tests with this though to see how I can open it up for scripting.

    November 6, 2018 at 5:56 PM #31671

    Daniel Leu
    Participant

    I was wondering how to get to the attachment ID…

    Following scriptlet shows the filename of the first attachment.

    
    var receipt_attachment_id = 'fld-8bbc0b1472a746cabc38f6d7ab045668';
    var receipt = record.getFieldValue(receipt_attachment_id);
    JSON.stringify(receipt);
    var meta = receipt[0];
    if (meta) {
    	meta.filename
    }
    
    November 6, 2018 at 9:19 PM #31678

    Brendan
    Keymaster

    I’ve just added support for getting the IDs directly for a File Attachment field.

    When you get the value, the result is an array of dictionaries that contain the following keys:

    {
        creationdate = "2018-11-06T18:25:28.000Z";
        filename = "TEST-Summary.tfarc";
        filesize = 14208;
        filesizedesc = "13.9 KB";
        isalias = 0;
        kind = "Tap Forms Archive";
        mimetype = "application/tapforms-tfarc";
        modifieddate = "2018-11-06T18:25:28.000Z";
    }
    November 6, 2018 at 9:48 PM #31680

    Daniel Leu
    Participant

    Nice! So JSON.stringify(); is not needed anymore?

    November 6, 2018 at 11:57 PM #31681

    Brendan
    Keymaster

    Well, it is still needed it seems. I’m not sure why yet. Because an NSArray in Objective-C is supposed to translate into a JavaScript array. But an NSDictionary just translates to a JavaScript Object, which I guess still needs to be stringified.

    November 7, 2018 at 3:55 AM #31682

    Guillaume Kuster
    Participant

    Thanks Brendan,

    here’s how I got the field id for the attachments, provided that I created a test form containing just an attachments field:

    var fieldIds = form.getFieldIds();
    console.log(fieldIds);
    November 7, 2018 at 3:56 AM #31683

    Guillaume Kuster
    Participant

    I’ve just added support for getting the IDs directly for a File Attachment field.

    This looks exactly like what I need. Thanks a lot!

    November 7, 2018 at 11:48 AM #31692

    Brendan
    Keymaster

    Ah yes. The form.getFieldIds() method. I forgot about that.

    You’ll see the File Attachment fields in your form appear on the left panel on the Script editor after the next update.

Viewing 6 reply threads

You must be logged in to reply to this topic.