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)
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
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);
}
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.