i've got a workflow using the send email action. ...
# suitescript
s
i've got a workflow using the send email action. it attaches a pdf copy of the current record. I'd like to store a reference to the file on the triggering record (custom field) as something like 'most recent pdf' or whatever. the workflow action doesn't trigger any user events on the transaction in question as far as i can tell. I can certainly find the most recent message with an attachment with a given Subject/sender/whatever linked to the transaction in some async/follow-on script/activity. but that feels sorta sloppy. i could also do a standalone WFA script to generate the pdf and send the email in code, but that seems like more work than needed for this 'nice to have' feature. anyone have a different solution? maybe something really easy and awesome i just don't know about. 🤣
a
I've been struggling trying to get the average "Cost Per good" into the body of my email 😭 tried doing {projected value} / {quantity} and it errored out my Workflow and I had to basically turn it off at this point in my sandbox. I don't know what I'd doing wrong because I am still very new to NS. Good luck to you!
f
I would use a suitelet to render the transaction, attach it to and send the email and save it to the record. Knowing your skills, this should be a one foot fence to jump over. Workflows leave a lot to be desired. The above approach will let you define a chain of handlers executing defined strategies that can then be reused.
Copy code
/**
 * @NApiVersion 2.1
 * @NScriptType Suitelet
 */
define(['N/render', 'N/email', 'N/file', 'N/record', 'N/runtime'], 
    function(render, email, file, record, runtime) {
    
    function onRequest(context) {
        if (context.request.method !== 'GET') return;
        
        try {
            const recordId = context.request.parameters.recordId;
            const recordType = context.request.parameters.recordType;
            
            // Load and render PDF
            const template = render.create();
            template.setTemplateByScriptId('CUSTTMPL_YOUR_TEMPLATE_HERE');
            template.addRecord('record', record.load({
                type: recordType,
                id: recordId
            }));
            
            const pdfFile = template.renderAsPdf();
            
            // Save PDF as file
            const fileId = pdfFile.save({
                folder: YOUR_FOLDER_ID,
                fileName: `${recordType}_${recordId}.pdf`
            });
            
            // Send email
            email.send({
                author: runtime.getCurrentUser().id,
                recipients: ['<mailto:recipient@example.com|recipient@example.com>'],
                subject: 'Transaction PDF',
                body: 'Please find the transaction PDF attached.',
                attachments: [file.load({ id: fileId })]
            });
            
            // Update record
            record.submitFields({
                type: recordType,
                id: recordId,
                values: {
                    'custbody_pdf_sent': true,
                    'custbody_pdf_sent_date': new Date()
                }
            });
            
            context.response.write('PDF generated and sent successfully');
            
        } catch (e) {
            log.error('Error in PDF generation suitelet', e);
            context.response.write('Error: ' + e.message);
        }
    }

    return {
        onRequest: onRequest
    };
});
s
i abhor workflows. my vote is always to avoid or replace them. wishing for "workflows 2.x" that compile to suitescript and are treated as just another script type. in this case, the workflow won't be replaced. since there aren't any event triggers to run against, a suitelet would be extra work. my only 'good' option appears to be a WFA script to replace the current 'Send Email' action. 🫠
f
Ha ha. Friends don't let friends use workflows. Abhor would be too kind of a description for me. Workflows encompass so many bad characteristics all wrapped up into one convenient nightmare.