Is there a way to optimise this code?

/**
Is there a way to optimise this code by
1. looping instead of the long repetition.
2. Also, to display nothing (instead of string 'NHS Number') when 'NHSIDNumber' is null
----
When NHS is 'not null':
|abc9874^^^MRN|1234567890^^^NHS Number|

----

When NHS is null:
Correct Display Should be:
|abc9874^^^MRN||  

Wrong Display:
abc9874^^^MRN|^^^NHS Number|
**/


var next = output.append(input[0]);

var body = next.text;

var type;

if(body.indexOf("PV1|") > 0)
{
      next.setProperty("PatientClass",next.getField("PV1.PatientClass"));
}

//*
//Start SWAP NHS + PASID
//*

var sPID = "PID."; //default to 'normal'



// Get the number of items in the array
var count = next.getRepeatCount(sPID + "PatientIdentifierList");

next.setProperty("pCount",count);

var NHSIDNumber = "";
var PASIDNumber = "";
//var PASAA = "";
//var PASAF = "";

if (count > 0)
{
      for(var i = 0; i < count; i ++)
      {            
            
            // Get the field's current value       
            if (next.getField(sPID + "PatientIdentifierList["+ i +"].IdentifierTypeCode") == "NHS")
            {
                  NHSIDNumber = next.getField(sPID + "PatientIdentifierList["+ i +"].IDNumber")            
                  next.setProperty("NHSID", NHSIDNumber);
            }
            else if (next.getField(sPID + "PatientIdentifierList["+ i +"].IdentifierTypeCode") == "FAH")
            {
                  PASIDNumber = next.getField(sPID + "PatientIdentifierList["+ i +"].IDNumber")      
                  //PASAA = next.getField(sPID + "PatientIdentifierList["+ i +"].AssigningAuthority.NamespaceID");
                  //PASAF = next.getField(sPID + "PatientIdentifierList["+ i +"].AssigningFacility.NamespaceID");
                  next.setProperty("PASID", PASIDNumber);
            }                  
      }            
      
            
      next.setField(sPID + "PatientIdentifierList[0].IDNumber",PASIDNumber);
      next.setField(sPID + "PatientIdentifierList[0].AssigningAuthority.NamespaceID", "MRN");
      next.setField(sPID + "PatientIdentifierList[0].IdentifierTypeCode", '');
      //next.setField(sPID + "PatientIdentifierList[0].AssigningAuthority.NamespaceID",PASAA);
      //next.setField(sPID + "PatientIdentifierList[0].AssigningFacility.NamespaceID",PASAF);
            
      
      next.setField(sPID + "AlternatePatientID[0].IDNumber",NHSIDNumber);
      next.setField(sPID + "AlternatePatientID[0].AssigningAuthority.NamespaceID", "NHS Number");
      next.setField(sPID + "AlternatePatientID[0].IdentifierTypeCode", '');
      //next.setField(sPID + "AlternatePatientID[0].IdentifierTypeCode.NamespaceID",'');
      //next.setField(sPID + "AlternatePatientID[0].IdentifierTypeCode.NamespaceID",'');
      
      next.setField(sPID + "PatientIdentifierList[1].IDNumber",'');
      next.setField(sPID + "PatientIdentifierList[1].IdentifierTypeCode", '');
      next.setField(sPID + "PatientIdentifierList[1].AssigningAuthority.NamespaceID", '');
      next.setField(sPID + "PatientIdentifierList[1].AssigningFacility.NamespaceID", '');
      
      next.setField(sPID + "PatientIdentifierList[2].IDNumber",'');
      next.setField(sPID + "PatientIdentifierList[2].IdentifierTypeCode", '');
      next.setField(sPID + "PatientIdentifierList[2].AssigningAuthority.NamespaceID", '');
      next.setField(sPID + "PatientIdentifierList[2].AssigningFacility.NamespaceID", '');
      
      next.setField(sPID + "PatientIdentifierList[3].IDNumber",'');
      next.setField(sPID + "PatientIdentifierList[3].IdentifierTypeCode", '');
      next.setField(sPID + "PatientIdentifierList[3].AssigningAuthority.NamespaceID", '');
      next.setField(sPID + "PatientIdentifierList[3].AssigningFacility.NamespaceID", '');
      
      next.setField(sPID + "PatientIdentifierList[4].IDNumber",'');
      next.setField(sPID + "PatientIdentifierList[4].IdentifierTypeCode", '');
      next.setField(sPID + "PatientIdentifierList[4].AssigningAuthority.NamespaceID", '');
      next.setField(sPID + "PatientIdentifierList[4].AssigningFacility.NamespaceID", '');
      
      next.setField(sPID + "PatientIdentifierList[5].IDNumber",'');
      next.setField(sPID + "PatientIdentifierList[5].IdentifierTypeCode", '');
      next.setField(sPID + "PatientIdentifierList[5].AssigningAuthority.NamespaceID", '');
      next.setField(sPID + "PatientIdentifierList[5].AssigningFacility.NamespaceID", '');
      
      next.setField(sPID + "PatientIdentifierList[6].IDNumber",'');
      next.setField(sPID + "PatientIdentifierList[6].IdentifierTypeCode", '');
      next.setField(sPID + "PatientIdentifierList[6].AssigningAuthority.NamespaceID", '');
      next.setField(sPID + "PatientIdentifierList[6].AssigningFacility.NamespaceID", '');
      
      next.setField(sPID + "PatientIdentifierList[7].IDNumber",'');
      next.setField(sPID + "PatientIdentifierList[7].IdentifierTypeCode", '');
      next.setField(sPID + "PatientIdentifierList[7].AssigningAuthority.NamespaceID", '');
      next.setField(sPID + "PatientIdentifierList[7].AssigningFacility.NamespaceID", '');
      
      next.setField(sPID + "PatientIdentifierList[8].IDNumber",'');
      next.setField(sPID + "PatientIdentifierList[8].IdentifierTypeCode", '');
      next.setField(sPID + "PatientIdentifierList[8].AssigningAuthority.NamespaceID", '');
      next.setField(sPID + "PatientIdentifierList[8].AssigningFacility.NamespaceID", '');
      
      next.setField(sPID + "PatientIdentifierList[9].IDNumber",'');
      next.setField(sPID + "PatientIdentifierList[9].IdentifierTypeCode", '');
      next.setField(sPID + "PatientIdentifierList[9].AssigningAuthority.NamespaceID", '');
      next.setField(sPID + "PatientIdentifierList[9].AssigningFacility.NamespaceID", '');
      
      next.setField(sPID + "PatientIdentifierList[10].IDNumber",'');
      next.setField(sPID + "PatientIdentifierList[10].IdentifierTypeCode", '');
      next.setField(sPID + "PatientIdentifierList[10].AssigningAuthority.NamespaceID", '');
      next.setField(sPID + "PatientIdentifierList[10].AssigningFacility.NamespaceID", '');
}      
//**
//*End SWAP      NHS + PASID
//**
Olugbnega OyeneyeDeveloperAsked:
Who is Participating?
 
Kim WalkerConnect With a Mentor Web Programmer/TechnicianCommented:
Be sure to always select all your code and click the CODE button above the comment/question window so it will be displayed in a format that is easy to parse.

It's difficult to say if anything can be optimized without knowing what is returned by output.append(input[0]);. I'm guessing output is a custom object.

But the following long repeat code is easily replaced with a loop
      next.setField(sPID + "PatientIdentifierList[1].IDNumber",'');
      next.setField(sPID + "PatientIdentifierList[1].IdentifierTypeCode", '');
      next.setField(sPID + "PatientIdentifierList[1].AssigningAuthority.NamespaceID", '');
      next.setField(sPID + "PatientIdentifierList[1].AssigningFacility.NamespaceID", '');
      
      ... 

      next.setField(sPID + "PatientIdentifierList[10].IDNumber",'');
      next.setField(sPID + "PatientIdentifierList[10].IdentifierTypeCode", '');
      next.setField(sPID + "PatientIdentifierList[10].AssigningAuthority.NamespaceID", '');
      next.setField(sPID + "PatientIdentifierList[10].AssigningFacility.NamespaceID", '');

Open in new window

Can be reduced to
      var ii = 1;
      while (ii <= 10) {
          next.setField(sPID + "PatientIdentifierList[" + ii +"].IDNumber",'');
          next.setField(sPID + "PatientIdentifierList[" + ii +"].IdentifierTypeCode", '');
          next.setField(sPID + "PatientIdentifierList[" + ii +"].AssigningAuthority.NamespaceID", '');
          next.setField(sPID + "PatientIdentifierList[" + ii +"].AssigningFacility.NamespaceID", '');
          ii++;
      }

Open in new window

2. I don't see anything in this code that generates any output -- certainly not the string that ends with "NHS Number."
0
 
Olugbnega OyeneyeDeveloperAuthor Commented:
Yeah, that solved that bit. Thank you.
It is HL7 messaging., using java script to transform it and the message is in transit.

 var ii = 1;
      while (ii <= i) {
          next.setField(sPID + "PatientIdentifierList[" + ii +"].IDNumber",'');
          next.setField(sPID + "PatientIdentifierList[" + ii +"].IdentifierTypeCode", '');
          next.setField(sPID + "PatientIdentifierList[" + ii +"].AssigningAuthority.NamespaceID", '');
          next.setField(sPID + "PatientIdentifierList[" + ii +"].AssigningFacility.NamespaceID", '');
          ii++;
      }
0
 
Olugbnega OyeneyeDeveloperAuthor Commented:
//This is solved:
//=============


/* Single input message case */
// Create the output message
var next = output.append(input[0]);

var body = next.text;

var type;

if(body.indexOf("PV1|") > 0)
{
      next.setProperty("PatientClass",next.getField("PV1.PatientClass"));
}

//*
//Start SWAP NHS + PASID
//*

var sPID = "PID."; //default to 'normal'

//if (next.getField("MSH.MessageType.TriggerEvent") == "A40")
//{
//      sPID = "PatientInformation[0].PID."
//}

// Get the number of items in the array
var count = next.getRepeatCount(sPID + "PatientIdentifierList");

next.setProperty("pCount",count);

var NHSIDNumber = "";
var PASIDNumber = "";
//var PASAA = "";
//var PASAF = "";

if (count > 0)
{
      for(var i = 0; i < count; i ++)
      {            
            
            // Get the field's current value       
            if (next.getField(sPID + "PatientIdentifierList["+ i +"].IdentifierTypeCode") == "NHS")
            {
                  NHSIDNumber = next.getField(sPID + "PatientIdentifierList["+ i +"].IDNumber")            
                  next.setProperty("NHSID", NHSIDNumber);
            }
            else if (next.getField(sPID + "PatientIdentifierList["+ i +"].IdentifierTypeCode") == "FACIL")
            {
                  PASIDNumber = next.getField(sPID + "PatientIdentifierList["+ i +"].IDNumber")      
                  //PASAA = next.getField(sPID + "PatientIdentifierList["+ i +"].AssigningAuthority.NamespaceID");
                  //PASAF = next.getField(sPID + "PatientIdentifierList["+ i +"].AssigningFacility.NamespaceID");
                  next.setProperty("PASID", PASIDNumber);
            }                  
      }            
      
            
      next.setField(sPID + "PatientIdentifierList[0].IDNumber",PASIDNumber);
      next.setField(sPID + "PatientIdentifierList[0].AssigningAuthority.NamespaceID", "MRN");
      next.setField(sPID + "PatientIdentifierList[0].IdentifierTypeCode", '');
      //next.setField(sPID + "PatientIdentifierList[0].AssigningAuthority.NamespaceID",PASAA);
      //next.setField(sPID + "PatientIdentifierList[0].AssigningFacility.NamespaceID",PASAF);
            
      
      
            if (NHSIDNumber == null)
      {      
      next.setField(sPID + "AlternatePatientID[0].IDNumber",'');
      next.setField(sPID + "AlternatePatientID[0].AssigningAuthority.NamespaceID",'' );
      next.setField(sPID + "AlternatePatientID[0].IdentifierTypeCode", '');
      //next.setField(sPID + "AlternatePatientID[0].IdentifierTypeCode.NamespaceID",'');
      //next.setField(sPID + "AlternatePatientID[0].IdentifierTypeCode.NamespaceID",'');
      }
      else
      {
      next.setField(sPID + "AlternatePatientID[0].IDNumber",NHSIDNumber);
      next.setField(sPID + "AlternatePatientID[0].AssigningAuthority.NamespaceID", "NHS Number");
      next.setField(sPID + "AlternatePatientID[0].IdentifierTypeCode", '');
      //next.setField(sPID + "AlternatePatientID[0].IdentifierTypeCode.NamespaceID",'');
      //next.setField(sPID + "AlternatePatientID[0].IdentifierTypeCode.NamespaceID",'');
      }
      


 var ii = 1;
      while (ii <= i) {
          next.setField(sPID + "PatientIdentifierList[" + ii +"].IDNumber",'');
          next.setField(sPID + "PatientIdentifierList[" + ii +"].IdentifierTypeCode", '');
          next.setField(sPID + "PatientIdentifierList[" + ii +"].AssigningAuthority.NamespaceID", '');
          next.setField(sPID + "PatientIdentifierList[" + ii +"].AssigningFacility.NamespaceID", '');
          ii++;
      }
      

      
//**
//*End SWAP      NHS + PASID
//**
}
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.