1 2 Previous Next 18 Replies Latest reply: Apr 18, 2012 6:25 AM by karenpeacock RSS

    Return text value if Boolean is true or false

    hhmis Apprentice

      Greetings,

       

      I'm trying to come up with a calculation for a stirng attribute that will detect if a boolean is true or false, then depending on which, it will return a text value of "yes" or "no". I think I'm close, but seem to be struggling getting it right. I'm fairly new to calculations, so don't laugh to hard at my attempt :

       

      import System

      static def GetAttributeValue(Request):

      if Request._SystemAccessECW == 'True'
      Value= "Yes"
      return Value
      if Request._SystemAccessECW == 'False'
      Value= "No"

       

       

      Thanks in adavance

        • 1. Re: Return text value if Boolean is true or false
          elizabethcombrink Employee

          Try this - make sure you keep the spaces as I've got them

           

          import System
          static def GetAttributeValue(Request):
          Value = "Yes"
          if Request._SystemAccessECW == 'True':
            Value= "Yes"
          else:
            Value = "No"
          return Value

           

           

          In Stu's guide to calculations, he mentions the Boo Primer which helps loads to get the syntax right!

          • 2. Re: Return text value if Boolean is true or false
            hhmis Apprentice

            Hi Elizabeth,

             

            No dice. I got this:

             

            CalcError1.JPG

             

            I then added a space to line 3 of your suggestion (in front of Value), and then got this:

             

            CalcError2.JPG

            • 3. Re: Return text value if Boolean is true or false
              elizabethcombrink Employee

              1 space needed in lines 3, 4 and 6, and 2 spaces for line 5 and 7.

               

              Home time for me.

              • 4. Re: Return text value if Boolean is true or false
                hhmis Apprentice

                The calculation seems to be working.... I added the spaces as suggested, but got the following:

                 

                CalcError3.JPG

                 

                I then added 2 spaces to line 8 and the syntax is now valid. However...I have another problem now:

                 

                I added the attrinute to my window I'm using this on and tested it out. Here's my results:

                 

                • When the boolean is unchecked, the attribute returns the value "No". This is good
                • When the boolean is checked, it returns the value "No". I need it to say yes.

                 

                Any more suggestions?? Here's the calculation so far (thanks Elizabeth. You rock):

                 

                 

                import System
                static def GetAttributeValue(Request):
                Value = "Yes"
                if Request._SystemAccessECW == 'True':
                  Value= "Yes"
                else:
                  Value = "No"
                  return Value
                • 5. Re: Return text value if Boolean is true or false
                  Expert

                  Hi

                   

                  It could just be how I am looking at it, but it looks like the last line is indented the same as the line above it.  Won't this mean that the only time that the "return" statement runs is when the "else" section is fired.  I think you may need to remove a space from the last line.

                   

                  Cheers


                  Paul

                  • 6. Re: Return text value if Boolean is true or false
                    Expert

                    To test for a boolean value, don't quote the True or False in your check, see below-

                     


                    import System

                    static def GetAttributeValue(Request):

                    if Request._SystemAccessECW == true

                    Value= "Yes"

                    else:

                    Value= "No"

                    return Value

                     

                    Cheers,

                    Hadyn

                    • 7. Re: Return text value if Boolean is true or false
                      hhmis Apprentice

                      @ Paul: I did remove the extra space on the last line and syntax was still valid. It did not help to get the results I'm looking for though.

                      @ Hadyn:I removed the quotes around True and got an unknown identifier error.

                       

                      Thanks to both of you for the suggestions. I was reading up on Boo Primer as Elizabeth suggested and thought maybe I'm in need of an "if-else" statement somewhere in there. After several attemps, its still a no go.

                      • 8. Re: Return text value if Boolean is true or false
                        Expert

                        Did you use a capital T in True? I think this is a case sensitive item, it either needs to be capitalised or not, can't remember sorry, so if it is capitalised then try without a capital T.

                         

                        Cheers,

                        Hadyn

                         

                        P.S. that calculation does contain an if-then-else statement already.

                        • 9. Re: Return text value if Boolean is true or false
                          csimpson@kumc.edu Expert

                          Somewhere I saw it done this way and it works for me ...

                           

                          if Request._SystemAccessECW.ToString() == 'True'

                           

                          I usually see everything after the Static line is indented 1 space.  Anything to be completed within an IF statement gets another indent.  Your example would end up with:

                          import line, no spaces

                          static line, no spaces

                          if line, 1 space

                          value line, 2 spaces

                          else line, 1 space

                          value line, 2 spaces

                          return line, 1 space

                           

                          If you have 2 spaces before the return value, then it will only return a value in the else situation and the true situation will never return a value.

                           

                          Of course nested if statements could require additional spaces and it strts getting messy. 

                           

                          I have an enhancement request for a BOO debug tool so we can step line by line through a calculation and see the values.  Visual Studio does an excellent job of tracing and would be a good example of how I would like to see it work.

                          • 10. Re: Return text value if Boolean is true or false
                            hhmis Apprentice

                            @ Hadyn: I changed to lowercase t and removed the quotes. Syntax is valid. Like I said, I'm a noob with this stuff...lol. Still a no go though.

                            @ Carl: I added your suggestion and checked spacing again. Syntax is valid, but it has not helped.

                             

                            I just don't see how this would not work correctly, unless it has nothing to do with the calculation and its not returning the "yes" value because of something else involved?

                             

                            Ok, to recap, here's my current calculation:

                             

                            import System

                            static def GetAttributeValue(Request):

                            if Request._SystemAccessECW.ToString() == true:

                              Value= "Yes"

                            else:

                              Value = "No"

                            return Value

                             

                             

                            And the goal of all this is:

                             

                            • When the boolean is unchecked, the attribute returns the value "No".  <---currently works
                            • When the boolean is checked, it returns the value "Yes".                   <---does not work
                            • 11. Re: Return text value if Boolean is true or false
                              csimpson@kumc.edu Expert

                              From the looks of it (hard to tell) it appears that everything below the Static line is not indented, all of my calculations are indented 1 space.  At the end of the line is a ":" which means the next section should be indented.  Notice that after the IF statement, the value is indented 2 spaces.  The ELSE is not needed because the 'No' is assumed.

                               

                              The other thing we don't know is if there is a default value.  If there is no default value then a third option exists (no choice has been made).  I recommend making True or False a default value.  I also recommend making it a mandatory value so it shows up on WebAccess windows as a check box and not a drop down value.  Might as well do both.

                               

                              I also don't see that Value has been initialized to a string so it might be expecting an integer.  This is how I would do it:

                               

                              import System
                              static def GetAttributeValue(Request):
                               Value = 'No'
                               if Request._SystemAccessECW.ToString() == 'True':
                                Value= 'Yes'
                               return Value
                              

                               

                              The courier font helps show the spacing a little bit better.

                               

                              The ToString returns a string and not a boolean value so you need to check it against a string.

                              • 12. Re: Return text value if Boolean is true or false
                                hhmis Apprentice

                                Carl,

                                 

                                This is awesome. It worked like a charm. Kudos to you sir. I'll take your advice about the webaccess part as well. Also, I do agree with you about having a debug tool. I think it would be very helpful. The errors when checking the syntax now just seem criptic and not too helpful. Personally, I'd like to see LD relase a guide on calculations that would include tons of examples (wishful thinking).

                                 

                                Thanks to all who contirbuted on this discussion.

                                 

                                Cheers

                                • 13. Re: Return text value if Boolean is true or false
                                  csimpson@kumc.edu Expert

                                  Agreed, a manual with LOTS of examples would really be nice.  Part of the problem is they don't support BOO.  It's in the product, as you can see here many people use it, but support doesn't officially support it.  These forums are the basic support mechanism for BOO and we keep passing around examples.  I am half tempted to start my own manual but there is still so much I don't know.

                                   

                                  On the positive side it does some amazing stuff and really makes Service Desk a much more powerful package.

                                  • 14. Re: Return text value if Boolean is true or false
                                    Expert

                                    Carl,

                                         Can you post up the link for the enhancement request, I would vote for a debug tool definitely. Also, I believe LANDesk does support the calculation feature, and any OOTB calculations but what they don't support is custom calculations.

                                     

                                    As it stands, you can do some debugging using tracing in the TPS.config, however this isn't always helpful. Also, for examples/info on writing BOO code I've found 3 things have really helped when developing calculations-

                                     

                                    • The BOO Primer and wider tutorial information found at http://www.boo.codehaus.org
                                    • Googling .NET functions and methods, as BOO can use a large portion of these functions, things like ToString() or Substring, plus various date time operators.
                                    • Googling Python syntax - while not explicitly a Python interpreter the BOO engine seems to use a near identical syntax and structure as Python code so I've found that where syntax and structure issues are letting me down I can usually google for some Python based examples to find the solution, as there is way more info on Python online than BOO.

                                     

                                    Cheers,

                                    Hadyn

                                    1 2 Previous Next