Mirth: extracting values from the msg (ex4) object w/o thowing errors when the object is not populated.

sidwelle
sidwelle used Ask the Experts™
on
Looking to write a function to query an ex4 object for a value. My problem is that when I try to extract the value and the field has not been populated, JavaScript throws an error.
I can write a try/catch statement to trap the errors, but when I try to extract many values the code starts to look clumsy.

Is there some way to pass the ex4 object to a function w/o the function throwing an error when passing a ex4.location that has not been populated ?

Example:
var msh7 = msg['MSH']['MSH.7']['MSH.7.1'].toString();    // <- this throws an error if MSH|07 has not been populated. (they didn't send a date)

Why can't I do something like this ?:
var msh7 = getValue(msg['MSH']['MSH.7']['MSH.7.1']);    //  <- but this also throws an error if the location is not populated !?

function getValue(obj){
   try{
      return obj.toString();
   }catch(e){ return '';}   // just return an empty str.
}
Comment
Watch Question

Do more with

Expert Office
EXPERT OFFICE® is a registered trademark of EXPERTS EXCHANGE®

Do more with

Expert Office
Submit tech questions to Ask the Experts™ at any time to receive solutions, advice, and new ideas from leading industry professionals.

Start 7-Day Free Trial