rania ben salem
rania ben salem

Reputation: 161

Get SUM from Iterator mediator esb wso2

I used Iterator and Script Mediator to calculate the sum of a student's grades.

I saw this problem i received each time the sum of 0 and noteis your say the totalnote does not take the variable note and increment For example if I put the value of totalnote =16 it is the sum of 16 and note of each row

My goal is to add up these 4 notes

Here is my code

            <property name="totalnote" scope="default" type="INTEGER" value="0"/>
            <iterate expression="//etudiants/etudiant">
                <target>
                    <sequence>
                        <property expression="json-eval($.etudiant.note)" name="note" scope="default" type="INTEGER"/>
                        <log level="custom">
                            <property expression="get-property('note')" name="msg"/>
                        </log>
                        <script language="js"><![CDATA[{var i = parseInt(mc.getProperty("note")) ;
var totalnote = parseInt(mc.getProperty("totalnote")) ;         
var totalnote = totalnote + i;
totalnote=totalnote.toString();
mc.setProperty("totalnote", totalnote);}]]></script>
                        <log level="custom">
                            <property expression="get-property('totalnote')" name="ms"/>
                        </log>
                    </sequence>
                </target>
            </iterate>
            <respond/>
        </inSequence>
        <outSequence/>
        <faultSequence/>
    </resource>
</api>

and the result I received:

[2022-12-01 10:04:40,450]  INFO {LogMediator} - {api:student} msg = 13
[2022-12-01 10:04:40,450]  INFO {LogMediator} - {api:student} msg = 15
[2022-12-01 10:04:40,450]  INFO {LogMediator} - {api:student} msg = 16
[2022-12-01 10:04:40,450]  INFO {LogMediator} - {api:student} msg = 17
[2022-12-01 10:04:40,469]  INFO {LogMediator} - {api:student} ms = 17
[2022-12-01 10:04:40,469]  INFO {LogMediator} - {api:student} ms = 13
[2022-12-01 10:04:40,469]  INFO {LogMediator} - {api:student} ms = 15
[2022-12-01 10:04:40,469]  INFO {LogMediator} - {api:student} ms = 16

Upvotes: 5

Views: 184

Answers (1)

sanoJ
sanoJ

Reputation: 3128

If you just need to get the sum of the note for all the students, you can simply use the following XPath expression,

<property expression="sum(//etudiants/etudiant/note[number(.) = number(.)])" name="totalnote" scope="default" type="STRING"/>

Depeding on your payload you may need to update the expression. Here I have assumed a payload like below,

{
  "etudiants": {
    "etudiant": [
      {
        "note": 17
      },
      {
        "note": 13
      }
    ]
  }
}

You should use the iterate mediator if you want to make back-end calls from each iteration. Here for each iteration, a new message is created and you need to collect them by an Aggregate mediator.

If you just want to iterate over a payload you can use the ForEach Mediator. ForEach Mediator supports modifying the original payload as well.

For more info: https://apim.docs.wso2.com/en/latest/reference/mediators/iterate-mediator/

Upvotes: 5

Related Questions