cancel
Showing results for 
Search instead for 
Did you mean: 

Need a UDF to get date timestamp from ASMA parameters in sap pi 7.31

Former Member
0 Kudos

Hi,

I need to take the SourceFileTimestamp and pass it in the mail body as text.

I need this date to be in specific format which is - MM/dd/yyyy hh:mm:ss

The date is coming as -YYYYMMDDTHHMMSSZ from ASMA SourceFileTimestamp 

where T represents start of time and Z represents end of time.

My code is as below but it is giving error -

java.lang.StringIndexOutOfBoundsException: String index out of range: -2

try{ 

DynamicConfiguration conf = (DynamicConfiguration)
container.getTransformationParameters().get(StreamTransformationConstants.DYNAMIC_CONFIGURATION);

DynamicConfigurationKey key1 = DynamicConfigurationKey.create("_http://sap.com/xi/XI/System/File","FileName");

DynamicConfigurationKey key2 = DynamicConfigurationKey.create("_http://sap.com/xi/XI/System/File","SourceFileTimestamp");

String fname= conf.get(key1);

String timestamp= conf.get(key2);

String Str= timestamp;

String date= Str.substring(0, 8);

String year= date.substring(0, 4);
String month= date.substring(4, 2);
String day= date.substring(6, 2);

String time= Str.substring(9, 6);

String hours= time.substring(9, 2);
String mins= time.substring(11, 2);
String secs= time.substring(13, 2);

String date1= month+" / "+day+" / "+year;
String time1= hours+" : "+mins+" : "+secs;

String filename= "EBS File with file name " + fname + " is sucessfully received to SAP @ " +date1+"  "+time1;

return "Dear Sir / Madam," +" \n " +" \n "+filename;

}

catch(Exception e)

{

     String exception = e.toString();

      return exception;

}

Kindly correct this UDF.

Thanks

Neha

Accepted Solutions (0)

Answers (9)

Answers (9)

pvishnuvardan_reddy
Active Contributor
0 Kudos

Hi Neha,

I have corrected the udf, try this out:

try{

DynamicConfiguration conf = (DynamicConfiguration)

container.getTransformationParameters().get(StreamTransformationConstants.DYNAMIC_CONFIGURATION);

DynamicConfigurationKey key1 = DynamicConfigurationKey.create("_http://sap.com/xi/XI/System/File","FileName");

DynamicConfigurationKey key2 = DynamicConfigurationKey.create("_http://sap.com/xi/XI/System/File","SourceFileTimestamp");

String fname= conf.get(key1);

String timestamp= conf.get(key2);

String Str= timestamp;

String date= Str.substring(0, 8);

String year= date.substring(0, 4);

String month= date.substring(4, 6);

String day= date.substring(6);

String time= Str.substring(8);

String hours= time.substring(1,3);

String mins= time.substring(3,5);

String secs= time.substring(5,7);

String date1= month+" / "+day+" / "+year;

String time1= hours+" : "+mins+" : "+secs;

String filename= "EBS File with file name " + fname + " is sucessfully received to SAP @ " +date1+"  "+time1;

return "Dear Sir / Madam," +" \n " +" \n "+filename;

}

catch(Exception e)

{

     String exception = e.toString();

      return exception;

}

Regards

Vishnu

pvishnuvardan_reddy
Active Contributor
0 Kudos

Hi Neha,

Please try this code:

try{

DynamicConfiguration conf = (DynamicConfiguration)

container.getTransformationParameters().get(StreamTransformationConstants.DYNAMIC_CONFIGURATION);

DynamicConfigurationKey key1 = DynamicConfigurationKey.create("_http://sap.com/xi/XI/System/File","FileName");

DynamicConfigurationKey key2 = DynamicConfigurationKey.create("_http://sap.com/xi/XI/System/File","SourceFileTimestamp");

String fname= conf.get(key1);

String timestamp= conf.get(key2);

String Str= timestamp;

String date= Str.substring(0, 8);

String year= date.substring(0, 4);

String month= date.substring(4, 6);

String day= date.substring(6);

String time= Str.substring(8);

String hours= time.substring(9, 11);

String mins= time.substring(11, 13);

String secs= time.substring(13, 15);

String date1= month+" / "+day+" / "+year;

String time1= hours+" : "+mins+" : "+secs;

String filename= "EBS File with file name " + fname + " is sucessfully received to SAP @ " +date1+"  "+time1;

return "Dear Sir / Madam," +" \n " +" \n "+filename;

}

catch(Exception e)

{

     String exception = e.toString();

      return exception;

}

I just corrected in substring lines only in your code.

Regards

Vishnu

Former Member
0 Kudos

Hi Vishnu,

For timestamp too am getting error for below code;

I need time format as YYYYMMDD HH:MM:SS

public String GET_XMLtime(Container container){

DynamicConfiguration conf = (DynamicConfiguration)

container.getTransformationParameters().get(StreamTransformationConstants.DYNAMIC_CONFIGURATION);

DynamicConfigurationKey key = DynamicConfigurationKey.create("http://sap.com/xi/XI/System/File","SourceFileTimestamp");

String timestamp= conf.get(key);

String Str= timestamp;

String date= Str.substring(0, 8);

String year= date.substring(0, 4);

String month= date.substring(4, 6);

String day= date.substring(6);

String time= Str.substring(8);

String hours= time.substring(9, 11);

String mins= time.substring(11, 13);

String secs= time.substring(13, 15);

String date1= month+" / "+day+" / "+year;

String time1= hours+" : "+mins+" : "+secs;

return timestamp;

pvishnuvardan_reddy
Active Contributor
0 Kudos

Hi Rajesh,

I have corrected the udf, please try this:

DynamicConfiguration conf = (DynamicConfiguration)

container.getTransformationParameters().get(StreamTransformationConstants.DYNAMIC_CONFIGURATION);

DynamicConfigurationKey key = DynamicConfigurationKey.create("http://sap.com/xi/XI/System/File","SourceFileTimestamp");

String timestamp= conf.get(key);

String Str= timestamp;

String date= Str.substring(0, 8);

String year= date.substring(0, 4);

String month= date.substring(4, 6);

String day= date.substring(6);

String time= Str.substring(8,16);

String hours= time.substring(1,3);

String mins= time.substring(3,5);

String secs= time.substring(5,7);

String date1= year + month + day;

String time1= hours+":"+mins+":"+secs;

timestamp = date1 + " " + time1;

return timestamp;

Former Member
0 Kudos

Thanks alot Vishu.

Now it is working correctly.

PFB...

former_member186851
Active Contributor
0 Kudos

Hello Rajesh,

Yes.UDF is mandatory.

So you achieved the requirement?

Former Member
0 Kudos

Yes Raghu. Now I can clearly validate ,Audit and check the postings from FILE--->IDOC scenario.

This xml filename existing in Idoc segments will be very useful.

Thanks much.

former_member186851
Active Contributor
0 Kudos

Good To Hear ..

Former Member
0 Kudos

Hi Raghu,

See if my comments are right. If wrong please correct me.

//Providing xi location package and title GetFilname as string.

Imports com.sap.xi.tf.*;

Public String GetFileName(Container container){

//This interface GetTransformation contains constants and Parameters that can be used in a java mapping to access runtime parameters.

Most of these parameters are attributes of the message header, e.g., sender party, sender service, etc.

DynamicConfiguration conf = (DynamicConfiguration)container.getTransformationParameters().get(StreamTransformationConstants.DYNAMIC_CONFIGURATION);

//In class DynamicConfigurationKey it Creates an instance specifying the namespace of adapter type and name of ASMA attributes.

DynamicConfigurationKey key = DynamciConfigurationKey.create(“http://sap.com/xi/XI/System/File”, “SourceFileName”);

//GetKey returns the value of specified key which is called in previous line.

String filename = conf.get(key);

//The filename is returned.

return filename;

markangelo_dihiansan
Active Contributor
0 Kudos

Hi Rajesh,

It should be


//In class DynamicConfigurationKey it Creates an instance specifying the namespace of adapter type and name of ASMA attributes.

DynamicConfigurationKey key = DynamciConfigurationKey.create("http://sap.com/xi/XI/System/File", "FileName");

Your double-quotes are slanted, it might not be accepted by the compiler. Aside from that, the code looks correct.

Regards,

Mark

former_member186851
Active Contributor
0 Kudos

Hello Rajesh,

Your asking whether the format of comments are right?

Former Member
0 Kudos

Yes the comments for each line of codings. Since I'm new to Java mapping or UDF'S.

//Providing xi location package and title GetFilname as label string.

Imports com.sap.xi.tf.*;

Public String GetFileName(Container container){

//This interface GetTransformationConstants contains constants that can be used in a java mapping to access runtime parameters.

Most of these parameters are attributes of the message header, e.g., sender party, sender service, etc.

GetTransformationParameters returns a map with the mapping runtime constants. For a description of which constants you can access by using this map.

The constants are attributes of the class StreamTransformationConstants. Ideally to get the value already stored in parameter.

DynamicConfiguration conf = (DynamicConfiguration)container.getTransformationParameters().get(StreamTransformationConstants.DYNAMIC_CONFIGURATION);

//In class DynamicConfigurationKey it Creates an instance specifying the namespace of adapter type and name of ASMA attributes.

DynamicConfigurationKey key = DynamciConfigurationKey.create(“http://sap.com/xi/XI/System/File”, “SourceFileName”);

//GetKey returns the value of specified key which is called in previous line. FileName is adapter parameter.

String filename = conf.get(key);

//The filename is returned containing a name of a file or folder, which can include a path and file extension.

Basically completes all the statements in the method.

return filename;

former_member186851
Active Contributor
0 Kudos

ya Rajesh.Seems to be good.


former_member186851
Active Contributor
0 Kudos

Try this.

Remove all input variables.

Then map the UDF output to the required output field(you can do substring or any functions as per the requirement)

Former Member
0 Kudos

Hi Raghu,

PFA....

In Interface mapping get filename as 'null' and same in idoc segments in ECC. Please help.

markangelo_dihiansan
Active Contributor
0 Kudos

Hi Rajesh,

You can't test dynamic configuration in message mapping test tab. For that to work, you need end-to-end testing.

Regards,

Mark

Former Member
0 Kudos

Hi Mark,

So how can this be done in 7.01.Kindly advice me briefly on this query.

I'm directly creating udf in message mapping and testing the xml(file to idoc scenario).

Below is the PI and ECC output screenshot:

markangelo_dihiansan
Active Contributor
0 Kudos

Hi,

In your sender file adapter, go to advanced and then check Set Adapter-Specific Message Attributes, then also check File Name.

Regards,

Mark

Former Member
0 Kudos

Hi Mark,

In v7.01 there is no such options such as ASMA in Sender File Adapter in Integration Directory.

Former Member
0 Kudos

Hi Raghu,

Please provide your inputs.

markangelo_dihiansan
Active Contributor
0 Kudos
Former Member
0 Kudos

Hi Mark,

Thanks for the reply.

I went across the link which you provided about sender file adapter configurations.

In 7.01 version there is no advanced tab in communication channel of Integration Directory. PFB for the same.

By the way FTP team post ony xml files in source directory so no need of FCC i guess.

Please help on this issue. Thanks

markangelo_dihiansan
Active Contributor
0 Kudos

Hi Rajesh,

In the screenshot you are using SAP Basis 6.40 for your adapter metadata. Please change to SAP BASIS 7.01 by re-choosing the adapter type.

Regards,

Mark

Former Member
0 Kudos

Thanks a lot mark.

Now the xml filename is reflecting in idoc segments. But is it mandatory to create UDF?

Former Member
0 Kudos

Correct Answer

Former Member
0 Kudos

Correct Answer

markangelo_dihiansan
Active Contributor
0 Kudos

Hi Rajesh,

If you want to retrieve values from Dynamic Configuration and use it in XML, then yes, UDF is necessary

Regards,

Mark

Former Member
0 Kudos

Hi Mark,

See if my comments are right. If wrong please very well correct me line by line.

//Providing xi location package and title GetFilname as string.

Imports com.sap.xi.tf.*;

Public String GetFileName(Container container){

//This interface GetTransformation contains constants and Parameters that can be used in a java mapping to access runtime parameters.

Most of these parameters are attributes of the message header, e.g., sender party, sender service, etc.

DynamicConfiguration conf = (DynamicConfiguration)container.getTransformationParameters().get(StreamTransformationConstants.DYNAMIC_CONFIGURATION);

//In class DynamicConfigurationKey it Creates an instance specifying the namespace of adapter type and name of ASMA attributes.

DynamicConfigurationKey key = DynamciConfigurationKey.create(“http://sap.com/xi/XI/System/File”, “SourceFileName”);

//GetKey returns the value of specified key which is called in previous line.

String filename = conf.get(key);

//The filename is returned.

return filename;

former_member186851
Active Contributor
0 Kudos

Hi ,

Try this below code

DynamicConfiguration conf = (DynamicConfiguration) container.getTransformationParameters().get(StreamTransformationConstants.DYNAMIC_CONFIGURATION);

DynamicConfigurationKey key = DynamicConfigurationKey.create("http://sap.com/xi/XI/System/File", "FileName");

string filename=conf.get(key);

filename="Your desired format";

return filename;

Map this to the required node.it doesnot have any input.

iaki_vila
Active Contributor
0 Kudos

Hi Neha,

Have you tried to use the dateTrans function and take its output like input parameter in your UDF?

Check this Sarvesh Singh's blog about dateTrans function

Regards

former_member186851
Active Contributor
0 Kudos

Hi,

You should return the Filename as a string and also no need of any input for the Filename UDF

Former Member
0 Kudos

Hi Raghu,

Thanks for the reply. Could you please elaborate and share the UDF.

Former Member
0 Kudos

Hi All,

Basically my requirement to get filename in idoc segments.


File to idoc scenario. Please check attached UDF and it gives error.

Thanks.

baskar_gopalakrishnan2
Active Contributor
0 Kudos

You might want to check this link and try to convert the source timestamp to your required timestamp. Parsing the timestamp using substring might leads to errors.  

http://stackoverflow.com/questions/5393847/how-can-i-convert-a-timestamp-from-yyyy-mm-ddthhmmsssssz-...