Forum Discussion

nirupama's avatar
nirupama
New Contributor II
3 years ago
Solved

To fetch value from group by output based on different values of one field

Hello Community,

I want to fetch ESTVERSION value after performing the group by REQUESTNUM based on condition as below:
If all records having STATUS=‘ESTIMATED’ then max(ESTVERSION) else if anyone STATUS=‘ACCEPTED’ then that ESTVERSION must be fetched. Tried with router, but not able to satisfy both the requirement.

Group_By Fields output0.json (619 Bytes)
required_output.json (260 Bytes)

  • Hi @nirupama , welcome!

    I believe the following expression in a mapper will do the job:

    $group.find(x=>x.STATUS == 'ACCEPTED') != null ? $group.find(x=>x.STATUS == 'ACCEPTED') : $group.sort((a,b)=> a.ESTVERSION - b.ESTVERSION).pop()

    Translated version would be:

    “If one object in the group has a “STATUS” field with “ACCEPTED” as value, than take that object. If not, than order the group on the “ESTVERSION” field, and take the last one, which means the max ESTVERSION.”

7 Replies

  • bojanvelevski's avatar
    bojanvelevski
    Valued Contributor

    Hi @nirupama , welcome!

    I believe the following expression in a mapper will do the job:

    $group.find(x=>x.STATUS == 'ACCEPTED') != null ? $group.find(x=>x.STATUS == 'ACCEPTED') : $group.sort((a,b)=> a.ESTVERSION - b.ESTVERSION).pop()

    Translated version would be:

    “If one object in the group has a “STATUS” field with “ACCEPTED” as value, than take that object. If not, than order the group on the “ESTVERSION” field, and take the last one, which means the max ESTVERSION.”

    • nirupama's avatar
      nirupama
      New Contributor II

      Thank you @bojanvelevski. It is working as expected. I tried with mapping expression you mentioned above by making few more changes in the input data and i’m able to fetch proper estversion.

      Thank you!!

      • nirupama's avatar
        nirupama
        New Contributor II

        Hi @bojanvelevski What would be the mapping expression "if two or more records in the group has a “STATUS” field with “ACCEPTED” as value and it has to fetch maximum of ESTVERSION among both. And second condition remains same, all are ESTIMATED”.

  • Welcome to the Community @nirupama!

    Please find a sample pipeline attached which should meet your current requirements.
    Community 13539 - Fetch value from group_2022_10_03.slp (4.1 KB)

    The Mapper snap is using the Array.sort() method to prioritize the group elements then taking the highest priority (element 0). Note that this might be an incomplete answer since sorting ESTVERSION is using lexicographic sorting but you may need to sort this value as integers - if you need this, take a look at the built-in parseInt() function.

    Hope this helps!

    • nirupama's avatar
      nirupama
      New Contributor II

      Thank you @koryknick for the quick response with the guidance. It is working when any one record is ACCEPTED but it is not when all records are ESTIMATED.