Solved

What event to use to set a custom field's value (from another field) on page load?

  • 26 April 2023
  • 4 replies
  • 477 views

Userlevel 1
Badge

I’m programmatically setting the value of a custom field depending on another field’s value. I want it to happen first thing when the page is displayed, before anything is changed by the user. I which event should I put the code?

Thanks is advance for any suggestions.

icon

Best answer by darylbowman 27 April 2023, 18:24

View original

4 replies

Badge +11

I believe the proper way would be to:

  • Create an event handler for FieldDefaulting of your custom field
  • Create an event handler for FieldSelecting of the dependency field

In the FieldSelecting event handler, call e.Cache.SetDefaultExt<> on the custom field to signal that FieldDefaulting should be called for the custom field.

In the FieldDefaulting event handler, check the value of the dependency field and set your custom field value.

 

My source is @Brian Stevens (Acumatica.dev)

Userlevel 1
Badge

Thanks so much for the reply, Daryl. It gives multiple errors, mostly, I’m sure, because I have no idea what I’m doing:

'PXFieldSelectingEventArgs' does not contain a definition for 'Cache' and no accessible extension method 'Cache' accepting a first argument of type 'PXFieldSelectingEventArgs' could be found

I don’t know enough about Acumatica to make it work. I was hoping there was something equivalent to the onPageLoad event in C# web development.

But I really appreciate your effort to help me.

David

Badge +11

Something like this:

protected virtual void _(Events.FieldDefaulting<DAC, DAC.customField> e)
{
DAC row = e.Row;
if (row is null) return;

// Get dependent field value and set custom field value
var value = ""; // dependent field

e.Cache.SetValueExt<DAC.customField>(row, value);
}

protected virtual void _(Events.FieldSelecting<DAC, DAC.dependingField> e)
{
DAC row = e.Row;
if (row is null) return;

// Call field defaulting event for 'CustomField'
if (row.DependingField is object)
e.Cache.SetDefaultExt<DAC.customField>(row);
}

 

Badge +11

Additionally, you may want to add some sort of check to make sure a value saved in the custom field is not overwritten when the record is selected again. I’m not certain that wouldn’t happen.

Reply


About Acumatica ERP system
Acumatica Cloud ERP provides the best business management solution for transforming your company to thrive in the new digital economy. Built on a future-proof platform with open architecture for rapid integrations, scalability, and ease of use, Acumatica delivers unparalleled value to small and midmarket organizations. Connected Business. Delivered.
© 2008 — 2024  Acumatica, Inc. All rights reserved