Skip to Content
0

Code to Generate seq number using message ID in SAP PI

Aug 18, 2017 at 06:56 AM

234

avatar image
Former Member

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&overridelayout=true.

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

10 |10000 characters needed characters left characters exceeded
* Please Login or Register to Answer, Follow or Comment.

3 Answers

Raghuraman S Aug 18, 2017 at 07:57 AM
0

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.

Show 3 Share
10 |10000 characters needed characters left characters exceeded
Former Member

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

0
0
Former Member

Hi RaghuRam,

I have followed the same blog.

0
Evgeniy Kolmakov Aug 18, 2017 at 11:35 AM
0

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.

Share
10 |10000 characters needed characters left characters exceeded
Manoj K Aug 20, 2017 at 06:17 PM
0

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

Share
10 |10000 characters needed characters left characters exceeded