Skip to main content
Solved

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


Forum|alt.badge.img

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.

Best answer by darylbowman

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

 

View original
Did this topic help you find an answer to your question?

4 replies

darylbowman
Captain II
Forum|alt.badge.img+13

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)


Forum|alt.badge.img
  • Author
  • Freshman II
  • 14 replies
  • April 27, 2023

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


darylbowman
Captain II
Forum|alt.badge.img+13
  • 1749 replies
  • Answer
  • April 27, 2023

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

 


darylbowman
Captain II
Forum|alt.badge.img+13

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


Cookie policy

We use cookies to enhance and personalize your experience. If you accept you agree to our full cookie policy. Learn more about our cookies.

 
Cookie settings