3 Replies Latest reply: Sep 11, 2013 1:02 PM by ti3r RSS

    Trouble adding if condition

    ti3r

      I have a form in Application Express.  The user selects a campaign idea in P68_CAMPAIGN_ID (select list with two options 'HA' and 'TC')  then enters a quantity in P68_QUANTITY (text field). the form also has a field for each day of week (Monday through Friday). 

       

      I have written javascript (shown below) to divide the quantity value and place 1/5 in each day field.  So if 20000 Quantity entered Monday shows 4000, Tuesday 4000, Wednesday 4000 Thursday 4000 and Friday 4000.  But what I need is for that autopopulation of the day fields to only occur if 'HA' campaign type is selected.  I thought it would be easy by simply adding an if statement to the javascript, but nothing worked as I expected.  Here is the code that now autopopulates the field when the user leaves the quantity field.  I think I just need to add if ($x(P68_CAMPAIGN_ID).value = 'HA')  and then run the function  else not run the function - but no placement of this if clause worked.  The autopopulation of the fields stopped working regardless of what was selected.

       

      Can anyone help?

       

      <script language="JavaScript" type="text/javascript">

         function getDAYS()

      {

          function getVal(item){

         if($x(item).value != "")

           return parseFloat($x(item).value);

         else

           return 0;

          }

      $x('P68_MONDAY').value =

        getVal('P68_QUANTITY') / 5;

      $x('P68_TUESDAY').value =

        getVal('P68_QUANTITY') / 5;

      $x('P68_WEDNESDAY').value =

        getVal('P68_QUANTITY') / 5;

      $x('P68_THURSDAY').value =

        getVal('P68_QUANTITY') / 5;

      $x('P68_FRIDAY').value =

        getVal('P68_QUANTITY') / 5;

      }

      </script>

        • 1. Re: Trouble adding if condition
          Jeff E

          Might only be a syntax issue.  Don't forget the quotes around the item name and use two equals for the comparision:

          if ($x('P68_CAMPAIGN_ID').value == 'HA') 

           

          If you are on APEX version 4.0+ you could create dynamic actions in place of the javaScript code.

           

          Also for getting an apex item value I like to use the $v(<item_name>) function and for setting an APEX item value I like to use the $s(<item_name>,<value>) function.

           

          Your function could be like this:

           

           function getDAYS()
          {
          if ($v('P68_CAMPAIGN_ID') == 'HA') {
            $s('P68_MONDAY',($v('P68_QUANTITY') / 5));
            $s('P68_TUESDAY',($v('P68_QUANTITY') / 5));
            $s('P68_WEDNESDAY',($v('P68_QUANTITY') / 5));
            $s('P68_THURSDAY',($v('P68_QUANTITY') / 5));
            $s('P68_FRIDAY',($v('P68_QUANTITY') / 5));
            }
          }
          
          • 2. Re: Trouble adding if condition
            ti3r

            Thanks for that simplified code  - it is great, but this still does not work with the 'if' statement.  P68_CAMPAIGN_ID is a select list - does that make a difference?  The code works fine until I enter the if statement.  I have checked using both the display value and return value of campaign_id - neither works.

            • 3. Re: Trouble adding if condition
              ti3r

              Well now it is working and I am not sure what small thing I changed!  But thanks so much for the response.  I learned a lot from your simplified code!