// Click the "gear" icon in the top right to view settings const config = input.config({ title: 'Generate Document', description: 'Automate document generation using DocuGenerate', items: [ input.config.text('api_key', { label: 'API Key', description: 'Get your unique key from the Settings page in DocuGenerate' }), input.config.text('template_id', { label: 'Template ID', description: "ID of the template to use for generating the document" }), input.config.table('table', { label: 'Table', description: 'Source table for your records' }), input.config.field('storage_field', { label: 'Storage', description: 'Field for storing the generated documents (URL or Attachment)', parentTable: 'table', }), input.config.select('output_format', { label: 'Format', description: 'Output format of the generated documents', options: [ {label: 'PDF (.pdf)', value: '.pdf'}, {label: 'Microsoft Word (.docx)', value: '.docx'}, {label: 'Microsoft Word 2007 (.doc)', value: '.doc'}, {label: 'OpenDocument Format (.odt)', value: '.odt'}, {label: 'Plain Text (.txt)', value: '.txt'}, {label: 'HTML (.html)', value: '.html'}, {label: 'PNG (.png)', value: '.png'} ] }) ] }); // Prompt the user to pick a record // If this script is run from a button field, this will use the button's record instead let record = await input.recordAsync('Select a record to use', config.table); if (record) { // Customize this section to handle the selected record // You can use record.getCellValue("Field name") to access cell values from the record output.markdown(`Generating document for **${record.name}**`); // Get the program name const programsTable = base.getTable("Programs"); const programRecord = record.getCellValue("Program")[0]; const program = await programsTable.selectRecordAsync(programRecord.id); // Construct the data object based on the selected record's values const data = [{ "Company Name": record.getCellValue("Company_Name"), "First Name": record.getCellValue("First_Name"), "Last Name": record.getCellValue("Last_Name"), "Program": program?.getCellValue("Name"), "Start Date": record.getCellValue("Start_Date"), "End Date": record.getCellValue("End_Date"), "Certificate Date": record.getCellValue("End_Date") }]; const options = { method: 'POST', headers: { 'Accept': 'application/json', 'Content-Type': 'application/json', 'Authorization': config.api_key }, body: JSON.stringify({ template_id: config.template_id, name: `Certificate for ${record.getCellValue("First_Name")} ${record.getCellValue("Last_Name")}`, output_format: config.output_format, data: data }) }; let status; const response = await fetch('https://api.docugenerate.com/v1/document', options) .then(response => { status = response.status; return response.json(); }) .catch(err => output.text(err)); if (status !== 201) { output.markdown('Error generating document: ' + response.message); } else { const storageFieldValue = (config.storage_field.type === 'multipleAttachments') ? [{url: response.document_uri}] : response.document_uri; await config.table.updateRecordsAsync([{ id: record.id, fields: {[config.storage_field.name]: storageFieldValue} }]); output.markdown('Document generated successfully! 🎉'); } } else { output.markdown('No record was selected'); }