Tuesday, April 23, 2013

Using PeopleCode to Insert Images into the Database: A Simple Example

I've looked all over the internet for simple examples of how to insert an image into a database table and display it on a page using PeopleSoft, but I was not able to find one. Most examples are more complex than I was looking for, because they make assumptions about what I probably already should have known. Because of some PeopleSoft "magic", this task is much easier than I thought it would be, but in the event that you learn the way I do, here's a simple example to help you get started.



1. Create a Record. Create a record to hold the images you want to schedule for display.  In this example, I have chosen a field (FILE_DATA_JPG) that will only hold J-PEG images. Build the record so that it exists in the database.
2. Create a Page. Create a component and page on which to use your new record. The page will look something like this. The white box on your App Designer page is an image object that will display the image that you select from your file system.
The image box on the associated web page will stretch to fit the size of the image that you select from the file system.  At a minimum, place an edit box on your page that is assigned to the IMAGE_NAME field of your record (you can make it invisible if you want).  Otherwise, clicking the Add Image button will do nothing (actually if you call Messagebox on the return code from your call to InsertImage, you'll see that it's returning a 1).


Attach the component to a menu of your choice. Set up the security that you need to get to the new component and create the appropriate navigation to your page.


3. Add Some PeopleCode. On the Record PeopleCode behind the Add Image button type in this PeopleCode:

&rc = InsertImage(Y_MFC_IMG.FILE_DATA_JPG);

Here's where it gets easier than I thought.  The InsertImage function is smart enough to call your operating system's File Attachment dialog and, once you have selected an image, to validate and place the image into the record column suited to handle an image file, as partially illustrated below:
I haven't experimented with adding two FILE_DATA image fields to the same record, but I suppose it would be problematic to do so.  

Using my example, because the FILE_DATA_JPG field will only hold J-PEG files, you will see an error similar to the following if you try to insert any other kind of image into it:



Somewhere in the SavePreChange PeopleCode of your record created in step 1, provide for the populating of the DateTime Created and Updated UserID fields by typing in this:

Y_MFC_IMG.DATETIME_CREATED = %Datetime;
Y_MFC_IMG.UPDATED_USERID = %OperatorId;

4. Navigate to the Page and Add an Image.  Once you’ve navigated to your page and added an image, your page will look something like this:

After you are done adding the image, don't forget to click the Save button on the page in order to save the record to the database.

Here's an example of a large image that I inserted.


Let me know what I might have missed, or if you have any questions.


5 comments:

Anonymous said...

Frank,
Thanks for the example! This worked perfectly for what I needed to do.
Bruce

Daniel said...

Very useful example, thanks!

Do you know how to do the reverse?

Retrieve the image from the database and display it on the page?

Anonymous said...

Thanks for the example.

I'll leave this useful tip.

In case you want to update the image, you have to add the field PSIMAGEVER in the record.

Carlos Correa said...

Thanks.

Anonymous said...

Can we allow different image formats options for users to upload the image instead of 1 like jpeg,bmp,pdf.