Wednesday, March 18, 2009

Are Component Interfaces really difficult to implement in PeopleCode? I don't think so!!

There are times when developers are asked to use component interfaces for what-not-to-mention reasons and they happily drag and drop the CI object to the PeopleCode editor and then spend another hour cleanup the skeleton code...when they really only want a few lines of the code..

What is that which makes CI code difficult to understand for beginners (probably some of the experience may also complain the same)? According to me it is the habit of drag-n-drop and never-really-looking-at-the-code practice that makes the simple CI and FileLayout PeopleCode difficult to understand. Lets begin our journey to understand what CI PeopleCode says to a developer...

The Basics:
1 . The Rowsets are reffered by Collections in the CI Jargon (derived from ApiObject).
2. Record and Fields are mapped to Properties in CI.
3. GetRow() [method of Rowset class] is mapped to Item() of ApiObject [your Collection is an instance of ApiObject].

The Coding Technique:

When we code for CI we tend to forget that CI is just like a Robot which has been automated for data entry into a PeopleSoft Component. So the basic rules of the game must be the same as

a. Signing on to the PeopleSoft application
b. Navigating to Component
c. Entering Search Key values
d. clicking on Search or Add button.
e. Enter data in the page fields. Insert/delete rows in the scrolls etc..
f. Save the data (or cancel if do not want to save).

Keeping in mind the steps a to f makes it very very simple to work with CI.
Thus here it goes:
1. Signon to PeopleSoft
Local Apiobject &Session;
&Sessions = %Session;

The above code clearly tells us that the first step is to sign on to the application. The &Session variables holds the derived session value from the system variable %Session.

2. Next "navigate" to the component interface.
Local ApiObject &CI;
&CI = &Session.GetCompIntfc(CompIntfc.MYCI);


3. Next "select" the mode in which you want to enter the data. Isn't it that we enter search keys when we want to add/update the data!!
&CI.FIELD1 = "MyVal";
&CI.FIELD2 = "MYVal2";

rem Lets get into add mode!!;
&CI.Create();
Just in case you want to get into update mode use &CI.Get();

4. Now we are on the page [i.e. moved on from Search/Add page to our real application page]. So lets enter data in some of the fields [remember record fields are properties here].
&CI.FIRSTNAME = "Funny";
&CI.LASTNAME = "NAME";


5. Do we have a Level 1 Scroll?. Ah yes..lest retrieve it and enter data in the fields...
Local ApiObject &Level1Scroll, &Level1Row;
&Level1Scroll = &CI.MYLEVEL1SCROLLNAME;

Since by default at least one blank row is there in the scroll [this can be controlled via the GetDummyRows property of the CI].
&Level1Row = &Level1Scrol.Item(1);
&Level1Row.SOMELEVEl1FIELD = "SOMEVAL1";


Oh now you want to insert a new row in your level 1 scroll as if you are clicking on the "+" button on the online page. Recall that Rowset class has InsertRow method to insert a new row after some row in the rowset. The corresponding method for our ApiObject [Collection] is InserItem(integer) where integer specifies the insertion of a new row "after which row".
Since we have currently one row in our Collection, we want to insert a new row after this row. Thus:
&Level1Row = &Level1Scroll.InsertItem(1);
&Level1Row.SOMELEVEl1FIELD= "SOMEVAL2";


6. Isn't this that we are all done with our data entry...Why not just save the data now.
&CI.Save();

The above code example shows the necessary steps to be followed when writing the CI Code. Of course you need to take care of including your code within the try...Catch block and appropriately wrapping your code within this block. The code is a simplistic explanation of what seems difficult for some of the developers.

2 comments: