CommCare Case Management

Hi Taylor,

Would it be possible to provide some sample code for using OpenFN to update cases in CommCare? I’ve been having trouble finding something that points to the proper syntax in the repo. Thanks!

Will

@Kevin, would you mind sharing your BWC Job #103 for Will here? I think it’s a really great example for creating a form submission that create/updates cases in CommCare. Also, you probably know this stuff better than I do with your various implementations—is there a better example to share?

Thanks in advance!

Taylor

P.S., Will, I think that case updates only happen by submitting a form: https://confluence.dimagi.com/display/commcarepublic/CommCare+Fundamentals±+Case+Management. So you’d use the same old Form Submission API and the following syntax: https://github.com/OpenFn/language-commcare#submitting-data-to-commcare-hq

submit(
  fields(
    field("@", function(state) {
      return {
        "xmlns": "http://openrosa.org/formdesigner/2BCC3E88-2D0D-4C07-8D4A-6B372F3799D9"
      };
    }),
    field("paitent_namentosh", dataValue("first_name")),
    field("question2", "Some answer here."),
    field("question3", "HKS"),
    field("question4", "item1"),
    field("question5", 69855),
    field("question6", 12)
  )
)

This is probably the best example I can think of to create/update a case in CommCare. It’s probably more information that you wanted, but I figured I would send the whole thing over so you can see it from start to finish. Hope this is helpful!

Kevin

job103.txt (4.41 KB)

Thanks, Kevin! This is great. (Pasting below as a code block so it’s searchable.)

For each “Participant” in Salesforce, create new or update existing CommCare cases:

each( "$.data.participants[*]", submit( fields( field("@", function(state) { return { "xmlns:jrm": "[http://dev.commcarehq.org/jr/xforms](http://dev.commcarehq.org/jr/xforms)", "xmlns": "[http://openrosa.org/formdesigner/22A693D2-7F34-4B8D-AE12-8A4A57C8868F](http://openrosa.org/formdesigner/22A693D2-7F34-4B8D-AE12-8A4A57C8868F)", "uiVersion": "1", "version": "325", "name": "New Participant", }; }), field("Name", function(state){ var name = ''; if(dataValue("participantName")(state) !== null) { name = dataValue("participantName")(state); } if(dataValue("participantLastName")(state) !== null) { name = name+' '+dataValue("participantLastName")(state); } if(dataValue("participantMiddleName")(state) !== null) { name = name+' ('+dataValue("participantMiddleName")(state)+')'; } return name; }), field("TNS_Id", dataValue("tnsId")), field("Training_Group", dataValue("trainingGroupId")), field("Market", dataValue("market")), field("Location", dataValue("trainingGroupLocationName")), field("Case_Id", dataValue("participantId")), field("Name_Id_Concat", function(state){ var nameConcat = ''; if(dataValue("participantName")(state) !== null) { nameConcat = dataValue("participantName")(state); } if(dataValue("participantLastName")(state) !== null) { nameConcat = nameConcat+' '+dataValue("participantLastName")(state); } if(dataValue("participantMiddleName")(state) !== null) { nameConcat = nameConcat+' ('+dataValue("participantMiddleName")(state)+')'; } if(dataValue("tnsId")(state) !== null) { nameConcat = nameConcat+' '+dataValue("tnsId")(state); } return nameConcat; }), field("n0:case", function(state) { return { "@": { "case_id": dataValue("participantId")(state), "date_modified": new Date().toISOString(), "user_id": "94013b3626176881a5c2e6094de1ec80", "xmlns:n0": "[http://commcarehq.org/case/transaction/v2](http://commcarehq.org/case/transaction/v2)" }, "n0:create": { "n0:case_name": function(){ var name = ''; if(dataValue("participantName")(state) !== null) { name = dataValue("participantName")(state); } if(dataValue("participantLastName")(state) !== null) { name = name+' '+dataValue("participantLastName")(state); } if(dataValue("participantMiddleName")(state) !== null) { name = name+' ('+dataValue("participantMiddleName")(state)+')'; } return name; }, "n0:owner_id": dataValue("ccMobileWorkerGroupId")(state), "n0:case_type": "Participant" }, "n0:update": { "n0:Case_Id": dataValue("participantId")(state), "n0:Location": dataValue("trainingGroupLocationName")(state), "n0:Market": dataValue("market")(state), "n0:Name_Id_Concat": function() { var nameConcat = ''; if(dataValue("participantName")(state) !== null) { nameConcat = dataValue("participantName")(state); } if(dataValue("participantLastName")(state) !== null) { nameConcat = nameConcat+' '+dataValue("participantLastName")(state); } if(dataValue("participantMiddleName")(state) !== null) { nameConcat = nameConcat+' ('+dataValue("participantMiddleName")(state)+')'; } if(dataValue("tnsId")(state) !== null) { nameConcat = nameConcat+' '+dataValue("tnsId")(state); } return nameConcat; }, "n0:TNS_Id": dataValue("tnsId")(state), "n0:Training_Group": dataValue("trainingGroupId")(state) } }; }), field("n1:meta", function(state) { return { "@": {"xmlns:n1": "[http://openrosa.org/jr/xforms](http://openrosa.org/jr/xforms)"}, "n1:deviceID": "867066029216796", "n1:timeStart": new Date().toISOString(), "n1:timeEnd": new Date().toISOString(), "n1:username": "openfn", "n1:userID": "94013b3626176881a5c2e6094de1ec80" }; }) ) ) );

End.