Monday 6 August 2018

Pega Programming to calculate Sum the odd and even numbers, respectively, from 1 to a given upperbound.

Sum the odd and even numbers, respectively, from 1 to a given upperbound


Hi Friends, I try to implement one simple program "Sum the odd and even numbers, respectively, from 1 to a given upperbound. "



Lets see how to implement this in Pega.

I created one flow 

In the Section I get the UpperBound Value using one text box. 

In the action tab of property control I added three action on Value change in text box.


Post Value - to post this value to clipboard.
Run Activity- to run the activity to acheive our functionalities
Refresh-This Section -  to display the output in below 2 text boxes(Not yet created , I will create later). On refresh only we can access the clipboard page property. So we refresh this section.

In Run Activity I called one activity named CalculateUpperBoundValue.



Lets see how to acheive this logic using activity.


This is the activity I created.


In first step I used Page-New , this step is to hold all the values while we process the data. I gave page name as PageOne , after we give here the Step Page name we should give page and class name in Pages and class tab.


Next Page-Copy, In this step I copied all the values from pyWorkPage to PageOne

U can ask , I am not gave pyWorkPage in any place, The answer is if  we are not give any page name in CopyFrom then in compile time it takes the page of previous step.

We can test this with tracer. I comment all the steps below 2 and see what happend.
You can see In second step the step page is pyWorkPage so automatically in PageFrom it takes pyWorkPage.

We are copying all the value in pyWorkPage to PageOne.

In Step 3 , I added Property-Set to declare properties with default value.

Here U can see I used Local Variable, if we used Local Variable we can use this value in all this places in this activity not outside this activity.

In the fourth step, I used Loop to achieve the while loop in above program.

I added a child for 4 th step to add all the even numbers and odd numbers. In 4.1 I added a when condition to check whether the current value is odd or even number.



(<CURRENT>) - it is similar to i in for loop
On first iteration, (<Current>) value will be 1 in sencond iteration (<Current>) value will be 2 and in 3rd iteration (<Current>) value will be 3 ans so on.

If the current value is Even means we no need to execute 4.2(Used for odd value calculation). So In the end of 4.1 , I wrote condition to jump to Step named SKIP  by using Always(Built in) when condition.

In 4.2 I added a property set to calculate odd value sum.


In 4.3 we add a step , in that step I added a precondition , there we use Always to  exit the Iteration.


In the last step I added one Page-Copy to copy all the manipulated data in PageOne to pyWorkPage.

To see the result,  We refer the SumEven property and SumOdd property from clipboard to Section.

I drag and drop one dynamic layout.
then refer that two property in this dynamic layout.

I refered those two property and make them read only.

Now our UI look like this,

We no need to see the SumEven and SumOdd if there is no value in UpperBoundValue, so we can add Visibility for that dynamic layout.

Now once again , run the flow or refresh .

 Afer I gave 10, result will be.


SumEven = 2+4+6+8+10 =30
SumOdd = 1+3+5+7+9 =25

If u have any doubt ask in comment :)

9 comments:

  1. Hi Bharat,
    I implemented the same scenarios but my out put is coming like
    EvenSum:02468101214
    OddSum:0123456789111121314

    ReplyDelete
    Replies
    1. Hi Property we are using should be of type Integer, If we used string property then it will assume the values as string value. So value will not add it will append.

      Delete
    2. Thanks bro
      after creating the property as integer its working as expected.

      Delete
  2. What is the 4th step of Property-set method
    Property name i can see local.Flag and what is the Property value field

    ReplyDelete
    Replies
    1. Sorry, We are not using the Local.Flag anywhere in our activity, So we can set that to null value ( "" ). It has no use.

      Unfortunately I not have this code in my system now.

      Delete
  3. What is the 4.1 step of Jump step

    ReplyDelete
    Replies
    1. 4.1 Transition when condition is always jump to later step SKIP

      Delete
  4. Brilliant One mate :)

    ReplyDelete