Help

Run script only on new records

Topic Labels: Scripting extentions
Solved
Jump to Solution
1924 2
cancel
Showing results for 
Search instead for 
Did you mean: 
Ariana_Garbers
4 - Data Explorer
4 - Data Explorer

Hello!
I am using the covert url to attachments extension and I’d like to run it every time I do an upload via csv, but only have it go through the newly uploaded records. Right now it does give the option to skip fields that already have attachments in them which is nice, but I have about 30,000 records and a decent amount of them don’t have urls to convert in the first place so it takes a while. Is there a way to only run a script based off a view or conditions? I’m not familiar with JavaScript, but I feel like there should be a way.

I’ve attached a screenshot of the extension script, any help is appreciated - thank you!
image
image

Screen Shot 2022-08-05 at 12.26.54 PM

1 Solution

Accepted Solutions
Vivid-Squid
11 - Venus
11 - Venus

Can you try to replace it with this?

let settings = input.config({
    title: "Convert URLs to attachments",
    description: `This script will download all attachments from a URL into an attachment field. The
source field can be a URL field or any text field containing a URL. If your URL field contains multiple URLs, each URL should be on its own line.`,
    items: [
        input.config.table("table", { label: "Table" }),
        input.config.field("urlField", {
            parentTable: "table",
            label: "URL field",
        }),
        input.config.field("attachmentField", {
            parentTable: "table",
            label: "Attachment field",
            description: "URLs will be downloaded into this field",
        }),
        input.config.view("chosenView", {
            label: "Pick View",
            description: "The view which filters",
            parentTable: "table",
        }),
    ], 
});

async function convertURLsToAttachments() {
    let { table, urlField, attachmentField, chosenView } = settings;

    if (attachmentField.type !== "multipleAttachments") {
        output.text(
            `${attachmentField.name} is not a attachment field.\nRun the script again when you have an attachment field.`
        );
        return;
    }

    let skipAlreadySet = await input.buttonsAsync(
        "Skip attachment entries that already have files?",
        [
            { label: "Yes", value: true },
            { label: "No", value: false },
        ]
    );

    let updates = [];
    for (let record of (
        await chosenView.selectRecordsAsync({ fields: [urlField, attachmentField] })
    ).records) {
        let existingAttachments = record.getCellValue(attachmentField) || [];
        if (skipAlreadySet && existingAttachments.length) continue;
        let urls = record.getCellValueAsString(urlField);
        if (typeof urls !== "string") continue;
        let attachmentsFromUrls = urls.split("\n").map((url) => ({ url: url.trim() }));
        updates.push({
            id: record.id,
            fields: {
                [attachmentField.id]: [
                    ...existingAttachments,
                    ...attachmentsFromUrls,
                ],
            },
        });
    }

    for (let i = 0; i < updates.length; i += 50) {
        await table.updateRecordsAsync(updates.slice(i, i + 50));
    }
}
await convertURLsToAttachments();

There will now be an option to select a view in the script settings. Not sure if its any faster though.

See Solution in Thread

2 Replies 2
Vivid-Squid
11 - Venus
11 - Venus

Can you try to replace it with this?

let settings = input.config({
    title: "Convert URLs to attachments",
    description: `This script will download all attachments from a URL into an attachment field. The
source field can be a URL field or any text field containing a URL. If your URL field contains multiple URLs, each URL should be on its own line.`,
    items: [
        input.config.table("table", { label: "Table" }),
        input.config.field("urlField", {
            parentTable: "table",
            label: "URL field",
        }),
        input.config.field("attachmentField", {
            parentTable: "table",
            label: "Attachment field",
            description: "URLs will be downloaded into this field",
        }),
        input.config.view("chosenView", {
            label: "Pick View",
            description: "The view which filters",
            parentTable: "table",
        }),
    ], 
});

async function convertURLsToAttachments() {
    let { table, urlField, attachmentField, chosenView } = settings;

    if (attachmentField.type !== "multipleAttachments") {
        output.text(
            `${attachmentField.name} is not a attachment field.\nRun the script again when you have an attachment field.`
        );
        return;
    }

    let skipAlreadySet = await input.buttonsAsync(
        "Skip attachment entries that already have files?",
        [
            { label: "Yes", value: true },
            { label: "No", value: false },
        ]
    );

    let updates = [];
    for (let record of (
        await chosenView.selectRecordsAsync({ fields: [urlField, attachmentField] })
    ).records) {
        let existingAttachments = record.getCellValue(attachmentField) || [];
        if (skipAlreadySet && existingAttachments.length) continue;
        let urls = record.getCellValueAsString(urlField);
        if (typeof urls !== "string") continue;
        let attachmentsFromUrls = urls.split("\n").map((url) => ({ url: url.trim() }));
        updates.push({
            id: record.id,
            fields: {
                [attachmentField.id]: [
                    ...existingAttachments,
                    ...attachmentsFromUrls,
                ],
            },
        });
    }

    for (let i = 0; i < updates.length; i += 50) {
        await table.updateRecordsAsync(updates.slice(i, i + 50));
    }
}
await convertURLsToAttachments();

There will now be an option to select a view in the script settings. Not sure if its any faster though.

Based on @Ariana_Garbers’ use case description, they might want to plug something along these lines into a Scripting automation. Assuming they’re not uploading thousands of CSVs on a monthly basis.