Error "Editing some fields [encounter] on Obs is not allowed" occurs when call saveEncounter

I am getting this error when trying to edit the obs on an encounter.

ERROR - BaseRestController.handleException(115) |2018-08-20 16:13:36,107| Editing some fields [encounter] on Obs is not allowed org.openmrs.api.APIException: Editing some fields [encounter] on Obs is not allowed

This occurs when it hits saveEncounter in this function that have to remove unwanted old obs :

	Set<Obs> existingObservations = encounter.getObsAtTopLevel(false);
	for (Obs obs : existingObservations) {
		if (removeObs(obs)) {
			encounter.removeObs(obs);
			obsService.purgeObs(obs);
		}
	}
	return encounterService.saveEncounter(encounter);

But it doesn’t always happen.

If I create the encounter through the encounter service as seen below, Encounter encounter = new Encounter(); encounter.setEncounterDatetime(new Date()); encounter.setEncounterType(encounterType); encounter.setLocation(visit.getLocation()); encounter.setPatient(visit.getPatient()); encounter.setVisit(visit);

	encounter = encounterService.saveEncounter(encounter);
	visit.addEncounter(encounter);

It doesn’t happen. If I create it with rest with this body content

{
  encounterDatetime: dateTime,
  encounterType: encounterType_uuid,
  visit: visit_uuid,
  patient: patient_uuid,
}

Then the error does happen. (Apologies if the answer to this question is the same as here : org.openmrs.api.APIException: Editing some fields on Obs is not allowed, it seemed like a different enough circumstance)

Can you share the json that you’re posting?

{encounterDatetime: “2018-08-20T20:36:53.954Z”, encounterType: “698b3ab5-ff32-4120-a1fd-7375748ea3a1”, visit: “b538bd8f-ce8b-4405-a7aa-c9f97ea0bd6a”, patient: “d86cb821-8d06-4c7f-9c02-572053dfdbeb”} posted to /openmrs/ws/rest/v1/encounter/

I know this is weird to ask but I’ve got to do it because am a little confused, are you sure that is it? That’s a new encounter with no observations, I wonder where the Obs is coming from.

So that’s my bad. I forgot a detail. There is an obs that is created in a separate call. That obs would look like this (its for a diagnosis if that matters).

{
  concept: visit_diagnosis_concept_uuid,
  obsDatetime: obsDateTime,
  encounter: encounter_uuid,
  person: person_uuid,
  groupMembers: [
    {
      concept: diagnosis_order_concept_uuid,
      value: primary_diagnosis_value_uuid,
      obsDatetime: obsDateTime,
      person: person_uuid,
    },
    {
      concept: diagnosis_certainty_concept_uuid,
      value: certainty_uuid,
      obsDatetime: obsDateTime,
      person: person_uuid,
    },
    {
      concept: diagnosis_problem_concept_uuid,
      value: diagnosis_problem_value_uuid,
      obsDatetime: obsDateTime,
      person: person_uuid,
    },
  ],
};

Before the obs is created, I do a check to see if an encounter exists and if not create one. So I was kind of thinking of the obs itself as unrelated to the error (since I can create the obs in the same way and have it attached to an encounter created through either method). But I could be wrong there.

Can you please share your code where you’re doing this because there is something that is still un clear. The way the Obs API works, you can’t create an Obs and then update it directly afterwards, to update an Obs, you need to clone the existing one(there is a utility method on the Obs class for this), make updates on the clone and save it. Alternatively, you can create the Encounter first and then the Obs next or at the same time by adding the Obs to Encounter and then save the encounter.

Sorry, might not be explaining things correctly.

Flow of program is that it creates an encounter via one of the two posted methods, an obs is created, old obs are deleted. Where do you mean I am updating the obs directly afterwards?

Also unless mistaken, I realized that the error occurs when call “encounterService.saveEncounter(encounter)” which is made entirely redundant by the “obsService.purgeObs(obs)” call since the obsService handles removing the obs. I’m still not sure why the encounterService’s saveEncounter will cause an error, but since obsService’s purgeObs takes care of removing the obs it seems like it isn’t a necessary call to make. Still kind of want to figure out why its happening.

@cmptrsean, any possibility for you to 1) add a (failing) context-sensitive test that provokes the error and 2) share your code? As in the GitHub repo/branch.

But earlier you did say the encounter has an Obs, as @mksd suggested, can you share a unit test that demonstrates what you are trying to do? Or your code.