Help

Re: Creating Child records from Attachments

947 0
cancel
Showing results for 
Search instead for 
Did you mean: 
Hector_J_Carr
5 - Automation Enthusiast
5 - Automation Enthusiast

Hello so I have a button that triggers this script.
The script is to create a unique child record based on the attachments I add to it. For example, if I add image1 and image 2 into the parent record, then I click the button to run the script and the it will create 2 child records named image1 and image 2.

So I have the following script, and it works, but its actually getting all of the records from the view. But I only need it from a single record from where I clicked the button, so I understand I need to pass the record to the script. Can someone help me do this please


//script settings
let settings = input.config({
    title: 'Create individual records from multiple attachments in a single attachment field',
    description: 'Creates 1 individual record in another table for each attachment in a multiple attachment field in the source table, and links the new records back to the source record.',
    items: [
        // Source table select
        input.config.table('tableSource', {
            label: 'Table with existing attachments'
        }),
        // Source table: Attachment field
        input.config.field('attachField', {
            parentTable: 'tableSource',
            label: 'Attachment field with multiple attachments to split',
        }),
        // Source table: Selected View
        input.config.view('selectedView', {
            label: 'View from the selected table',
            parentTable: 'tableSource',
        }),
        // Destination table select
        input.config.table('tableDest', {
            label: 'Table to create new records in'
        }),
        // Destination table: Name or title field
        input.config.field('destinationField', {
            parentTable: 'tableDest',
            label: 'Deliverable name or title field in destination table',
        }),
    ]
});
async function splitAttachments() {
	let { tableSource, attachField, selectedView, tableDest, destinationField } = settings;
    if (attachField.type !== 'multipleAttachments') {
        output.text(`"${attachField.name}" is not an attachment field. Run the script again with an attachment field.`);
        return;
    }
    // Loads the records and fields from the selections above
    let attachQuery = await selectedView.selectRecordsAsync();
    console.log('query:', attachQuery)
    let  attachRecords = attachQuery.records;
    console.log('records', attachRecords)

    // Loops through qualified records and create new records in target table
    for (let i=0; i<attachRecords.length; i++){
        let attachments = attachRecords[i].getCellValue(attachField);
        console.log('attachments:', attachments)
        if(attachments !== null){
            // Array for records with attachments and their info
            let attachmentInfo = []
            for(let l=0; l<attachments.length; l++){
                attachmentInfo.push({
                    fields: {
                        [destinationField.name]: [
                            {url: attachments[l].url},
                        ]
                    }
                })
            }
             console.log('attachmentInfo', attachmentInfo)
             while (attachmentInfo.length > 0){
                    await tableDest.createRecordsAsync(attachmentInfo.slice(0,50));
                    attachmentInfo = attachmentInfo.slice(50)
                }
        }
    }
}
await splitAttachments();
2 Replies 2

Is this a script that you wrote or a script that you found? If you are not the script writer, do you have access to the original script write?

If you are the one writing the script, use await input.record Async() to get the record from the button field click and make the related changes to remove the loop.

Hector_J_Carr
5 - Automation Enthusiast
5 - Automation Enthusiast

Would that be included at the very beggining of the script? I did not write it actually I pieced it together from some scripts in this forum.