Unable to set OBS -> ORDER relationship on an existing OBS

Hi, we have a piece of code on our project that creates TestOrder from Obs generated by a specific form. The relationship between Obs and Order is not working for existing Obs, even if we set the order using Obs.setOrder() and then saving the Obs.

The code looks something like this:

TestOrder order = createTestOrder(...); orderService.saveOrder(order, null); obs.setOrder(order); obsService.saveObs(obs, "Creating the test order");

We discovered that this is happening because the method that is called behind the scenes to copy the values of Obs Obs.newInstance() is not copying the order property. When we tried to change this method to copy the Order it broke test method transferEncounter_shouldTransferAnEncounterWithObservationsButNotOrdersToGivenPatient

We fixed the problem by manually voiding the current obs and creating the new one already with the order set, something like:

Obs newObs = Obs.newInstance(obs); newObs.setOrder(order); obs.setVoided(true); encounter.addObs(newObs); encounterService.saveEncounter(encounter);

Is there a better solution for this?

1 Like

Could you be having a unit test to reproduce this problem?