How to change attachment file name in Power Apps?

1.1k views Asked by At

I'm trying to change the file name of an attachment in an attachment control. So far, I've followed a solution from online thread.

I used this script in the OnAddFile of the Attachment DataCardValue control. Then used colAttachments in the Items property of the same control and colAttachments in the Update property of the Attachment data card control. However, something weird seems to be happening.

  1. On adding the attachment file with colAttachments in the Items property and Update property, the file does not attach. 

  2. If I remove colAttachments from either Items property or Update property, I'm able to attach the file but it shows the original file name. While the file is attached, if I change either the Items property or Update property, so that both properties have colAttachments, then the file name changes to the new name.

    ClearCollect(
        colAttachments,
        {
            AbsoluteUri: " ",
            DisplayName: " ",
            Id: " ",
            Name: " ",
            Value: " "
        }
    );
    Collect(
        colAttachments,
        DataCardValue16.Attachments
    );
    RemoveIf(
        colAttachments,
        Id = " "
    );
    UpdateIf(
        colAttachments,
        Len(First(colAttachments).Name) >= 1,
        {
            Name: Replace(
                First(colAttachments).Name,
                1,
                (Len(First(colAttachments).Name) - 4),
                "invoice" & Text(
                    Now(),
                    "[$-en-US]yyyy-mm-dd-hh-mm-ss"
                )
            ),
            DisplayName: Replace(
                First(colAttachments).Name,
                1,
                (Len(First(colAttachments).Name) - 4),
                "invoice" & Text(
                    Now(),
                    "[$-en-US]yyyy-mm-dd-hh-mm-ss"
                )
            )
        }
    )

Is there any way I can resolve this problem?

2

There are 2 answers

0
Allen_MSFT On

The script provided in the thread seems to be causing issues with attaching the file and changing the file name in the attachment control. One possible solution is to use the Patch function instead of the Collect and UpdateIf functions. Here's an example script that uses the Patch function to update the file name:

ClearCollect(colAttachments, DataCardValue16.Attachments);
RemoveIf(colAttachments, Id = "");
Patch(colAttachments, First(colAttachments), {
    Name: "invoice" & Text(Now(), "[$-en-US]yyyy-mm-dd-hh-mm-ss") & ".pdf",
    DisplayName: "invoice" & Text(Now(), "[$-en-US]yyyy-mm-dd-hh-mm-ss") & ".pdf"
});

This script first collects the attachments in the attachment control and removes any empty attachments. Then, it uses the Patch function to update the file name and display name of the first attachment in the collection. The file name is set to "invoice" followed by the current date and time in the format "yyyy-mm-dd-hh-mm-ss" and the file extension ".pdf". The display name is set to the same value as the file name.

0
user3526093 On

I had same issue while trying to update attachment name with dropdown value.

I tried updating items property with If(Form1=FormMode.New, colAttachment,Parent.Default) and it worked

I feel this is an issue with the latest version of powerapps