Skip to main content
Solved

REST API PUT requests suddenly return mostly "Sequence contains more than one matching element"

  • December 16, 2024
  • 9 replies
  • 227 views

Today all our PUT requests regarding Appointments seem to have stopped working via the REST API.  We’re using a custom endpoint.  Has anybody seen an error like this before?  We are a bit stuck, as all the endpoints were previously working.

"message": "An error has occurred.",

"exceptionMessage": "Sequence contains more than one matching element",

"exceptionType": "System.InvalidOperationException",

"stackTrace": 

at System.Linq.Enumerable.SingleOrDefault[TSource](IEnumerable`1 source, Func`2 predicate)

at PX.Api.ContractBased.EntityExportContextBuilder.GetDescriptorKeys(EntityImpl entity, String baseMappingKey, Boolean includeNulls) in C:\\build\\code_repo\\NetTools\\PX.Api.ContractBased\\EntityExportContextBuilder.cs:line 2064

at PX.Api.ContractBased.EntityExportContextBuilder.TryDetermineDescriptorFromValues(EntityImpl entity, String baseMappingKey, EntityDescriptor& entityDescriptor, Boolean includeNulls) in C:\\build\\code_repo\\NetTools\\PX.Api.ContractBased\\EntityExportContextBuilder.cs:line 2021

at PX.Api.ContractBased.EntityService.DetermineEntityDescriptor(ISystemContract systemContract, String version, String name, EntityImpl entity, EntityExportContextBuilder entityExportContextBuilder, EntityDescriptor& entityDescriptor, PXGraph graph, CbOperationContext operationContext, Boolean& isArchived) in C:\\build\\code_repo\\NetTools\\PX.Api.ContractBased\\EntityService.cs:line 763

at PX.Api.ContractBased.EntityService.Put(ISystemContract systemContract, String version, String name, EntityImpl entity, CbOperationContext operationContext, Boolean throwOnError) in C:\\build\\code_repo\\NetTools\\PX.Api.ContractBased\\EntityService.cs:line 653

at PX.Api.ContractBased.Soap.SoapFacadeBase.PutImpl(EntityImpl entity, Boolean throwOnValidationError) in C:\\build\\code_repo\\NetTools\\PX.Api.ContractBased\\Soap\\SoapFacadeBase.cs:line 126

at PX.Api.ContractBased.SystemContracts.V2.RestController.PutEntity(EntityImpl entity, String select, String filter, String expand, String custom) in C:\\build\\code_repo\\NetTools\\PX.Api.ContractBased\\SystemContracts\\V2\\RestController.cs:line 304

at lambda_method(Closure , Object , Object[] )

at System.Web.Http.Controllers.ReflectedHttpActionDescriptor.ActionExecutor.<>c__DisplayClass6_2.<GetExecutor>b__2(Object instance, Object[] methodParameters)

at System.Web.Http.Controllers.ReflectedHttpActionDescriptor.ExecuteAsync(HttpControllerContext controllerContext, IDictionary`2 arguments, CancellationToken cancellationToken)

 

Best answer by jinin

Hi ​@joegrist ,

The error message "Sequence contains more than one matching element" usually means a query in the Acumatica REST API or BQL is returning multiple records instead of one.

Check the database for duplicate records that match the query conditions.

9 replies

jinin
Pro I
Forum|alt.badge.img+11
  • Pro I
  • Answer
  • December 16, 2024

Hi ​@joegrist ,

The error message "Sequence contains more than one matching element" usually means a query in the Acumatica REST API or BQL is returning multiple records instead of one.

Check the database for duplicate records that match the query conditions.


Dmitrii Naumov
Acumatica Moderator
Forum|alt.badge.img+7
  • Acumatica Moderator
  • December 16, 2024

We’re using a custom endpoint.

@joegrist Have you made any changes to the endpoint recently?

One of the things that I frequently see is when people map a field from details/grid to the header of the endpoint entity. That causes the system to select multiple records (because it joins the details).

 

 


  • Author
  • Jr Varsity I
  • May 14, 2025

This error is caused by the system having trouble locating the entity to be operated on.  It's something to do with the endpoint setup I suppose.  

It magically went away, but is now happening again.

I’m going to try deleting and re-creating the endpoint.

Here is the stack trace in a newer version of Acumatica, reading it carefully indicates the system is getting duplicates while trying to locate the entity to update,

 

{
"message": "An error has occurred.",
"exceptionMessage": "Sequence contains more than one matching element",
"exceptionType": "System.InvalidOperationException",
"stackTrace": " at System.Linq.Enumerable.SingleOrDefault[TSource](IEnumerable`1 source, Func`2 predicate)\r\n at PX.Api.ContractBased.EntityExportContextBuilder.GetDescriptorKeys(EntityImpl entity, String baseMappingKey, Boolean includeNulls)\r\n at PX.Api.ContractBased.EntityExportContextBuilder.TryDetermineDescriptorFromValues(EntityImpl entity, String baseMappingKey, EntityDescriptor& entityDescriptor, Boolean includeNulls)\r\n at PX.Api.ContractBased.EntityService.DetermineEntityDescriptor(ISystemContract systemContract, String version, String name, EntityImpl entity, EntityExportContextBuilder entityExportContextBuilder, EntityDescriptor& entityDescriptor, PXGraph graph, CbOperationContext operationContext, Boolean& isArchived)\r\n at PX.Api.ContractBased.EntityService.Put(ISystemContract systemContract, String version, String name, EntityImpl entity, CbOperationContext operationContext, Boolean throwOnError)\r\n at PX.Api.ContractBased.AspNetCore.CbEndpointFeatureServiceExtensions.Put(IEntityService entityService, ICbEndpointFeature feature, EntityImpl entity, CbOperationContext operationContext)\r\n at PX.Api.ContractBased.WebApi.Controllers.EntityController.PutEntity(EntityImpl entity, String select, String filter, String expand, String custom)\r\n at lambda_method(Closure , Object , Object[] )\r\n at Microsoft.AspNetCore.Mvc.Internal.ActionMethodExecutor.SyncActionResultExecutor.Execute(IActionResultTypeMapper mapper, ObjectMethodExecutor executor, Object controller, Object[] arguments)\r\n at Microsoft.AspNetCore.Mvc.Internal.ControllerActionInvoker.<InvokeActionMethodAsync>d__12.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 Microsoft.AspNetCore.Mvc.Internal.ControllerActionInvoker.<InvokeNextActionFilterAsync>d__10.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 Microsoft.AspNetCore.Mvc.Internal.ControllerActionInvoker.Rethrow(ActionExecutedContext context)\r\n at Microsoft.AspNetCore.Mvc.Internal.ControllerActionInvoker.Next(State& next, Scope& scope, Object& state, Boolean& isCompleted)\r\n at Microsoft.AspNetCore.Mvc.Internal.ControllerActionInvoker.<InvokeInnerFilterAsync>d__13.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 Microsoft.AspNetCore.Mvc.Internal.ResourceInvoker.<InvokeNextExceptionFilterAsync>d__24.MoveNext()"
}

 


Dmitrii Naumov
Acumatica Moderator
Forum|alt.badge.img+7
  • Acumatica Moderator
  • May 14, 2025

@joegrist Do you have fields from ‘Details’ mapped to the header of the record in your endpoint?


  • Author
  • Jr Varsity I
  • May 16, 2025

@Dmitrii Naumov no, thad doesn’t seem to be the issue here.  Here is the XML, I can’t see anything about this that isn’t completely boring and normal….

 

            <TopLevelEntity name="Appointment" screen="FS300200">
<Fields>
<Field name="ActualDuration" type="StringValue" />
<Field name="ActualEndDate" type="DateTimeValue" />
<Field name="ActualEndTime" type="DateTimeValue" />
<Field name="ActualHandleManually" type="BooleanValue" />
<Field name="ActualServiceDuration" type="StringValue" />
<Field name="ActualStartDate" type="DateTimeValue" />
<Field name="ActualStartTime" type="DateTimeValue" />
<Field name="AppointmentNbr" type="StringValue" />
<Field name="Customer" type="StringValue" />
<Field name="Description" type="StringValue" />
<Field name="Details" type="AppointmentDetails[]" />
<Field name="EstimatedServiceDuration" type="StringValue" />
<Field name="Finished" type="BooleanValue" />
<Field name="Logs" type="AppointmentLogs[]" />
<Field name="ScheduledDuration" type="StringValue" />
<Field name="ScheduledEndDate" type="DateTimeValue" />
<Field name="ScheduledEndTime" type="DateTimeValue" />
<Field name="ScheduledHandleManually" type="BooleanValue" />
<Field name="ScheduledStartDate" type="DateTimeValue" />
<Field name="ScheduledStartTime" type="DateTimeValue" />
<Field name="ServiceOrderNbr" type="StringValue" />
<Field name="ServiceOrderType" type="StringValue" />
<Field name="Status" type="StringValue" />
</Fields>
<Mappings>
<Mapping field="ActualDuration">
<To object="AppointmentSelected" field="ActualDuration" />
</Mapping>
<Mapping field="Details">
<Mapping field="ActualDuration">
<To object="AppointmentDetails" field="ActualDuration" />
</Mapping>
<Mapping field="LineRef">
<To object="AppointmentDetails" field="LineRef" />
</Mapping>
<Mapping field="StaffMemberID">
<To object="AppointmentDetails" field="StaffID" />
</Mapping>
<Mapping field="AppointmentNbr">
<To object="AppointmentDetails" field="RefNbr" />
</Mapping>
<Mapping field="Branch">
<To object="AppointmentDetails" field="BranchID" />
</Mapping>
<Mapping field="EstimatedDuration">
<To object="AppointmentDetails" field="EstimatedDuration" />
</Mapping>
<Mapping field="InventoryID">
<To object="AppointmentDetails" field="InventoryID" />
</Mapping>
<Mapping field="LineNbr">
<To object="AppointmentDetails" field="LineNbr" />
</Mapping>
<Mapping field="LineStatus">
<To object="AppointmentDetails" field="UIStatus" />
</Mapping>
<Mapping field="LineType">
<To object="AppointmentDetails" field="LineType" />
</Mapping>
<Mapping field="Location">
<To object="AppointmentDetails" field="SiteLocationID" />
</Mapping>
</Mapping>
<Mapping field="EstimatedServiceDuration">
<To object="AppointmentRecords" field="EstimatedDurationTotal" />
</Mapping>
<Mapping field="Finished">
<To object="AppointmentSelected" field="Finished" />
</Mapping>
<Mapping field="ActualHandleManually">
<To object="AppointmentSelected" field="HandleManuallyActualTime" />
</Mapping>
<Mapping field="ScheduledHandleManually">
<To object="AppointmentSelected" field="HandleManuallyScheduleTime" />
</Mapping>
<Mapping field="Logs">
<Mapping field="AddtoActualDuration">
<To object="LogRecords" field="TrackOnService" />
</Mapping>
<Mapping field="LaborItem">
<To object="LogRecords" field="LaborItemID" />
</Mapping>
<Mapping field="LineNbr">
<To object="LogRecords" field="LineNbr" />
</Mapping>
<Mapping field="LogLineStatus">
<To object="LogRecords" field="Status" />
</Mapping>
<Mapping field="LogLineRef">
<To object="LogRecords" field="LineRef" />
</Mapping>
<Mapping field="ManageTimeManually">
<To object="LogRecords" field="KeepDateTimes" />
</Mapping>
<Mapping field="ServiceOrderType">
<To object="LogRecords" field="DocType" />
</Mapping>
<Mapping field="StaffMember">
<To object="LogRecords" field="BAccountID" />
</Mapping>
<Mapping field="StartDate">
<To object="LogRecords" field="DateTimeBegin_Date" />
</Mapping>
<Mapping field="StartTime">
<To object="LogRecords" field="DateTimeBegin_Time" />
</Mapping>
<Mapping field="TrackTime">
<To object="LogRecords" field="TrackTime" />
</Mapping>
<Mapping field="AppointmentNbr">
<To object="LogRecords" field="DocRefNbr" />
</Mapping>
<Mapping field="Description">
<To object="LogRecords" field="Descr" />
</Mapping>
<Mapping field="DetailLineRef">
<To object="LogRecords" field="DetLineRef" />
</Mapping>
<Mapping field="Duration">
<To object="LogRecords" field="TimeDuration" />
</Mapping>
<Mapping field="EarningType">
<To object="LogRecords" field="EarningType" />
</Mapping>
<Mapping field="EndDate">
<To object="LogRecords" field="DateTimeEnd_Date" />
</Mapping>
<Mapping field="EndTime">
<To object="LogRecords" field="DateTimeEnd_Time" />
</Mapping>
<Mapping field="InventoryID">
<To object="LogRecords" field="FSAppointmentDet__InventoryID" />
</Mapping>
</Mapping>
<Mapping field="ScheduledDuration">
<To object="AppointmentSelected" field="ScheduledDuration" />
</Mapping>
<Mapping field="ScheduledEndDate">
<To object="AppointmentSelected" field="ScheduledDateTimeEnd_Date" />
</Mapping>
<Mapping field="ScheduledEndTime">
<To object="AppointmentSelected" field="ScheduledDateTimeEnd_Time" />
</Mapping>
<Mapping field="ScheduledStartDate">
<To object="AppointmentSelected" field="ScheduledDateTimeBegin_Date" />
</Mapping>
<Mapping field="ActualEndDate">
<To object="AppointmentSelected" field="ActualDateTimeEnd_Date" />
</Mapping>
<Mapping field="ScheduledStartTime">
<To object="AppointmentSelected" field="ScheduledDateTimeBegin_Time" />
</Mapping>
<Mapping field="ServiceOrderNbr">
<To object="AppointmentRecords" field="SORefNbr" />
</Mapping>
<Mapping field="ServiceOrderType">
<To object="AppointmentRecords" field="SrvOrdType" />
</Mapping>
<Mapping field="Status">
<To object="AppointmentRecords" field="Status" />
</Mapping>
<Mapping field="ActualEndTime">
<To object="AppointmentSelected" field="ActualDateTimeEnd_Time" />
</Mapping>
<Mapping field="ActualServiceDuration">
<To object="AppointmentRecords" field="SrvOrdType" />
</Mapping>
<Mapping field="ActualStartDate">
<To object="AppointmentSelected" field="ExecutionDate" />
</Mapping>
<Mapping field="ActualStartTime">
<To object="AppointmentSelected" field="ActualDateTimeBegin_Time" />
</Mapping>
<Mapping field="AppointmentNbr">
<To object="AppointmentRecords" field="RefNbr" />
</Mapping>
<Mapping field="Customer">
<To object="AppointmentRecords" field="CustomerID" />
</Mapping>
<Mapping field="Description">
<To object="AppointmentRecords" field="DocDesc" />
</Mapping>
</Mappings>
<Actions>
<Action name="Complete" mappedTo="CompleteAppointment" />
<Action name="Start" mappedTo="StartAppointment" />
</Actions>
</TopLevelEntity>

 


  • Author
  • Jr Varsity I
  • May 16, 2025

By the way, this is very similar to the default endpoint, thete’s just less fields.  The purpose is to try and get the amount of data down as processing a lot of appointments is reeeeeeaaalllyyyyy slooooowwwww 


Dmitrii Naumov
Acumatica Moderator
Forum|alt.badge.img+7
  • Acumatica Moderator
  • May 16, 2025

@joegrist indeed, the mapping looks ok. Does the same request work with the default endpoint?

 

By the way, you don’t need to have an endpoint with limited set of fields. You can specify which subset of fields to return using the default endpoint. You need to specify the fields you want in the $select parameter and the other fields will not be returned.


  • Author
  • Jr Varsity I
  • May 16, 2025

@Dmitrii Naumov Yes indeed, the request seems to work just fine on the default endpoint.  I was getting subjectively faster results using a custom endpoint, my guess is that it’s doing less JOINs in the background.  But I don’t have proper statistics to prove that.  Would be nice to fix this, but the default endpoint is working for now…. however I’m concerned this will happen again, naturally 


Dmitrii Naumov
Acumatica Moderator
Forum|alt.badge.img+7
  • Acumatica Moderator
  • May 16, 2025

@joegrist please submit a support case for proper investigation. We’ll take a look.