Incrementing a counter for number of cases

Hi Commcare family ! hope everyone is safe

im trying to add a hidden value so that my app can count number of cases submitted and add value +1 every time a case is submitted by the same user
i’ve tried the below Xpath expressions:

coalesce(#case/visit_count, 0) + 1 , to counting visits , i gave visit_count a value ,saved visit_count as a property however i was presented with the following error : Validation Error: Problem with bind for /data/count contains invalid calculate expression

i also tried the count expression : count(instance(“casedb”)/casedb/case[index/parent = current()/@case_id][@status = “open”]) and i got a similar error as above

is there an example with Xpath that i can apply?

Regards

Hi there

try this out perhaps

count(instance(‘casedb’)/casedb/case[index/parent = instance(‘commcaresession’)/session/data/case_id][@status = ‘open’])

however, you should know that this will count the number of cases that are currently on the device that the code is running on. things like synchronization and locations/organizational hierarchy can affect what cases get passed down to the devices based on the username. this is of course more problematic in case you’re using case sharing.

in general, you want to pay attention to which cases are on the device is you’re using the count as a part of the ID. there are cases where some devices submitting the same or similar data can generate the same ID

HTH

Mazz

Hi Mazz - thanks for your reply

im not getting any more error messages but the hidden value keeps counting all the cases with a zero. i tried different testing approaches but it returns the same value every time : 0

Regards, stay safe

Hey there

yeah that might be the case if it’s not a child case of the case you’re already on. meaning, if you’re creating cases that are unrelated to this one, it won’t count them. and if they are, for the count to change, you have to submit your form to create those cases before it can count them.

so ideally, you’ll count whatever’s in the casedb instance, and then add the number of cases that WILL be created

Good luck!

Mazz