unchase / Unchase.Odata.Connectedservice

:scroll: A Visual Studio extension for connecting to OData services with generating client-side C# proxy-classes
Apache License 2.0
44 stars 13 forks source link

C# OData MS Business Central - Add child record belonging to a parent #51

Closed nick-webbgh closed 2 years ago

nick-webbgh commented 3 years ago

I'm trying to add a Business Central [Sales Line] child record to a [Sales Header] parent invoice record in a C# OData program using an Unchase Connected Service. I know the header record exists because the program has just created it & I can see it in BC, but when I try to add the child record using the binding tablerelation Document No: 102206 I get an error (see below) that indicates it can't see the header record. I've tried synchronous and asynchronous methods; same failure. In both tables the tablerelation field is Code[20]. Same data type.

Error: The field Document No. of table Sales Line contains a value (102206) that cannot be found in the related table (Sales Header).

This doesn't make any sense. Any thoughts?

Sample code:-

ReferenceBC.NAV.APISalesLine lobjUSL = ReferenceBC.NAV.APISalesLine.CreateAPISalesLine("Invoice", "102206", 10000);

// lobjPSL is a Sales Invoice Line object (Posted) // lobjUSL is a Sales Invoice Line object (Unposted) // gobjDSC is the DataServiceContext object in the Unchase connected service

// populate properties lobjUSL.Property1 = lobjPSL.Property1; lobjUSL.Property2 = lobjPSL.Property2; // etc...

gobjDSC.AddToAPISalesLine(lobjUSL); gobjDSC.SaveChanges(SaveChangesOptions.None); // Error shown above