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.”