0

I am using D365 Finance and operations. I have a form and four fields in it. Every time I enter a new record of one of these three fields, their values should be concatenated and the combined text put to the 'Details' field which is the fourth field. I think I need to use onupdateevent for this but I do not know-how.

I used onmodifyingfield event handler and it works but only when I enter and save the second record. I mean I save record and auto-populate does not work but when I save the second record and refresh the page I can see the auto-populated field in the first record. Here is my codes;

[DataEventHandler(tableStr(InventSite), DataEventType::ModifyingField)]
public static void InventSite_onModifyingField(Common sender, DataEventArgs e)
{
    MyTable myTable;

    update_recordset mytable setting Details = MyTable.Field1 + ", " + MyTable.Field2;

I would really be appreciated if anyone can help me with this.

Alex
  • 1,433
  • 9
  • 18
Granger
  • 21
  • 1
  • 10

1 Answers1

1

You can attach an event handler to Field1, Field2 and Field3 Modified events on the form DataSource and when it's triggered simply retrieve the current record, concatenate those values and write them into the Details field. The DataSource will then handle inserting or updating all those values into the database record:

[
    FormDataFieldEventHandler(formDataFieldStr(InventSite, InventSite, Field1), FormDataFieldEventType::Modified),
    FormDataFieldEventHandler(formDataFieldStr(InventSite, InventSite, Field2), FormDataFieldEventType::Modified),
    FormDataFieldEventHandler(formDataFieldStr(InventSite, InventSite, Field3), FormDataFieldEventType::Modified)
]
public static void Field1_OnModified(FormDataObject sender, FormDataFieldEventArgs e)
{
    // get the form DataSource
    FormDataSource dataSource = sender.datasource();
    
    // get current record
    InventSite inventSite = dataSource.cursor();
    
    // contatenate string values
    str details = strFmt("%1, %2, %3", inventSite.Field1, inventSite.Field2, inventSite.Field3);
    
    // update field value
    inventSite.Details = details;
}

Note: I don't understand what MyTable myTable buffer is in your code example, but in this case by looking at DataEventHandler I suppose that all four fields are created in the InventSite table.

Alex
  • 1,433
  • 9
  • 18
  • Thank you very much. Your code works great for 2 fields but I didn't mention one part(my mistake) One of the fields is the lookup field. Details field contains the name field of the lookup table. if I should more clear field 1: Name(it is the InventSite form) field 2: Description ( the same) field 3: Type ( it is a lookup field that comes from type form and we should take its name) Type form includes name and type fields. So tried to add eventhandler to the type form for 3. field but it didn't work I really would be appreciated and grateful if you can help me with this too. – Granger May 07 '21 at 15:21
  • If I need to explain more clearly, the value that I want to get is in the reference group (type: int64) I need to get its 'name(control name)' value, and Like others (field1 and field2, I need it to be written automatically in the 'details' field. please help... – Granger May 10 '21 at 10:44