Create Task based on manually entered date

FLHW user creates the patient .
FLHW fills 2 form for one patient i.e( Enrollment form & Follow up form)

After FLHW fills the first form of patient i.e Enrollment form .
Enrollment form has a question where next visit date is filled manually.

Based on manually entered next visit date on Enrollment form , A new task should be created(i.e follow-up form ).

Hi @Sanjit7,

It looks like you want to specify the due date for a task using a date field on the source report.
For that, you can use dueDate instead of days in the events.

Here is the reference document you can follow to understand how to use dueDate:

Here are some examples of dueDate being used in tasks:

  1. Tutorial:
    Building A Complex Task (Optional) | Community Health Toolkit

  2. Default config:
    https://github.com/medic/cht-core/blob/f4a5f11197e19439af863c1c2732fe14f4f73a07/config/default/tasks.js#L179-L181

Got error while creating task


module.exports = [
  {
    name: 'follow_up',
    title: 'task.followup.title',
    appliesTo: 'reports',
    appliesToType: ['enrollment_form'],
    appliesIf: function (contact, report) {
      const SBP = Utils.getField(report, 'g3_bp_measurement.q20_sbp');  
      const DBP = Utils.getField(report, 'g3_bp_measurement.q21_dbp');
      return SBP >= 140 || DBP >= 90;
    },
    actions: [
      {
        form: 'follow_up'
      }
    ],
    events: [
      {
        id: 'follow-up-event-id',
        start: 0,
        end: 1,
        dueDate: function (event, contact, report) {
          const nextVisitDate = Utils.getField(report, 'g4.q36_next_visit_date');
          return Utils.addDate(nextVisitDate);
        }
      }
    ],
    resolvedIf: function (contact, report, event, dueDate) {
      return Utils.isFormSubmittedInWindow(
        contact.reports,
        'enrollment_form',
        Utils.addDate(dueDate, -event.start).getTime(),
        Utils.addDate(dueDate, event.end + 1).getTime()
      );
    }

  }
];

Error code

addDate@https://192.168.137.155/main.js:1:1792115
dueDate@https://192.168.137.155/main.js line 1 > Function:3:1944
p@https://192.168.137.155/main.js line 1 > Function:3:6294
a@https://192.168.137.155/main.js line 1 > Function:3:6073
anonymous/</e.exports@https://192.168.137.155/main.js line 1 > Function:3:7637
anonymous/<@https://192.168.137.155/main.js line 1 > Function:3:1456
o@https://192.168.137.155/main.js line 1 > Function:3:110
anonymous/<@https://192.168.137.155/main.js line 1 > Function:3:902
anonymous@https://192.168.137.155/main.js line 1 > Function:3:911
y@https://192.168.137.155/main.js:1:2658522
getEmissionsFor@https://192.168.137.155/main.js:1:2660760
74967/p.exports@https://192.168.137.155/main.js:1:2656793
q@https://192.168.137.155/main.js:1:2655340
98017/O/</<@https://192.168.137.155/main.js:1:2655780
invoke@https://192.168.137.155/polyfills.js:1:10767
run@https://192.168.137.155/polyfills.js:1:6134
78311/</J/<@https://192.168.137.155/polyfills.js:1:21030
invokeTask@https://192.168.137.155/polyfills.js:1:11385
runTask@https://192.168.137.155/polyfills.js:1:6758
M@https://192.168.137.155/polyfills.js:1:13425
invokeTask@https://192.168.137.155/polyfills.js:1:12466
q@https://192.168.137.155/polyfills.js:1:25168
ee@https://192.168.137.155/polyfills.js:1:25478
ye@https://192.168.137.155/polyfills.js:1:25760

It looks like CHT has encountered a problem when running the Utils.addDate function.

Although it might not be the cause of the error above, the function requires two arguments:

  1. a date to start with
  2. the number of days to add

You are providing only the first argument i.e. nextVisitDate.
Also, please make sure that nextVisitDate is a valid JS Date.
You are probably getting a string from the report in a format similar to 2023-11-28. You need to convert it into JS date:

const nextVisitDate = Utils.getField(report, 'g4.q36_next_visit_date');
//optional: check in browser log that you are getting the correct date
console.log('next visit': nextVisitDate); //remove later
const nextVisitDateJS = new Date(nextVisitDate);
return nextVisitDateJS;

You might have more details about the error in the browser console. Please check.