FTP PeopleCode

FTP PeopleCode

I was recently working on an interface that needed to pull files from the vendor via FTP peoplecode. More specifically, I needed to retrieve an unknown number of ASCII files from a vendor’s server, with no specific naming convention.

I have done this before using a ‘call system’ command to kick off a bat script that would handle staging the files. This worked pretty good, and I eventually got the bat script to perform fairly decent error handling. But this interface was being written in App Engine. well more accurately, a PeopleCode program kicked off by an App Engine shell (I still hate writing App Engine programs the “PeopleSoft recommended” way).

PeopleCode does provide the GetAttachment function, but this seemed quite limiting in that it required me knowing the4name of the file I wanted to retrieve. I needed something more flexible. Searching the internet, I found a Slerp article by David Jen that explains how to use PeopleCode’s external library declaration to utilize the FTP functionality in the wininet.dll library. This was great, but it still required me to know the name of the file I wanted to ‘get’. But, at least, I now had another road to pursue. After understanding more of what was available to me in the wininet.dll and playing around with different configurations, I came up with the following:

/************************* DECLARE EXTERNAL FUNCTIONS *************************/
Declare Function GetLastError Library “kernel32”
() Returns long As number;

Declare Function FormatMessageA Library “kernel32”
(long Value As number, long Value As number, long Value As number, long Value As number, string Ref As string, long Value As number, long Value As number) Returns long As number;

Declare Function InternetOpenA Library “wininet.dll”
(string Value As string, long Value As number, string Value As string, string Value As string, long Value As number) Returns long As number;

Declare Function InternetConnectA Library “wininet.dll”
(long Value As number, string Value As string, integer Value As number, string Value As string, string Value As string, long Value As number, long Value As number, long Value As number) Returns long As number;

Declare Function FtpSetCurrentDirectoryA Library “wininet.dll”
(long Value As number, string Value As string) Returns long As number;

Declare Function FtpGetFileA Library “wininet.dll”
(long Value As number, string Value As string, string Value As string, long Value As number, long Value As number, long Value As number, long Value As number) Returns boolean;

Declare Function FtpDeleteFileA Library “wininet.dll”
(long Value As number, string Value As string) Returns boolean;

Declare Function FtpCommandA Library “wininet.dll”
(long Value As number, long Value As number, long Value As number, string Value As string, long Value As number, long Ref As number) Returns boolean;

Declare Function InternetReadFile Library “wininet.dll”
(long Value As number, string Ref As string, integer Value As number, integer Ref As number) Returns boolean;

Declare Function InternetCloseHandle Library “wininet.dll”
(long Value As number) Returns integer As number;

/*********************** END DECLARE EXTERNAL FUNCTIONS ***********************/

/* Program Variable Declarations */
Local array of string &gFileList;
Local number &gHostOpen, &gHostConnect;
Local string &gHostFileName;
Local number &gReturnCode;
Local boolean &gReturnStatus;

Function GetNTMessage(&inReturnCode As number) Returns string;
Local number &l_MsgLength;
Local string &l_ReturnMsg;

&l_MsgLength = FormatMessageA(4096, 0, &inReturnCode, 0, &l_ReturnMsg, 256, 0);
If &l_MsgLength > 0 Then
Return &l_ReturnMsg;
Else
Return “”;
End-If;
End-Function;

Function FTPOpenHostConnection(&inLogFile As File) Returns number;
Local number &l_OPEN_PRECONFIG = 0;
Local number &l_Handle;

/* Open connection to host */
&l_Handle = InternetOpenA(“Peoplecode FTP”, &l_OPEN_PRECONFIG, “”, “”, 0);
If &l_Handle = 0 Then
&inLogFile.WriteLine(“FTP ERROR: Unable to open Internet connection.”);
End-If;
Return &l_Handle;
End-Function;

Function FTPConnectToHost(&inLogFile As File, &inHandleOpen As number, &inHostSystem As string, &inHostDirectory As string, &inHostFTPAccount As string, &inHostPassword As string) Returns number;
Local number &l_FTP_PORT = 21;
Local number &l_INET_FTP = 1;
Local number &l_INET_PASSIVE = 134217728;

Local number &l_HostDirSet, &l_Handle;
Local number &l_ReturnCode;

/* Login to Host */
&l_Handle = InternetConnectA(&inHandleOpen, &inHostSystem, &l_FTP_PORT, &inHostFTPAccount, &inHostPassword, &l_INET_FTP, &l_INET_PASSIVE, 0);

If &l_Handle = 0 Then
&inLogFile.WriteLine(“FTP ERROR: Unable to open connection to ” | &inHostSystem | ” !!!”);
Return 0;
End-If;

/* Change remote directory */
If All(&inHostDirectory) Then /* If host directory passed, set it. */
&l_HostDirSet = FtpSetCurrentDirectoryA(&l_Handle, &inHostDirectory);
Else
&l_HostDirSet = 1;
End-If;

If &l_HostDirSet = 0 Then
&inLogFile.WriteLine(“FTP ERROR: Unable to set directory to ” | &inHostDirectory | ” !!!”);
&l_ReturnCode = InternetCloseHandle(&l_Handle);
Return 0;
End-If;

Return &l_Handle;
End-Function;

Function FTPGetFileList(&inLogFile As File, &inHandleConnect As number) Returns array of string;
Local string &l_FTP_CMD = “NLST”;
Local number &l_ASCII = 1;

Local array of string &l_FileList = CreateArrayRept(“”, 0);

Local number &l_FTPHandle;
Local string &l_Out, &l_Text_Buffer;
Local number &l_Bytes = 100;
Local number &l_Bytes_Read;
Local boolean &l_ReturnStatus;
Local number &l_ReturnCode;

&l_ReturnStatus = FtpCommandA(&inHandleConnect, 1, &l_ASCII, &l_FTP_CMD, 0, &l_FTPHandle);
If &l_FTPHandle = 0 Then
&inLogFile.WriteLine(“FTP ERROR: Unable to get directory !!!”);
Return &l_FileList;
Else
&inLogFile.WriteLine(“Getting List of files from host.”);
&l_ReturnStatus = InternetReadFile(&l_FTPHandle, &l_Text_Buffer, &l_Bytes, &l_Bytes_Read);
While &l_Bytes_Read > 0
&l_Out = &l_Out | &l_Text_Buffer;
&l_ReturnStatus = InternetReadFile(&l_FTPHandle, &l_Text_Buffer, &l_Bytes, &l_Bytes_Read);
End-While;
&l_ReturnCode = InternetCloseHandle(&l_FTPHandle);
End-If;
&l_FileList = Split(&l_Out, Char(13) | Char(10));
&inLogFile.WriteLine(String(&l_FileList.Len) | ” file(s) found.”);
Return &l_FileList;
End-Function;

Function FTPGetFile(&inLogFile As File, &inHandleConnect As number, &inHostFileName As string, &inLocalDirectory) Returns boolean;
Local number &l_FTP_ASCII = 1;
Local string &l_LocalFileName;
Local number &l_ReturnCode;
Local string &l_ReturnMsg;
Local boolean &l_GetFile;

/* Get (Session, local file, remote file, failexist, flags&attibutes, flags, context */
&inLogFile.WriteLine(“Getting ” | &inHostFileName | “…”);
&l_LocalFileName = &inLocalDirectory | &inHostFileName;
&l_GetFile = FtpGetFileA(&inHandleConnect, &inHostFileName, &l_LocalFileName, 0, 0, &l_FTP_ASCII, 0);

If Not &l_GetFile Then
&l_ReturnCode = GetLastError();
&l_ReturnMsg = GetNTMessage(&l_ReturnCode);
&inLogFile.WriteLine(“FTP ERROR: ” | String(&l_ReturnCode) | ” : ” | &l_ReturnMsg);
&inLogFile.WriteLine(“FTP ERROR: Unable to retrieve file !!!”);
Return False;
End-If;

/* Sucessful Get */
&inLogFile.WriteLine(” Get Sucessful.”);
Return True;
End-Function;

Function FTPDeleteFile(&inLogFile As File, &inHandleConnect As number, &inHostFileName As string) Returns boolean;
Local boolean &l_DeleteFile;
Local number &l_ReturnCode;
Local string &l_ReturnMsg;

&l_DeleteFile = FtpDeleteFileA(&inHandleConnect, &inHostFileName);

If Not &l_DeleteFile Then
&l_ReturnCode = GetLastError();
&l_ReturnMsg = GetNTMessage(&l_ReturnCode);
&inLogFile.WriteLine(” FTP ERROR: ” | String(&l_ReturnCode) | ” : ” | &l_ReturnMsg);
&inLogFile.WriteLine(” FTP ERROR: Unable to delete file.”);
Return False;
End-If;

/* SUCCESS */
&inLogFile.WriteLine(” File Deleted from HRSmart server.”);
Return True;
End-Function;

/*****************************************************************************/
/* MAIN PROGRAM
/*****************************************************************************/

/* Log File */
Local File &gLogFile;

/* Host information */
Local string &HOSTSERVER = “www.example.com”;
Local string &HOSTACCOUNT = “usename”;
Local string &HOSTPASSWORD = “password”;
Local string &HOSTDIRECTORY = “”;
Local string &FILE_MASK = “%”;

/* A File Object &gLogFile needs to have already been open prior to calling this routine. */
/* Log information is written to this file */

&gHostOpen = FTPOpenHostConnection(&gLogFile);
If &gHostOpen > 0 Then
&gHostConnect = FTPConnectToHost(&gLogFile, &gHostOpen, &HOSTSERVER, &HOSTDIRECTORY, &HOSTACCOUNT, &HOSTPASSWORD);
If &gHostConnect > 0 Then
&gFileList = FTPGetFileList(&gLogFile, &gHostConnect);
While &gFileList.Len > 0
&gHostFileName = &gFileList.Shift();
If DBPatternMatch(&gHostFileName, &FILE_MASK, False) Then
If FTPGetFile(&gLogFile, &gHostConnect, &gHostFileName, &LOCALDIRECTORY) Then
&gReturnStatus = FTPDeleteFile(&gLogFile, &gHostConnect, &gHostFileName);
End-If;
End-If;
End-While;
&gReturnCode = InternetCloseHandle(&gHostConnect);
&gReturnCode = InternetCloseHandle(&gHostOpen);
Else
&gReturnCode = InternetCloseHandle(&gHostOpen);
End-If;
End-If;

One thing to note… it seems that PeopleCode can only use DLL functions when they return datatypes that are understood by PeopleCode (strings, numbers, boolean). In trying to find a way to search the FTP directory, I came across a couple functions named FindFirstFile and FindNextFile. Unfortunately, these returned a pointer to a pointer to a WIN32_FIND_DATA structure. I could not figure out a way to use these. As a result, I had to take a slightly longer approach using the FTPCommand function.

Credit :http://ideatec.blogspot.com/2005/03/ftp-in-peoplecode.html

PL SQL Select Update

PL SQL Select Update

This is PL SQL Select Update

declare
cursor c1 is

select course_no,course_name,faculty
from coursemain;

rec c1%rowtype;
begin

for rec in c1 loop

update coursemain
Set course_name = rec.course_name,
faculty_no = rec.faculty
where course_no = rec.course_no;

end loop;

end;

PeopleCode To Browse for File and Save attachments To FTP or Database Server

PeopleCode To Browse for File and Save attachments To FTP or Database Server

/* Using add_attachment if required- only using AddAttachment
Declare Function add_attachment PeopleCode FILE_ATTACH_WRK.ATTACHADD FieldChange;
add_attachment(FUNCLIB_UTIL.FTPSITE, &FILEEXTENSION, &SUBDIRECTORY, &FILESIZE, False, &recname, FUNCLIB_UTIL.ATTACHSYSFILENAME, FUNCLIB_UTIL.ATTACHUSERFILE, &MESSAGE_LVL, &RETCODE);
*/

/* All the following assignments to &URL_ID work – I have commented off all except one in each case, because only one assignment is required */
/*&URL_ID = “RECORD://HRS_ATTACHMENTS”;*/
/*&URL_ID = “ftp://user:password@ftpserver.peoplesoft.com:6000/”;*/
/*&URL_ID = “ftp://user:password@ftpserver.peoplesoft.com:6000/PATH”;*/
/*&URL_ID = “URL.HRS_INT_ATCH”;*/
/* The following goes to the default folder (default folder assigned to the login user id) on the FTP server – example: /sw/app/psoft/psofthr*/
&URL_ID = “ftp://pmilind:password@mycompany.com”;
/* If the default folder on FTP server is /sw/app/psoft/psofthr then the following puts it to /sw/app/psoft/psofthr/log */
/*&URL_ID = “ftp://pmilind:password@mycompany.com/log”;*/
/* The following puts the file on the /tmp folder on the system root */
/*&URL_ID = “ftp://pmilind:password@mycompany.com/../../../../tmp”;*/

If Exact(Left(&URL_ID, 4), “URL.”) Then
   &RETCODE = AddAttachment(@(&URL_ID), &ATTACHSYSFILENAME, &FILEEXTENSION, &ATTACHUSERFILE, &FILESIZE);
Else
   &RETCODE = AddAttachment(&URL_ID, &ATTACHSYSFILENAME, &FILEEXTENSION, &ATTACHUSERFILE, &FILESIZE);
End-If;

References:
http://www.pscustomizer.com/peoplesoft-examples/attachments/
http://peoplesoft.ittoolbox.com/groups/technical-functional/peopletools-l/write-file-into-database-using-sqr-2528747
https://forums.oracle.com/forums/thread.jspa?messageID=10320192
http://docs.oracle.com/cd/E15645_01/pt850pbr0/eng/psbooks/tpcl/book.htm?File=tpcl/htm/tpcl02.htm#H4541 
http://docs.oracle.com/cd/E15645_01/pt850pbr0/eng/psbooks/tpcd/chapter.htm?File=tpcd/htm/tpcd11.htm
http://peoplesoftstuff.blogspot.com/2012/07/upload-file-from-local-to-server.html 
http://peoplesoft.ittoolbox.com/groups/technical-functional/peopletools-l/browse-functionality-on-run-control-page-819632

PeopleCode to Save File Attachment from any Server to FTP or Database Server

PeopleCode to Save File Attachment from any Server to FTP or Database Server 

(does not Browse for File on the PC – just takes it from a variable name – use this if the files are already known or created by the program where no user input is required)

/* This code saves file from any server (NT/Unix) to FTP or Database Server using PutAttachment command*/
/* Unlike AddAttachment, PutAttachment does not open the file browse window for user to select one file to load */

Local File &MYFILE;
Local string &ATTACHUSERFILE, &ATTACHSYSFILENAME, &fname, &fname_provided, &Date_Time;
/*Local array of string &FNAMES;*/
/*&FNAMES = FindFiles(RUN_CNTL_HR.WHERE_CLAUSE, %FilePath_Absolute); /* Use for Multiple Files – Store paths of all files on a folder to an Array */
/*While &FNAMES.Len > 0*/

&URL_ID = “RECORD://HRS_ATTACHMENTS”;
/*&URL_ID = “ftp://user:password@ftpserver.peoplesoft.com:6000/”;*/
/*&URL_ID = “URL.HRS_INT_ATCH”;*/
/*&URL_ID = “ftp://user:password@mycompany.com”;*/

&fname_provided = RUN_CNTL_HR.WHERE_CLAUSE;
/*&fname_provided = “/sw/app/psoft/psofthr/OSGN.pdf”; — if the app server is on Unix – file on Unix also*/
/*&fname_provided = “\\sy-hr9upg\hrdev\ps\OSGN.pdf”; — if the app server is on NT – file on NT also – follow UNC naming convention*/
/* if the app server is on unix and the files are on NT – write an app engine program, start it from a page button, but force it to run on NT */

&MYFILE = GetFile(&fname_provided, “R”, %FilePath_Absolute); /* get the file */
&Date_Time = DateTimeToLocalizedString(%Datetime, “yyyyMMdd_HHmmss”);
&ATTACHUSERFILE = &MYFILE.Name; /* Actual File Name with path */
&fname = Substitute(&MYFILE.Name, “:\”, “_”);
&fname = Substitute(&fname, “\”, “_”);
&fname = Substitute(&fname, “/”, “_”);
&fname = Substitute(&fname, ” “, “_”);
&ATTACHSYSFILENAME = Replace(&fname, Len(&fname) – 3, 0, “_” | &Date_Time | “_” | &file_no); /* Unique name – same name may overwrite old file */
If Exact(Left(&URL_ID, 4), “URL.”) Then
   &return_code = PutAttachment(@(&URL_ID), &ATTACHSYSFILENAME, &ATTACHUSERFILE); /* Upload file */
Else
   &return_code = PutAttachment(&URL_ID, &ATTACHSYSFILENAME, &ATTACHUSERFILE); /* Upload file */
End-If;

References:
http://www.pscustomizer.com/peoplesoft-examples/attachments/
http://peoplesoft.ittoolbox.com/groups/technical-functional/peopletools-l/write-file-into-database-using-sqr-2528747
https://forums.oracle.com/forums/thread.jspa?messageID=10320192
http://docs.oracle.com/cd/E15645_01/pt850pbr0/eng/psbooks/tpcl/book.htm?File=tpcl/htm/tpcl02.htm#H4541 
http://docs.oracle.com/cd/E15645_01/pt850pbr0/eng/psbooks/tpcd/chapter.htm?File=tpcd/htm/tpcd11.htm
http://peoplesoftstuff.blogspot.com/2012/07/upload-file-from-local-to-server.html 
http://peoplesoft.ittoolbox.com/groups/technical-functional/peopletools-l/browse-functionality-on-run-control-page-819632

PeopleCode to Copy/Retrieve file FROM Database to File Server or any LAN area

PeopleCode to Copy/Retrieve file FROM Database to File Server or any LAN area

/* The file will be copied to the /files directory on the App Server */
&file_name = &InOutAttachUserfile;
&temp_dir = “C:\Temp\”;

/* Copy file from database record to the files directory */
&Result = GetAttachment(&URL_ID, &file_name, &temp_dir | &file_name);

 

References:
http://www.pscustomizer.com/peoplesoft-examples/attachments/
http://peoplesoft.ittoolbox.com/groups/technical-functional/peopletools-l/write-file-into-database-using-sqr-2528747
https://forums.oracle.com/forums/thread.jspa?messageID=10320192
http://docs.oracle.com/cd/E15645_01/pt850pbr0/eng/psbooks/tpcl/book.htm?File=tpcl/htm/tpcl02.htm#H4541 
http://docs.oracle.com/cd/E15645_01/pt850pbr0/eng/psbooks/tpcd/chapter.htm?File=tpcd/htm/tpcd11.htm
http://peoplesoftstuff.blogspot.com/2012/07/upload-file-from-local-to-server.html 
http://peoplesoft.ittoolbox.com/groups/technical-functional/peopletools-l/browse-functionality-on-run-control-page-819632

Application Engine PeopleCode to Copy one File to FTP or Database Server

Application Engine PeopleCode to Copy one File to FTP or Database Server

[Purpose: if the application server is on Unix and the files are on NT/LAN – write an app engine program, start it from a page button, etc. on app server (Unix), but force it to run on process scheduler on NT or Windows that has access to LAN]
Local File &MYFILE;
Local string &ATTACHUSERFILE, &ATTACHSYSFILENAME, &fname, &fname_provided, &Date_Time;
/*Local array of string &FNAMES;*/
/*&FNAMES = FindFiles(RUN_CNTL_HR.WHERE_CLAUSE, %FilePath_Absolute); /* Use for Multiple Files – Store paths of all files on a folder to an Array */
/*While &FNAMES.Len > 0*/

&URL_ID = “RECORD://HRS_ATTACHMENTS”;
/*&URL_ID = “ftp://user:password@ftpserver.peoplesoft.com:6000/”;*/
/*&URL_ID = “URL.HRS_INT_ATCH”;*/
/*&URL_ID = “ftp://userid:password@mycompany.com”;*/

/*fname_provided = RUN_CNTL_HR.WHERE_CLAUSE;*/
/*name_provided = “/sw/app/psoft/psofthr/OSGN5.pdf”;*/
/*&fname_provided = “/sw/app/psoft/psofthr/OSGN.pdf”; — if the app server is on Unix – file on Unix also*/
&fname_provided = “\\sy-hr9upg\hrdev\ps\test1\OSGN5.docx”; /* if the app server is on NT – file on NT also – follow UNC naming convention*/
/* if the app server is on unix and the files are on NT – write an app engine program, start it from a page button, but force it to run on NT */

/* get the file */
&MYFILE = GetFile(&fname_provided, “R”, %FilePath_Absolute);

&Date_Time = DateTimeToLocalizedString(%Datetime, “yyyyMMdd_HHmmss”);
&ATTACHUSERFILE = &MYFILE.Name; /* Actual File Name with path */
&fname = Substitute(&MYFILE.Name, “:\”, “_”);
&fname = Substitute(&fname, “\”, “_”);
&fname = Substitute(&fname, “/”, “_”);
&fname = Substitute(&fname, ” “, “_”);
&ATTACHSYSFILENAME = Replace(&fname, Len(&fname) – 3, 0, “_” | &Date_Time | “_” | &file_no); /* Unique name – same name may overwrite old file */
If Exact(Left(&URL_ID, 4), “URL.”) Then
   &return_code = PutAttachment(@(&URL_ID), &ATTACHSYSFILENAME, &ATTACHUSERFILE); /* Upload file */
Else
   &return_code = PutAttachment(&URL_ID, &ATTACHSYSFILENAME, &ATTACHUSERFILE); /* Upload file */
End-If;

 

References:
http://www.pscustomizer.com/peoplesoft-examples/attachments/
http://peoplesoft.ittoolbox.com/groups/technical-functional/peopletools-l/write-file-into-database-using-sqr-2528747
https://forums.oracle.com/forums/thread.jspa?messageID=10320192
http://docs.oracle.com/cd/E15645_01/pt850pbr0/eng/psbooks/tpcl/book.htm?File=tpcl/htm/tpcl02.htm#H4541 
http://docs.oracle.com/cd/E15645_01/pt850pbr0/eng/psbooks/tpcd/chapter.htm?File=tpcd/htm/tpcd11.htm
http://peoplesoftstuff.blogspot.com/2012/07/upload-file-from-local-to-server.html 
http://peoplesoft.ittoolbox.com/groups/technical-functional/peopletools-l/browse-functionality-on-run-control-page-819632

Application Engine PeopleCode to Copy All or Some Files in a Folder to FTP or Database Server

Application Engine PeopleCode to Copy All or Some Files in a Folder to FTP or Database Server

Local File &MYFILE, &LogFile;
Local array of string &FNAMES;
Local number &file_no;
Local string &ATTACHUSERFILE, &ATTACHSYSFILENAME, &fname, &Date_Time;
Local Record &DOC_REC;
Local SQL &SQL1;

&LogFile = GetFile(“\\sy-hr9upg\hrdev\ps\test1\q_fileupload_log.log”, “w”, “a”, %FilePath_Absolute);

/* Set Log File */
&FNAMES = FindFiles(“\\sy-hr9upg\hrdev\ps\test1\*.docx”, %FilePath_Absolute);

/* Store paths of all files(pdf) of a folder to an Array */
&LogFile.WriteLine(“No. of files found: “ | &FNAMES.Len);
&file_no = 1;

&URL_ID = “RECORD://HRS_ATTACHMENTS”;
/*&URL_ID = “ftp://user:password@ftpserver.peoplesoft.com:6000/”;*/
/*&URL_ID = “ftp://user:password@ftpserver.peoplesoft.com:6000/PATH”;*/
/*&URL_ID = “URL.HRS_INT_ATCH”;*/
/* The following goes to the default folder (default folder assigned to the login user id) on the FTP server – example: /sw/app/psoft/psofthr*/
/*&URL_ID = “ftp://pmilind:password@mycompany.com”;*/
/* If the default folder on FTP server is /sw/app/psoft/psofthr then the following puts it to /sw/app/psoft/psofthr/log */
/*&URL_ID = “ftp://pmilind:password@mycompany.com/log”;*/
/* The following puts the file on the /tmp folder on the system root */
/*&URL_ID = “ftp://pmilind:password@mycompany.com/../../../../tmp”;*/

While &FNAMES.Len > 0
&MYFILE = GetFile(&FNAMES.Shift(), “R”, %FilePath_Absolute);

/* Catch each file */
&LogFile.WriteLine(“File No: “ | &file_no | “, File Name: “ | &MYFILE.Name);

   /* String processing to prepare parameters for putattachment() */
   &Date_Time = DateTimeToLocalizedString(%Datetime, “yyyyMMdd_HHmmss”);
   &ATTACHUSERFILE = &MYFILE.Name;

/* Actual File Name with path */
   &fname = Substitute(&MYFILE.Name, “:\”, “_”);
   &fname = Substitute(&fname, “\”, “_”);
   &fname = Substitute(&fname, “/”, “_”);
   &fname = Substitute(&fname, ” “, “_”);
   &ATTACHSYSFILENAME = Replace(&fname, Len(&fname) – 4, 0, “_” | &Date_Time | “_” | &file_no);

/* Sysfilename for DB purpose ? should be unique – use 4 if extension is 4 chars (docx), else use 3 (pdf) */
&LogFile.WriteLine(“&ATTACHUSERFILE = “ | &ATTACHUSERFILE);
&LogFile.WriteLine(“&ATTACHSYSFILENAME = “ | &ATTACHSYSFILENAME);

If Exact(Left(&URL_ID, 4), “URL.”) Then
   &return_code = PutAttachment(@(&URL_ID), &ATTACHSYSFILENAME, &ATTACHUSERFILE);

 /* Upload each file */
Else
&return_code = PutAttachment(&URL_ID, &ATTACHSYSFILENAME, &ATTACHUSERFILE);

/* Upload each file */
End-If;

/* Note: AddAttachment() is not allowed in AE */
&LogFile.WriteLine(“&return_code = ” | &return_code);
&MYFILE.Close();
&file_no = &file_no + 1;
End-While;

/* Create Step 2 in Application Engine*/
/* Make Sure Step 1 has COMMIT:After Step */
/* Uploaded Files to be deleted from Folder for security reasons */
/* Begin – Delete files from Folder – K.S.Rama Naidu */
/*Local File &MYFILE, &LogFile;
Local array of string &FNAMES;
Local number &file_no;
Local string &ATTACHUSERFILE, &ATTACHSYSFILENAME;*/
&LogFile = GetFile(“C:\temp\q_fileupload_log.log”, “w”, “a”, %FilePath_Absolute);

/* Set Log File */
/* CHANGE ACTUAL PATH IN PRODUCTION – Create a new URL to avoid hardcode – or pass from Run Control Page*/
/*
&FNAMES = FindFiles(URL.NEW_URL, %FilePath_Absolute);
*/
&FNAMES = FindFiles(“\\sy-hr9upg\hrdev\ps\test1\*.docx”, %FilePath_Absolute);
&LogFile.WriteLine(“No. of files found: ” | &FNAMES.Len);
&LogFile.WriteLine(” “);
&file_no = 1;
While &FNAMES.Len > 0
/* Catch each file */
&MYFILE = GetFile(&FNAMES.Shift(), “R”, %FilePath_Absolute);
&LogFile.WriteLine(“File No: ” | &file_no | “, File Name: ” | &MYFILE.Name);

/* DELETE each file */
&MYFILE.Delete();
REM &return_code = DeleteAttachment(URL.NEW_URL, &ATTACHSYSFILENAME);

&LogFile.WriteLine(“&return_code = ” | &return_code);
&LogFile.WriteLine(” “);
&MYFILE.Close();

&file_no = &file_no + 1;
End-While;

/* The above deletes files from source area*/
/* The following deletes file from the FTP or Database Server.
&ATTACHSYSFILENAME = “Test123.pdf”;
If Exact(Left(&URL_ID, 4), “URL.”) Then
&return_code = DeleteAttachment(@(&URL_ID), &ATTACHSYSFILENAME);
Else
&return_code = DeleteAttachment(&URL_ID, &ATTACHSYSFILENAME);
End-If;
*/

/* Use the following code in the main PeopleCode to schedule this app engine directly
&processRqst = CreateProcessRequest();
&processRqst.RunControlID = %Datetime;
&processRqst.ProcessType = “Application Engine”;
&processRqst.ProcessName = “YourAEName”;
&processRqst.RunDateTime = %Datetime;
&processRqst.RunLocation = “PSNT”;
&processRqst.Schedule();

If &processRqst.Status = 0 Then
WinMessage(“Success”, 0);
Else
WinMessage(“Error”, 0);
End-If;

*/

References:
http://www.pscustomizer.com/peoplesoft-examples/attachments/
http://peoplesoft.ittoolbox.com/groups/technical-functional/peopletools-l/write-file-into-database-using-sqr-2528747
https://forums.oracle.com/forums/thread.jspa?messageID=10320192
http://docs.oracle.com/cd/E15645_01/pt850pbr0/eng/psbooks/tpcl/book.htm?File=tpcl/htm/tpcl02.htm#H4541
http://docs.oracle.com/cd/E15645_01/pt850pbr0/eng/psbooks/tpcd/chapter.htm?File=tpcd/htm/tpcd11.htm
http://peoplesoftstuff.blogspot.com/2012/07/upload-file-from-local-to-server.html
http://peoplesoft.ittoolbox.com/groups/technical-functional/peopletools-l/browse-functionality-on-run-control-page-819632

Peoplecode to Delete Multiple File Attachments

Peoplecode to Delete Multiple File Attachments

Local Rowset &rs1;

&URL_ID = “RECORD://HRS_ATTACHMENTS”;

&rs1 = CreateRowset(Record.HRS_ATTACHMENTS);
&rs1.Fill(“where ATTACHSYSFILENAME like :1”, “%OSGN%”);

For &i = 1 To &rs1.ActiveRowCount
&Value1 = &rs1(&i).HRS_ATTACHMENTS.ATTACHSYSFILENAME.Value;
&return_code = DeleteAttachment(&URL_ID, &Value1);
End-For;

 

References:
http://www.pscustomizer.com/peoplesoft-examples/attachments/
http://peoplesoft.ittoolbox.com/groups/technical-functional/peopletools-l/write-file-into-database-using-sqr-2528747
https://forums.oracle.com/forums/thread.jspa?messageID=10320192
http://docs.oracle.com/cd/E15645_01/pt850pbr0/eng/psbooks/tpcl/book.htm?File=tpcl/htm/tpcl02.htm#H4541
http://docs.oracle.com/cd/E15645_01/pt850pbr0/eng/psbooks/tpcd/chapter.htm?File=tpcd/htm/tpcd11.htm
http://peoplesoftstuff.blogspot.com/2012/07/upload-file-from-local-to-server.html
http://peoplesoft.ittoolbox.com/groups/technical-functional/peopletools-l/browse-functionality-on-run-control-page-819632

How to Run Report Definition of BI Publisher in PeopleCode

How to Run Report Definition of BI Publisher in PeopleCode

/*————————————————————————————– */
/* You must reference an application package at the beginning of the event  */
/*————————————————————————————– */
import PSXP_RPTDEFNMANAGER:*;

Local Record &rcdQryPrompts;
Local string &LanguageCd, &MyTemplate, &MyReportName, &OutFormat, &State;
Local date &AsOfDate;

/*– Set XML Publisher report required parameters –*/
&LanguageCd = “THA”;
&AsOfDate = %Date;
&OutFormat = “PDF”;

/* —————————————————  */
/* Create a PDF using XML Report Definition  */
/* —————————————————- */
&MyReportName = “Report Name”;
&MyTemplate = “TemplateName”;

/* ————————————————————————————-   */
/* Hard code my State value – would normally pull from a field on the page */
/* ————————————————————————————-   */
&EMPLID = Record.EMPLID;
&YEAR = Record.YEAR;
&SEQ = Record.SEQ;
/* —————————————————————————– */
/* Declare and Instantiate (construct) your Report Definition Object */
/* —————————————————————————– */
Local PSXP_RPTDEFNMANAGER:ReportDefn &oReportDefn = create PSXP_RPTDEFNMANAGER:ReportDefn(&MyReportName);

/* —————————————————————————– */
/* Get a handle on your Report Definition */
/* —————————————————————————– */
&oReportDefn.Get();

/* —————————————————————————– */
/* Since there is a prompt to the query used in this report, you need to */
/* provide the value for the prompt */
/* —————————————————————————– */
&rcdQryPrompts = &oReportDefn.GetPSQueryPromptRecord();
If Not &rcdQryPrompts = Null Then
&oReportDefn.SetPSQueryPromptRecord(&rcdQryPrompts);
/* —————————————————————————– */
/* Provide a value to the State Prompt */
/* —————————————————————————– */
rem &rcdQryPrompts.PROCESS_INSTANCE.Value = &ProcessInstane;
&rcdQryPrompts.EMPLID.Value = &EMPLID;
&rcdQryPrompts.YEAR.Value = &YEAR;
&rcdQryPrompts.SEQ.Value = &SEQ;
End-If;

/* —————————————————————————– */
/* Kick of the report process */
/* —————————————————————————– */
&oReportDefn.ProcessReport(&MyTemplate, &LanguageCd, &AsOfDate, &OutFormat);

/* —————————————————————————– */
/* CommitWork must be called prior to displaying the output since the */
/* application package performed work and SQL statements. If you do */
/* not commit the work performed to this point you will receive an */
/* error like “Think-time PeopleCode event (ViewAttachment), but a SQL */
/* update has occurred in the commit interval. (2, 148)” */
/* —————————————————————————– */
CommitWork();

/* —————————————————————————– */
/* Display Report to the user */
/* —————————————————————————– */
&oReportDefn.DisplayOutput();

Traversing Level 0 to Level 3 By PeopleCode

Traversing Level 0 to Level 3 By PeopleCode

Example code about Traversing Level 0 to Level 3 By PeopleCode. You can copy and paste this code of each event action and change record name and scroll name and filed name.

 

Reference : http://www.dbtutor.com/2013/06/02/peoplecode-for-traversing-level-0-to-level-3