PeopleCode | A base structure for accessing a component interface
A. The Perils of Code Generation
Most PeopleSoft developers would be aware that once a component interface has been created, you can auto-generate the code by dragging the CI object into the code window. Unfortunately, like most code generators, you receive a rather confusing block of code with a high degree of ‘background noise’. By the time you go through the code and work out which parts are useful and which parts are not – commenting out unwanted parts as necessary – you end up with the PeopleCode version of the ‘dog’s breakfast’, a messy block of code that is difficult to interpret.
An alternative approach is to start with your own CI template code, which contains just the basic functionality. The example below covers all the usual tasks involved when using a CI: initialising the component interface, defining the keys, setting the values of some fields at various scroll levels, saving, and cancelling.
B. Why Cancel?
Incidentally, some junior developers I’ve trained like to keep the ‘Cancel’ step commented out, somehow believing that ‘Cancel’ works like ‘Rollback’ and will undo all the CI’s previous changes. When interacting with a CI, always keep in mind that the CI is intended to replicate the experience of a user entering data on the component manually. In most cases the user will click ‘Save’ and then move back to the search record or to another component. The ‘Cancel’ step is designed to mimic this ‘moving away’ step. It tells the program that you are finished with the current record and can move back to the search to process the next record. Technically speaking, if the CI is only designed to process one and only one record, then the ‘Cancel’ step is not necessary. However, it’s good programming practice to always include the ‘Cancel’ (you never know how the code might be altered in the future).
Finally, a quick pointer in regard to terminology. In CI language, a ‘rowset’ is known as a ‘collection’, while a row is called an ‘item’. These name changes are due to the fact that the CI technology was developed with the non-PeopleSoft world in mind. By allowing external programs to access the CI, ‘collections’ and ‘items’ provide a more standardised form of interface.
D. CI Template Code
Here is the full code fragment:
/* Declare variables */ Local File &fileLog; Local ApiObject &oSession, &oCI; Local ApiObject &oLevel1Collection, &oLevel1; Local ApiObject &oLevel2Collection, &oLevel2; /* Standard Error Handler function, writing messages to log file */ Function errorHandler() Local ApiObject &oPSMessageCollection, &oPSMessage; Local number &i; Local string &sErrMsgSetNum, &sErrMsgNum, &sErrMsgText, &sErrType; &oPSMessageCollection = &oSession.PSMessages; For &i = 1 To &oPSMessageCollection.Count &oPSMessage = &oPSMessageCollection.Item(&i); &sErrMsgSetNum = &oPSMessage.MessageSetNumber; &sErrMsgNum = &oPSMessage.MessageNumber; &sErrMsgText = &oPSMessage.Text; &fileLog.WriteLine(&sErrType | " (" | &sErrMsgSetNum | "," | &sErrMsgNum | ") - " | &sErrMsgText); End-For; rem ***** Delete the Messages from the collection *****; &oPSMessageCollection.DeleteAll(); End-Function; try /* Set the Log File */ &fileLog = GetFile("CI.log", "w", "a", %FilePath_Relative); &fileLog.WriteLine("Begin"); /* Get current PeopleSoft Session */ &oSession = %Session; /* Set the PeopleSoft Session Error Message Mode */ &oSession.PSMessagesMode = 1; /* Get the Component Interface */ &oCI = &oSession.GetCompIntfc(CompIntfc.TEST_CI); If &oCI = Null Then errorHandler(); throw CreateException(0, 0, "GetCompIntfc failed"); End-If; /* Set the Component Interface Mode to 'Correct History' */ &oCI.InteractiveMode = False; &oCI.GetHistoryItems = True; &oCI.EditHistoryItems = True; /* Set Component Interface Get/Create Keys */ &oCI.EMPLID = &Curr_Rec.EMPLID.Value; /* Execute Get */ If Not &oCI.Get() Then rem ***** No rows exist for the specified keys.*****; errorHandler(); throw CreateException(0, 0, "Get failed"); End-If; /* Set CI Properties */ &oCI.FIELD_1 = "ABC"; &oCI.FIELD_2 = "XYZ"; /* Define Level 1 */ &oLevel1Collection = &oCI.COLLECTION_NAME; &oLevel1 = &oLevel1Collection.Item(1); /* Set Level 1 Properties */ &oLevel1.FIELD_1 = "ABC"; &oLevel1.FIELD_2 = "XYZ"; /* Define Level 2 */ &oLevel2Collection = &oLevel1.COLLECTION_NAME; /* Loop through Level 2 Collection */ For &i = 1 to &oLevel2Collection.Count &oLevel2 = &oLevel2Collection.Item(&i); /* Set Level 2 Properties */ &oLevel2.FIELD_1 = "ABC"; &oLevel2.FIELD_2 = "XYZ"; End-for; /* Execute Save */ If Not &oCI.Save() Then; errorHandler(); throw CreateException(0, 0, "Save failed"); End-If; /* Execute Cancel */ If Not &oCI.Cancel() Then; errorHandler(); throw CreateException(0, 0, "Cancel failed"); End-If; catch Exception &ex rem Handle the exception; &fileLog.WriteLine(&ex.ToString()); end-try; &fileLog.WriteLine("End"); &fileLog.Close();