In SS2.x, can you set a field value in beforeLoad of a User Event script?
Hello!
So in a User Event script in the beforeLoad function I need to set a custom Transaction Body Field value, like this:
var newRecord = context.newRecord; newRecord.setValue({ fieldId: 'custbody_my_custom_field', value: true // or false });
Logging the result of the above, I can see the value change, but it does not persist through the entire page load, and always returns to the default state. I have ‘Store Value’ checked on the field’s setup. So, is it just not possible to set a field value in beforeLoad and have that value persist?
Thanks for any help!!
Per Help:
/app/help/helpcenter.nl?fid=section_N2962963.html
- Data cannot be manipulated for records that are loaded in beforeLoad scripts. If you attempt to update a record loaded in beforeLoad, the logic is ignored.
- Data can be manipulated for records created in beforeLoad user events.
Looks like you cannot set a field using Before Record Load on existing records. I did a quick test using a Before Load UE script and it let me change a custom field value on a “new” sales order but not when I’m trying to edit an existing sales order.
Yeah, thanks. battk on the Slack channel linked to those docs and unfortunately, this can’t be done on an existing record, which mine is. Some people are suggesting using a client script with the user event script. I’ll need to goto the docs… for a warning message I am having difficulty passing data from a User Event to Client already. Not being able to manipulate data on an existing record on BeforeLoad seems like a needless restriction, but . . .
Hi, are you sure there’s no other user event or workflow or client script re-setting the variable after you set it on the first place? I tried this:
/** * @NApiVersion 2.x * @NScriptType UserEventScript * UserEventScript */ define([], function a() { 'use strict'; return { beforeLoad: beforeLoad }; function beforeLoad(context) { context.newRecord.setValue({ fieldId: 'custbody_sample', value: true, ignoreFieldChange: true }); } } );
It works, the field is set to true when the sales order finished loading.
Thanks! Yes, I am sure. The field I am attempting to set is a new custom field, not referenced anywhere else. It’s good to know you got the above to work and the only thing I see right off is the `ignoreFieldChange` property. I am pretty new to SS and need to look that up in the documentation; I am not including that, so maybe it is as simple as that!
So I added the ignoreFieldChange property, and this is definitely not setting the value, or rather, the logging shows the same as it has: It sets the value, but then it doesn’t persist and it goes back to its original value. So weird. Wish I knew what was going on
Leacc, thanks! Yes, I did so in a newly created record, didn’t try on EDIT. Mreser, you might want to try with a workflow instead.
What event mode you want to change the specific custom field.
I tried to change the custom field value in “view” event mode and successfully changed. Using this code block
var
newRecord = context.newRecord;
newRecord.setValue({
fieldId:
'custbody_my_custom_field'
,
value: true
// or false
});
return newRecord
.save();