cancel
Showing results for 
Search instead for 
Did you mean: 

Code to Generate seq number using message ID in SAP PI

sahithi_moparthi
Contributor
0 Kudos

Hi Gurus,

We need your help for the below requirement.We have a scenario IDOC to File.For each IDOC it should create the File with the seq no.

I have achieved the scenario using Java mapping as mentioned in the below blog

http://www.sdn.sap.com/irj/scn/index?rid=/library/uuid/503e9bea-ea09-2e10-ec8a-cdf369de99d3&override....

But i am facing an issue when the message stuck in SAP PI.Suppose at the target the latest file seq no is 12,and 13 seq no msg got stuck in SAP PI.When we reprocessed the message the seq no generated is 14 instead of 13.

I have checked the below link,but the code was missing.

https://blogs.sap.com/2014/06/30/generating-incremental-sequence-number-in-pi-per-message-id/

Could any one please provide code how to achieve the required result.

Regards,

Sahithi

sahithi_moparthi
Contributor
0 Kudos

Hi Manoj,

Thanks for your response.

  • We are on PI 7.31.We don't have B2B add on in our system.
  • RFC Lookup: If we use RFC lookup can't we face the same issue.Suppose the seqno generated from RFC lookup is 12 and if the message failed in SAP PI,then if we reprocess the message then again RFC calls happens and the seqno generated will be 14 instead of 13. How can we overcome this situation? how we can send the missing seq number.
former_member190293
Active Contributor
0 Kudos

Hi Manoj!

I guess issue described above is caused by incorrect path assigning for target file. If you place it in any common folder you hardly face such kind of problem.

And another thought: for high-load scenario with huge amount of documents processed with a time flow I'm just wondering how fast the table on ECC side grows and how it impacts the overall processing time. And if we have to add another counter for any other system, I think, it's not the best idea to use ECC system as the "common counters keeper": if any connectivity issue happens - all other processes using this counter functionality will stop working as well.

Regards, Evgeniy.

Accepted Solutions (0)

Answers (3)

Answers (3)

manoj_khavatkopp
Active Contributor
0 Kudos

Sahithi,

You may use the below code to generate counter . Reprocessing of same message is also taken care where counter doesn't get incremented.

And for avoiding issue with multi instance in PI/PO system i am using a common folder . You may change the folder if required but make sure it is common for all instance in your PI system.

1.Compile the below code in required JDK version and import into your namespace via Imported Archive.

2.Add this archive into your mapping and also add import statement.

3.Call it via UDF via 2 inputs : a.) Messsge ID b.)Some constant (Add your mapping name as constant)

In UDF call as :

return SetCounter.MsgID(msgid,Const);

Complete Code :

package com.sap.mapping.java;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.util.*;
public class SetCounter 
{
public static void main(String[] args) 
{
	String msgID= "123456";
	String Const= "Map2";
	System.out.println(SetCounter.MsgID(msgID,Const));
}
public static String MsgID(String msgid,String Const)
{
String U_Counter="";
Properties properties = new Properties();
File propertiesfile = new File("D:/usr/sap/MsgId.txt");
try
{
propertiesfile.createNewFile();
properties.load(new FileInputStream(propertiesfile));
}
catch (IOException e)
{
e.printStackTrace();
return "Error:File not read";
}
String number = properties.getProperty(msgid);
if (number == null)
{
number = "0";
String counter = String.valueOf(Integer.parseInt(number) + 1);
properties.setProperty(msgid, counter);
U_Counter= getCounter(Const,"ADD");
}
U_Counter= getCounter(Const,"GET");
try
{
properties.store(new FileOutputStream(propertiesfile), null);
} catch (IOException e) {
return "Error:File not updated";
}
return U_Counter;
}
public static String getCounter(String Const , String opr)
{
String counter = "";
Properties properties = new Properties();
File propertiesfile = new File("D:/usr/sap/Counter.txt");
try
{
propertiesfile.createNewFile();
properties.load(new FileInputStream(propertiesfile));
}
catch (IOException e)
{
e.printStackTrace();
return "Error:File not read";
}
String number = properties.getProperty(Const);
if (number == null)
{
number = "0";
counter = String.valueOf(Integer.parseInt(number) + 1);
properties.setProperty(Const, counter);
}
else
{
	if(opr.equalsIgnoreCase("ADD"))
	{
		counter = String.valueOf(Integer.parseInt(number) + 1);
		properties.setProperty(Const, counter);		
	}
	else
	{
		counter = number;
	}
}
try
{
properties.store(new FileOutputStream(propertiesfile), null);
} catch (IOException e) {
return "Error:File not updated";
}
return counter;
}
}

Br,

Manoj

former_member190293
Active Contributor
0 Kudos

Hi Sahithi!

Just at first sight: I would try to implement Adapter modules sequense consisting of two parts: first part is intended to read the current counter value from properties file and store it to Dynamic Configuration, the second, placed after standard adapter module - increments the counter. Thus, if the message is successfully sent to messaging system we increment the counter and the current counter is stored within the message, so, if the message is failed at any further step, we can resend it with the same counter. If the message fails at adapter processing level - it's no need to assign any counter for it since it hasn't entered the messaging system.

Regards, Evgeniy.

former_member186851
Active Contributor
0 Kudos

Hello Sahithi,

The UDF codes are in the Screenshots in the same link,but I would suggest Its better to sequence number in IDOC(ABAP side) and map it in PI.

sahithi_moparthi
Contributor
0 Kudos

Hi Raghuram,

I need the code for java mapping.In UDF it was mentioned they had used 2 methods one for msgID and other is seqnumber.

I could not see any java code in the attachment section(getSeqMsgID).

Regards,

Sahithi M

former_member186851
Active Contributor
sahithi_moparthi
Contributor
0 Kudos

Hi RaghuRam,

I have followed the same blog.