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!!

 

Rookie Asked on September 16, 2019 in SuiteScript.
Add Comment
4 Answer(s)
Best answer

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.

 

Beginner Answered on September 16, 2019.

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 . . .

on September 16, 2019.
Add Comment

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.

Beginner Answered on September 16, 2019.

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!

on September 16, 2019.

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

on September 16, 2019.
Add Comment

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.

Beginner Answered on September 16, 2019.
Add Comment

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();

 
Beginner Answered on March 27, 2022.
Add Comment

Your Answer

By posting your answer, you agree to the privacy policy and terms of service.
  • This site made possible by our sponsors:   Tipalti   Celigo   Become a Sponsor   Become a Sponsor