how do I set a value in a field on a form, using b...
# ask-stanley-ai
m
how do I set a value in a field on a form, using beforeLoad in UE script?
c
AI generated answer (using internal Slack threads):
In a User Event (UE) script, you can set a value in a field on a form using the
beforeLoad
event, but there are some restrictions. The
setValue
method is only allowed in
beforeLoad
when the record is being created. If you're trying to set a value while editing a record,
setValue
will be ignored in
beforeLoad
. Here's a sample code snippet:
Copy code
javascript
function beforeLoad(context) {
    if (context.type === context.UserEventType.CREATE) {
        context.newRecord.setValue({
            fieldId: 'custbody_my_custom_field',
            value: 'My Value'
        });
    }
}
In this example, 'custbody_my_custom_field' is the ID of the custom field you want to set, and 'My Value' is the value you want to set. If you need to set a value while editing a record, you might want to consider using a Client Script instead. In a Client Script, you can use the
pageInit
function to set a value when the page is loaded, regardless of whether the record is being created or edited. Here's a sample code snippet:
Copy code
javascript
function pageInit(context) {
    context.currentRecord.setValue({
        fieldId: 'custbody_my_custom_field',
        value: 'My Value'
    });
}
****** 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.853275895
Slack thread 2: here (available only if the original thread was posted in the last 90 days)
Similarity score: 0.850252509
Slack thread 3: here (available only if the original thread was posted in the last 90 days)
Similarity score: 0.849396586
Please provide your feedback on this answer.