Announcement

Collapse
No announcement yet.

Repeating field blank or not blank conditions

Collapse
X
 
  • Filter
  • Time
  • Show
Clear All
new posts

  • Repeating field blank or not blank conditions

    Hi all,

    Trying to fix someone's old code and running into an issue on how to fix what they had going on.

    Here's an example of the segments:

    We need to trigger code when CON.3.1 = "Success" & CON.11.2 = "1".

    CON|1||RandomValue|||||||
    CON|2||Success||||||||10^1| (this is the segment we want to trigger the code)


    We want to skip CON|1 since it doesn't match the criteria. we want the code to trigger for CON|2 since it does match.

    What I'm seeing is that the transformer gets caught on CON|1 and says it cannot read property CON.11.2 from undefined. Best I can tell, this references the CON|1 and that's because there is no CON.11.2 value present.



    I've tried a few various things but nothing changed the error message. I know I have to change how it loops or references the CON.11.2, but I can't yet figure out how to tie it all together.



    Code:
          for (var y = 0; y < msg['CON'].length(); y++)
    
                                    if ( msg['CON']['CON.3'][y]['CON.3.1'].toString() == 'Success'  &&  msg['CON']['CON.11'][y]['CON.11.2'].toString() == '1')
    
    
    {
    
                                                    if (msg['CON']['CON.11'][y]['CON.11.1'].toString() ==  "10")
    
                                                    {
    
                                                                    Consent = "T";
    
                                                    }             
    
                                    }

  • #2
    We need to trigger code when CON.3.1 = "Success" & CON.11.2 = "1".

    CON|1||RandomValue|||||||
    CON|2||Success||||||||10^1| (this is the segment we want to trigger the code)

    for each(con in msg['CON'])
    {
    if(con['CON.1']['CON.1.1'].toString() == 'Success' && con['CON.11']['CON.11.2'].toString() == '1')
    {
    code here
    }
    }


    Actually, I would get rid of the internal if and do this...


    for each(con in msg['CON'])
    {
    if(con['CON.1']['CON.1.1'].toString() == 'Success' && con['CON.11']['CON.11.2'].toString() == '1' && con['CON.10']['CON.10.1'].toString() == '10')
    {
    code here
    }
    }
    Last edited by cory_cole; 01-22-2020, 05:49 AM.

    Comment


    • #3
      Originally posted by cory_cole View Post
      We need to trigger code when CON.3.1 = "Success" & CON.11.2 = "1".

      CON|1||RandomValue|||||||
      CON|2||Success||||||||10^1| (this is the segment we want to trigger the code)

      for each(con in msg['CON'])
      {
      if(con['CON.1']['CON.1.1'].toString() == 'Success' && con['CON.11']['CON.11.2'].toString() == '1')
      {
      code here
      }
      }


      Actually, I would get rid of the internal if and do this...


      for each(con in msg['CON'])
      {
      if(con['CON.1']['CON.1.1'].toString() == 'Success' && con['CON.11']['CON.11.2'].toString() == '1' && con['CON.10']['CON.10.1'].toString() == '10')
      {
      code here
      }
      }
      Aha! So my problem the Y variable. I tried adding the FOR EACH statement but left the Y. I guess the for each does everything I was looking for, and the var Y was unnecessary.

      Thank you!

      Comment


      • #4
        The Y is necessary if you are are doing the count. I find that the for each is easier to work with.

        Comment


        • #5
          Originally posted by cory_cole View Post
          The Y is necessary if you are are doing the count. I find that the for each is easier to work with.

          Could you help me fully understand the difference?

          For Each = checks conditions in all appropriate/present segments/fields?
          y variable = counts all (assumes they will have a present value?)?

          Comment


          • #6
            I prefer Cory's solution here, too (be careful with copy/paste. It looks like he accidentally switched CON.11.1 to CON.10.1.) Your original problem is that the [y] is in the wrong place.

            Since not every CON segment (in your example) has a CON.11, you get the error when y is equal to or greater than the number of CON segments with a CON.11.

            Short version:
            Code:
            // this means "give me the CON.3.1 value of the yth CON.3 in the message"
            msg['CON']['CON.3'][y]['CON.3.1'].toString()
            // this means "give me the CON.3.1 value of the yth CON in the message"
            msg['CON'][y]['CON.3']['CON.3.1'].toString()
            Detailed version:

            When you try to access child elements by name it actually returns a list, which can contain 0 or more nodes. When you access child elements by name of a list containing 1 or more elements, it returns all of the matching children for all of the list elements in a single list. When you access child elements by name of an empty list it returns an empty list.

            When you call toString on the list it behaves differently depending on the size of the list. If the list is empty it returns an empty string. If the list is a single element, it returns the toString value of that single element. If the list has more than 1, it returns the xml values of all the elements in the list.

            When you access a list by index, it will return undefined if the index is higher than the number of elements in the list.

            So, based on your example message of
            Code:
            CON|1||RandomValue|||||||
            CON|2||Success||||||||10^1|
            where the first CON segment only has 10 fields present,

            Code:
            msg['CON'][0]['CON.11']['CON.11.2'].toString()
            // msg['CON'][0] is the first CON segment
            // msg['CON'][0]['CON.11'] is an empty list
            // msg['CON'][0]['CON.11']['CON.11.2'] is an empty list
            // msg['CON'][0]['CON.11']['CON.11.2'].toString() is an empty string
            
            msg['CON'][1]['CON.11']['CON.11.2'].toString()
            // msg['CON'][1] is the second CON segment
            // msg['CON'][1]['CON.11'] is a single element list containing a child element of the second CON segment
            // msg['CON'][1]['CON.11']['CON.11.2'] is a single element list containing a descendant of the second CON segment
            // msg['CON'][1]['CON.11']['CON.11.2'].toString() is "1"
            
            msg['CON']['CON.11'][0]['CON.11.2'].toString()
            // msg['CON'] is a list of all of the CON segments
            // msg['CON']['CON.11'] is a list of all of the CON.11 elements that are children of all of the CON segments
            // msg['CON']['CON.11'][0] is the CON.11 element that is a child of the SECOND CON segment, because it is the first one from the previous list
            // msg['CON']['CON.11'][0]['CON.11.2'].toString() is "1". Not what you intended.
            
            msg['CON']['CON.11'][1]['CON.11.2'].toString()
            // msg['CON'] is a list of all of the CON segments
            // msg['CON']['CON.11'] is a list of all of the CON.11 elements that are children of all of the CON segments
            // msg['CON']['CON.11'][1] is undefined because there is only 1 in the list
            // msg['CON']['CON.11'][1]['CON.11.2'] throws an exception
            If there was another | at the end of the first CON segment your code would actually produce the correct results because every CON segment would have a CON.11 field defined, though the implementation would still not technically be correct.

            Comment


            • #7
              Originally posted by agermano View Post
              I prefer Cory's solution here, too (be careful with copy/paste. It looks like he accidentally switched CON.11.1 to CON.10.1.) Your original problem is that the [y] is in the wrong place.
              I did catch the typo

              And thank you for that tremendous explanation! That was incredibly helpful.

              Comment

              Working...
              X