In the beforeLoad event handler function of a User...
# ask-stanley-ai
a
In the beforeLoad event handler function of a User Event script deployed to a type of transaction, I'm trying to set the value of one of the transaction body fields, but when the record has finished loading, I don't see a value in the field. When I set the field using the same value using nlapiSetFieldValue in the browser console, it works fine. How do I get this to work on beforeLoad?
c
AI generated answer (using internal Slack threads):
In the beforeLoad event of a User Event script, you can't set the value of a field if the record is being edited or viewed. The beforeLoad event is mainly used for adding buttons or subtabs, or for hiding/displaying fields based on certain conditions. The setValue function will be ignored in the beforeLoad event when the record is being edited or viewed. If you want to set the value of a field when the record is being loaded, you should use a Client Script and set the field value in the pageInit function. The pageInit function is triggered when the page is initialized, so you can set the field value there and it will be displayed when the page is loaded. Here is a sample code snippet:
Copy code
javascript
/**
 * @NApiVersion 2.x
 * @NScriptType ClientScript
 */
define(['N/currentRecord'], function(currentRecord) {
    function pageInit(scriptContext) {
        var record = scriptContext.currentRecord;
        record.setValue({
            fieldId: 'your_field_id',
            value: 'your_value',
            ignoreFieldChange: true
        });
    }
    return {
        pageInit: pageInit
    };
});
Remember to replace 'your_field_id' and 'your_value' with your actual field ID and the value you want to set. ****** 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.887136638
Slack thread 2: here (available only if the original thread was posted in the last 90 days)
Similarity score: 0.886632
Slack thread 3: here (available only if the original thread was posted in the last 90 days)
Similarity score: 0.875801265
Please provide your feedback on this answer.