Here is a starting point from a database query to creating an AL1 HL7 segment.
var queryResult = qie.doQuery("CPS DB",
//"Select Name, Description, RASH, SHOCK, RESP, GI, HEME, OTHER, ONSETDATE, ISCRITICAL, GPI, DDID, KDC, ALLCLASS from ALLERGY where pid = {mc:PID} and XID = 1000000000000000000 and SDID = {TXA-12}");
"Select Name, Description, RASH, SHOCK, RESP, GI, HEME, OTHER, ONSETDATE, ISCRITICAL, GPI, DDID, KDC, ALLCLASS from ALLERGY where pid = {mc:PID} and XID = 1000000000000000000");
var count = queryResult.getCount('/');
for (var i=1 ; i <= count ; i++) {
message.addChild('/', 'AL1');
// if RASH, SHOCK, RESP, GI, HEME, OTHER are set to Y then use that as the reaction code
// GPI, DDID, KDC are all allergy codes
// ALLCLASS:
// D Drug allergy; DA
// F Food allergy; FA
// E Environmental allergy; MA
var allergyType = queryResult.getNode("ALLCLASS["+i+"]");
switch (allergyType){
case 'D': allergyType = 'DA';
break;
case 'F': allergyType = 'FA';
break;
default: allergyType = 'MA';
break;
}
// Description is the reaction note entered manually
message.setNode("AL1["+i+"]-1", i);
message.setNode("AL1["+i+"]-2", allergyType);
var allergyCoding = '';
var allergyCode = '';
if (StringUtils.isNotBlank(queryResult.getNode("GPI["+i+"]"))) {
allergyCode = queryResult.getNode("GPI["+i+"]");
allergyCoding = 'GPI';
} else if (StringUtils.isNotBlank(queryResult.getNode("DDID["+i+"]"))) {
allergyCode = queryResult.getNode("DDID["+i+"]");
allergyCoding = 'DDID';
} else if (StringUtils.isNotBlank(queryResult.getNode("KDC["+i+"]"))) {
allergyCode = queryResult.getNode("KDC["+i+"]");
allergyCoding = 'KDC';
}
message.setNode("AL1["+i+"]-3.1", allergyCode);
message.setNode("AL1["+i+"]-3.2", queryResult.getNode("Name["+i+"]"));
message.setNode("AL1["+i+"]-3.3", allergyCoding);
// ISCRITICAL Contains the criticality of the interaction.
// One of Y (Critical), S (Severe) = SV
// N (Moderate) = MO
// I (Insignificant) = MI
var allergySeverity = queryResult.getNode("ISCRITICAL["+i+"]");
switch (allergySeverity){
case 'Y': allergySeverity = 'SV';
break;
case 'S': allergySeverity = 'SV';
break;
case 'N': allergySeverity = 'MO';
break;
default: allergySeverity = 'MI';
break;
}
message.setNode("AL1["+i+"]-4", allergySeverity);
message.setNode("AL1["+i+"]-5", queryResult.getNode("Description["+i+"]"));
message.setNode("AL1["+i+"]-6", queryResult.getNode("ONSETDATE["+i+"]"));
}