Versions Compared

Key

  • This line was added.
  • This line was removed.
  • Formatting was changed.

...

Code Block
languagejs
// Constants for hardcoded strings
var ADV_REASON_STRING = 'ADVERSE EVENT';
var STUDY_EVENT_NAME = 'AE / CM';
var FORM_NAME = 'AE / CM';
var AE_TREATMENT_ACTION_ITEM = 'AE_TREATMENT_ACTION';
var DRUG_WITHDRAWN_VALUE = 'Drug Withdrawn';

// Fetch the current reason value
var reason = itemJson.item.value;
logger('reason: ' + reason);

// If reason is not ADVERSE EVENT (case insensitive), log and return true
if (reason.toLowerCase() !== ADV_REASON_STRING.toLowerCase()) {
    logger('The reason is not an ADVERSE EVENT.');
    return true;
}

// Otherwise, perform lookup using findFormData
var aeForms = findFormData(STUDY_EVENT_NAME, FORM_NAME);
logger('Number of AE / CM forms found: ' + aeForms.length);

// Initialize flag to check for 'Drug Withdrawn'
var drugWithdrawnFound = false;

// Iterate over each form and check AE_TREATMENT_ACTION value
for (var i = 0; i < aeForms.length; i++) {
    var form = aeForms[i];
    var treatmentActionItem = findFirstItemByName(form, AE_TREATMENT_ACTION_ITEM);
    
    if (treatmentActionItem && treatmentActionItem.value) {
        logger('AE_TREATMENT_ACTION value in form ' + (i + 1) + ': ' + treatmentActionItem.value);
        
        // Check if the value is 'Drug Withdrawn' (case insensitive)
        if (treatmentActionItem.value.toLowerCase() === DRUG_WITHDRAWN_VALUE.toLowerCase()) {
            drugWithdrawnFound = true;
            break; // No need to continue, we found a match
        }
    } else {
        logger('AE_TREATMENT_ACTION not found or has no value in form ' + (i + 1));
    }
}

// If 'Drug Withdrawn' is found, return true
if (drugWithdrawnFound) {
    return true;
}

// If no 'Drug Withdrawn' is found, return false with an error message
customErrorMessage('No AE_TREATMENT_ACTION item with the value "Drug Withdrawn" was found.');
return false;

Edit Check Walkthrough

...