on 2024 Dec 05 11:31 AM
Hi Experts,
I have a requirement to sort the EmpJob nodes based on start dates only when there is sinlge EmpEmployment node and jobInfoNav has multiple EmpJob nodes which contains emplStatusNav/PicklistOption/localeLabel is Active in the ascending order. Eg. I am expecting EmpJob/startDate = 2024-11-01T00:00:00.000 in the first node and then EmpJob/startDate = 2025-01-01T00:00:00.000 in the second node. The payload contains multiple records, if these conditions are not satisfied there should not be any sorting done to nodes.
I have tried with the below-mentioned code but it is not working.
Any help or suggestions would be highly appreciated.
import com.sap.gateway.ip.core.customdev.util.Message;
import java.util.HashMap;
import groovy.xml.XmlUtil;
import groovy.util.NodeList
import groovy.util.XmlParser;
import groovy.xml.MarkupBuilder;
import java.util.Date;
import java.text.SimpleDateFormat;
import java.util.Calendar;
def Message processData(Message message) {
def body = message.getBody(java.lang.String);
def payload = new XmlParser().parseText(body);
payload.PerPerson.each{rec ->
def countofEmpEmployment = rec.employmentNav.EmpEmployment.size()
if (countofEmpEmployment <= 1){
def countofEmpJob = rec.employmentNav.EmpEmployment.jobInfoNav.EmpJob.size();
def EmpJobStatus = rec.employmentNav.EmpEmployment.jobInfoNav.EmpJob.emplStatusNav.PicklistOption.localeLabel.text()
if(EmpJobStatus.contains("ActiveActive")){
rec.value = rec.employmentNav.EmpEmployment.jobInfoNav.EmpJob.sort{sd ->
sd.startDate.text()}
}
}
}
message.setBody(XmlUtil.serialize(payload));
return message;
}Regards,
Pavan
Request clarification before answering.
Sorting xml with Groovy is quite well explained in the answer to this stack overflow question. Adjusting this to your requirement gives the following Groovy script:
import com.sap.gateway.ip.core.customdev.util.Message
import groovy.xml.*
Message processData(Message message) {
def body = message.getBody(Reader)
def PerPerson = new XmlParser().parse(body)
PerPerson.PerPerson.each { person ->
def EmpEmployment = person.employmentNav.EmpEmployment
if (EmpEmployment.size() == 1) {
def areAllJobsActive = EmpEmployment.jobInfoNav.EmpJob.every { job -> job.emplStatusNav.PicklistOption.localeLabel.text() == 'Active' }
if (areAllJobsActive) {
EmpEmployment.jobInfoNav[0].children().sort(true) { job -> job.startDate.text() }
}
}
}
message.setBody(XmlUtil.serialize(PerPerson))
return message
}
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
| User | Count |
|---|---|
| 18 | |
| 6 | |
| 6 | |
| 6 | |
| 4 | |
| 3 | |
| 3 | |
| 2 | |
| 2 | |
| 2 |
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.