Can you help me get started writing a beforeLoad u...
# ask-stanley-ai
s
Can you help me get started writing a beforeLoad user event script, that when emailing from the communication tab on a purchase order, the script defaults the email template based on the custom form for the transaction?
c
AI generated answer (using internal Slack threads):
Sure, you can achieve this by creating a User Event Script that triggers on the beforeLoad event. Here's a high-level overview of how you can do this: 1. Create a new User Event Script and set the trigger to beforeLoad. 2. In your script, you'll need to check if the context is 'email'. You can do this by checking if
context.type === context.UserEventType.EMAIL
. 3. If the context is 'email', you can then get the custom form of the transaction using
record.getValue('customform')
. 4. Based on the custom form, you can then set the email template. You can do this by setting the value of the 'emailtemplate' field on the record. Here's a basic example of how your script might look:
Copy code
javascript
/**
 * @NApiVersion 2.x
 * @NScriptType UserEventScript
 */
define(['N/record', 'N/runtime'], function(record, runtime) {
    function beforeLoad(context) {
        if (context.type === context.UserEventType.EMAIL) {
            var customForm = context.newRecord.getValue('customform');
            var emailTemplate;

            // Set the email template based on the custom form
            if (customForm === '1') {
                emailTemplate = '10'; // Replace with your template ID
            } else if (customForm === '2') {
                emailTemplate = '20'; // Replace with your template ID
            }

            if (emailTemplate) {
                context.newRecord.setValue('emailtemplate', emailTemplate);
            }
        }
    }

    return {
        beforeLoad: beforeLoad
    };
});
Please replace the '1', '2', '10', and '20' with your actual custom form and email template IDs. ****** Disclaimer: This response was generated by an artificial intelligence assistant based on previous conversations from multiple Slack channels, and may contain inaccuracies or mistakes. ******
Slack thread 1: here (available only if the original thread was posted in the last 90 days)
Similarity score: 0.850965083
Slack thread 2: here (available only if the original thread was posted in the last 90 days)
Similarity score: 0.847132683
Slack thread 3: here (available only if the original thread was posted in the last 90 days)
Similarity score: 0.846214175
Please provide your feedback on this answer.