Monday 30 July 2018

Count row/ line from csv file in SAP HCI/CPI

We had a requirement to count the lines in CSV file and return the value at end of that payload.

For this, we had written a groovy script to count the lines and return it to store in property . By that we retrieved it in the content modifier.


Code:

import com.sap.gateway.ip.core.customdev.util.Message;
import java.util.HashMap;

def Message processData(Message message) {
def lineNo = 0;
def lines = message.getBody(java.lang.String) as String;
    lines.eachLine {
    lineNo++} 
message.setProperty("count1",lineNo);
return message;
}

Content Modifier:

Add this in body to print the count of the lines

${property.count1}



4 comments:

  1. Hi, I have my XML to CSV converter, after that I have to use the grovy code to count the lines?
    Can you please tell me where is my "table" located? how do I count.
    I am very lost, thanks.

    ReplyDelete
  2. Sorry for late reply. What is the table mean? After xml to csv converter, if you use this script, it will count the lines and give you the count as output.

    If still the issue not solved let me know in brief, So that I can help you. If you found it, please update. Thanks

    ReplyDelete
  3. thank you so much for posting this! I was suffering trying to count the csv lines inside the splitter with a content modifier.

    ReplyDelete
  4. Hi Loganathan , in the first go I am counting the No. of Records in the file, once the file is Posted in SF system I would like to know many are success & Failed records from the file after posting in Sf system..?

    ReplyDelete