Skip to main content

Notifications

Announcements

No record found.

Community site session details

Community site session details

Session Id :
Finance | Project Operations, Human Resources, ...
Answered

Add field to response in event

(2) ShareShare
ReportReport
Posted on by 32
Hello Dynamics 365 Community,
I need to extend an OData response, when we read an entity. I resently learned about extensions and tried to do it with them. I was also happy that I could hook on the postLoaded event to search for the needed data using select statements.
I have my information now but no clue how to add them to the response. I saw suggestions to add custom fields in extensions, but that is not working for me since I am extending a tableStr. (See my simplified code example below).
For a deeper understanding: I want to extend the Status of an ECO Header to the Response.
 
[ExtensionOf(tableStr(EngChgEngineeringChangeOrderHeaderEntity))]
final class EngChgEngineeringChangeOrderHeaderEntity_Extension
{

    [DataEventHandler(tableStr(EngChgEngineeringChangeOrderHeaderEntity), DataEventType::PostedLoad)]
    public static void EngChgEngineeringChangeOrderHeaderEntity_onPostedLoad(Common _sender, DataEventArgs _eventArgs)
    {
        // Get event data as entity
        EngChgEngineeringChangeOrderHeaderEntity ecoHeaderEntity = _sender as EngChgEngineeringChangeOrderHeaderEntity;
        
        // Perform actions in correct company
        changecompany(ecoHeaderEntity.DataAreaId)
        {
            // Read data from EngChgEcmHeader table (to also read Status)
            EngChgEcmHeader engChgEcmHeader;
            select RecId, Status from engChgEcmHeader
                where engChgEcmHeader.RecId == _sender.RecId;
           
            if (engChgEcmHeader)
            {
                print(engChgEcmHeader.Status);
            }
            
        }
    }

}
 
I am reaching out to you since I have the feeling I am doing something completly wrong. How do you solve something like that? 
 
Categories:
  • Waed Ayyad Profile Picture
    7,760 Super User 2025 Season 1 on at
    Add field to response in event
     
    Can you verify the answer that helped you.
  • CH-11120820-0 Profile Picture
    32 on at
    Add field to response in event
    Thank you!
  • Waed Ayyad Profile Picture
    7,760 Super User 2025 Season 1 on at
    Add field to response in event

    Hi, 

    Is your issue resolved? If yes, mark the answers that helped you as verified. 

    Thanks,

    Waed Ayyad

  • Verified answer
    Waed Ayyad Profile Picture
    7,760 Super User 2025 Season 1 on at
    Add field to response in event
    Hi,
     
    Did you do full build and Sync database after you added the new field? Also did you refresh entity list?
     
    Check the following link about adding new field to data entity using extension: d365ffo-ax-add-new-field-to-a-data-entity
     
    Also check the following link about refresh entity list: d365-finance-missing-data-entities-refresh-entity-list
     

    Thanks,

    Waed Ayyad

    If this helped, please mark it as "Verified" for others facing the same issue

  • CH-11120820-0 Profile Picture
    32 on at
    Add field to response in event
    Thank you for the quick answer. Maybe I am doing something wrong here. 
    When I am trying to add the field to the data source extension and the staging, my OData fetch request is not working anymore. This is my stacktrace:
     
    at Microsoft.Dynamics.Ax.MSIL.Interop.throwException(Int32 ExceptionValue, interpret* ip)\r\n   
    at Microsoft.Dynamics.Ax.MSIL.cqlCursorIL.NextRec(IntPtr table)\r\n   
    at Microsoft.Dynamics.Ax.Xpp.Common.NextRec() in D:\\dbs\\sh\\l23t\\0520_210300\\cmd\\b\\Source\\Kernel\\xppil\\XppSupport\\Common.cs:line 1038\r\n   
    at Microsoft.Dynamics.AX.Framework.Linq.Data.Msil.AxRecord.MoveNext()\r\n   
    at Microsoft.Dynamics.AX.Framework.Linq.Data.AxProjectionReader`1.Enumerator.MoveNext() in D:\\dbs\\sh\\l23t\\0520_210300\\cmd\\b\\Source\\Frameworks\\LinqToManagedInteropLayer\\Microsoft.Dynamics.Ax.Linq.Data\\AxProjectionReader.cs:line 280\r\n   
    at Microsoft.Dynamics.Platform.Integration.Services.OData.Query.QueryEvaluator.Evaluate(IQueryable query, IServicesRequestSessionProvider servicesRequestSessionProvider, IODataSecurityRights securityRights)\r\n   
    at Microsoft.Dynamics.Platform.Integration.Services.OData.Query.QueryEvaluator.Single(IQueryable query, IServicesRequestSessionProvider servicesRequestSessionProvider, IODataSecurityRights securityRights)\r\n   
    at Microsoft.Dynamics.Platform.Integration.Services.OData.AxODataController.Get()\r\n   
    at lambda_method(Closure , Object , Object[] )\r\n   
    at System.Web.Http.Controllers.ReflectedHttpActionDescriptor.ActionExecutor.<>c__DisplayClass10.<GetExecutor>b__9(Object instance, Object[] methodParameters)\r\n   
    at System.Web.Http.Controllers.ReflectedHttpActionDescriptor.ExecuteAsync(HttpControllerContext controllerContext, IDictionary`2 arguments, CancellationToken cancellationToken)\r\n
    --- End of stack trace from previous location where exception was thrown ---\r\n   
    at System.Runtime.ExceptionServices.ExceptionDispatchInfo.Throw()\r\n   
    at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task)\r\n   
    at System.Web.Http.Tracing.ITraceWriterExtensions.<TraceBeginEndAsyncCore>d__18`1.MoveNext()\r\n
    --- End of stack trace from previous location where exception was thrown ---\r\n   
    at System.Runtime.ExceptionServices.ExceptionDispatchInfo.Throw()\r\n   
    at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task)\r\n   
    at System.Web.Http.Controllers.ApiControllerActionInvoker.<InvokeActionAsyncCore>d__0.MoveNext()\r\n
    --- End of stack trace from previous location where exception was thrown ---\r\n   
    at System.Runtime.ExceptionServices.ExceptionDispatchInfo.Throw()\r\n   
    at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task)\r\n   
    at System.Web.Http.Tracing.ITraceWriterExtensions.<TraceBeginEndAsyncCore>d__18`1.MoveNext()\r\n
    --- End of stack trace from previous location where exception was thrown ---\r\n   
    at System.Runtime.ExceptionServices.ExceptionDispatchInfo.Throw()\r\n   
    at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task)\r\n   
    at System.Web.Http.Filters.ActionFilterAttribute.<CallOnActionExecutedAsync>d__5.MoveNext()\r\n
    --- End of stack trace from previous location where exception was thrown ---\r\n   
    at System.Runtime.ExceptionServices.ExceptionDispatchInfo.Throw()\r\n   
    at System.Web.Http.Filters.ActionFilterAttribute.<CallOnActionExecutedAsync>d__5.MoveNext()\r\n
    --- End of stack trace from previous location where exception was thrown ---\r\n   
    at System.Runtime.ExceptionServices.ExceptionDispatchInfo.Throw()\r\n   
    at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task)\r\n   
    at System.Web.Http.Filters.ActionFilterAttribute.<ExecuteActionFilterAsyncCore>d__0.MoveNext()\r\n
    --- End of stack trace from previous location where exception was thrown ---\r\n   
    at System.Runtime.ExceptionServices.ExceptionDispatchInfo.Throw()\r\n   
    at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task)\r\n   
    at System.Web.Http.Filters.ActionFilterAttribute.<CallOnActionExecutedAsync>d__5.MoveNext()\r\n
    --- End of stack trace from previous location where exception was thrown ---\r\n   
    at System.Runtime.ExceptionServices.ExceptionDispatchInfo.Throw()\r\n   
    at System.Web.Http.Filters.ActionFilterAttribute.<CallOnActionExecutedAsync>d__5.MoveNext()\r\n
    --- End of stack trace from previous location where exception was thrown ---\r\n   
    at System.Runtime.ExceptionServices.ExceptionDispatchInfo.Throw()\r\n   
    at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task)\r\n   
    at System.Web.Http.Filters.ActionFilterAttribute.<ExecuteActionFilterAsyncCore>d__0.MoveNext()\r\n
    --- End of stack trace from previous location where exception was thrown ---\r\n   
    at System.Runtime.ExceptionServices.ExceptionDispatchInfo.Throw()\r\n   
    at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task)\r\n   
    at System.Web.Http.Controllers.ActionFilterResult.<ExecuteAsync>d__2.MoveNext()\r\n
    --- End of stack trace from previous location where exception was thrown ---\r\n   
    at System.Runtime.ExceptionServices.ExceptionDispatchInfo.Throw()\r\n   
    at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task)\r\n   
    at System.Web.Http.Controllers.ExceptionFilterResult.<ExecuteAsync>d__0.MoveNext()\r\n
    --- End of stack trace from previous location where exception was thrown ---\r\n   
    at System.Runtime.ExceptionServices.ExceptionDispatchInfo.Throw()\r\n   
    at System.Web.Http.Controllers.ExceptionFilterResult.<ExecuteAsync>d__0.MoveNext()\r\n
    --- End of stack trace from previous location where exception was thrown ---\r\n   
    at System.Runtime.ExceptionServices.ExceptionDispatchInfo.Throw()\r\n   
    at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task)\r\n   
    at System.Web.Http.Tracing.Tracers.HttpControllerTracer.<ExecuteAsyncCore>d__5.MoveNext()\r\n
    --- End of stack trace from previous location where exception was thrown ---\r\n   
    at System.Runtime.ExceptionServices.ExceptionDispatchInfo.Throw()\r\n   
    at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task)\r\n   
    at System.Web.Http.Tracing.ITraceWriterExtensions.<TraceBeginEndAsyncCore>d__18`1.MoveNext()\r\n
    --- End of stack trace from previous location where exception was thrown ---\r\n   
    at System.Runtime.ExceptionServices.ExceptionDispatchInfo.Throw()\r\n   
    at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task)\r\n   
    at System.Web.Http.Dispatcher.HttpControllerDispatcher.<SendAsync>d__1.MoveNext()
     
    Maybe I am not adding the fields correctly. Is there any documentation/video on how to add these fields? 
  • Verified answer
    Martin Dráb Profile Picture
    231,723 Most Valuable Professional on at
    Add field to response in event
    Moved from Integration, Dataverse, and general topics forum.
     
    The correct solution is easier and better than your current approach. EngChgEcmHeader is already a data source of the entity (e.g. the OrderNumber field is bound to it), therefore you don't any additional select statement and a virtual field. Simply create a data entity extension (of EngChgEngineeringChangeOrderHeaderEntity) and add a new field bound to EngChgEcmHeader.Status). Because the entity supports data management, create also an extension of EngChgEngineeringChangeOrderHeaderStaging table and add a field of the same name (and EDT) there. And that's it; you don't need any code at all.

Under review

Thank you for your reply! To ensure a great experience for everyone, your content is awaiting approval by our Community Managers. Please check back later.

Helpful resources

Quick Links

Daivat Vartak – Community Spotlight

We are honored to recognize Daivat Vartak as our March 2025 Community…

Announcing Our 2025 Season 1 Super Users!

A new season of Super Users has arrived, and we are so grateful for the daily…

Kudos to the February Top 10 Community Stars!

Thanks for all your good work in the Community!

Leaderboard

#1
André Arnaud de Calavon Profile Picture

André Arnaud de Cal... 292,850 Super User 2025 Season 1

#2
Martin Dráb Profile Picture

Martin Dráb 231,723 Most Valuable Professional

#3
nmaenpaa Profile Picture

nmaenpaa 101,156 Moderator

Leaderboard

Product updates

Dynamics 365 release plans