Create a New Instance of Data Example

The following is an example of how to create a new instance of a Component Interface.

To create a new instance of data:

In this example, you are creating a new instance of data for the EXPRESS Component Interface, which is based on the EXPRESS_ISSUE_INV component. The following is the complete code sample: the steps explain each line.

Local ApiObject &MYSESSION;
Local ApiObject &MYCI;
&MYSESSION = %Session;
&MYCI = &MYSESSION.GetCompIntfc(COMPINTFC.EXPRESS);
&MYCI.BUSINESS_UNIT = "H01B";
&MYCI.INTERNAL_FLG = "Y";
&MYCI.ORDER_NO = "NEXT’;
&MYCI.Create();
&MYCI.CUSTOMER = "John’s Chicken Shack";
&MYCI.LOCATION = "H10B6987";
.
.
.
If NOT(&MYCI.Save()) Then
   /* save didn’t complete */
   &COLL = &MYSESSION.PSMessages;
   For &I = 1 to &COLL.Count
      &ERROR = &COLL.Item(&I);
      &TEXT = &ERROR.Text;
      /* do error processing */
   End-For;
   &COLL.DeleteAll();
End-if;
  1. Get a session object.

    Before you can get a Component Interface, you have to get a session object. The session controls access to the Component Interface, provides error tracing, enables you to set the runtime environment, and so on.

    &MYSESSION = %Session;
  2. Get a Component Interface.

    Use the GetCompIntfc method with a session object to get the Component Interface. You must specify a Component Interface definition that has already been created. You receive a runtime error if you specify a Component Interface that doesn’t exist.

    &MYCI = &MYSESSION.GetCompIntfc(COMPINTFC.EXPRESS);

    After you execute the GetCompIntfc method, you have only the structure of the Component Interface. You haven’t populated the Component Interface with data yet.

  3. Set the CREATEKEYS.

    These key values are required to open a new instance of the data. If the values you specify aren’t unique, that is, if an instance of the data already exists in the database with those key values, you receive a runtime error.

    &MYCI.BUSINESS_UNIT = "H01B";
    &MYCI.INTERNAL_FLG = "Y";
    &MYCI.ORDER_NO = "NEXT’;
  4. Create the instance of data for the Component Interface.

    After you set the key values, you must use the Create method to populate the Component Interface with the key values you set.

    &MYCI.Create();

    This creates an instance of this data. However, it hasn’t been saved to the database. You must use the Save method before the instance of data is committed to the database.

  5. Set the rest of the fields.

    Assign values to the other fields.

    &MYCI.CUSTOMER = "John’s Chicken Shack";
    &MYCI.LOCATION = "H10B6987";
    .
    .
    .

    If you have specified InteractiveMode as True, every time you assign a value or use the InsertItem or DeleteItem methods, any PeopleCode programs associated with that field (either with record field or the component record field) fires (FieldEdit, FieldChange, RowInsert, and so on.)

  6. Save the data.

    When you execute the Save method, the new instance of the data is saved to the database.

    If NOT(&MYCI.Save()) Then

    The Save method returns a Boolean value: True if the save was successful, False otherwise. You can use this value to do error checking.

    The standard PeopleSoft save business rules (that is, any PeopleCode programs associated with SaveEdit, SavePreChange, WorkFlow, and so on.) are executed. If you didn’t specify the Component Interface to run in interactive mode, FieldEdit, FieldChange, and so on, also run at this time for all fields that had values set.

    Note:

    If you’re running a Component Interface from an Application Engine program, the data won’t actually be committed to the database until the Application Engine program performs a COMMIT.

  7. Check Errors.

    You can check if there were any errors using the PSMessages property on the session object.

    If NOT(&MYCI.Save()) Then
       /* save didn’t complete */
       &COLL = &MYSESSION.PSMessages;
       For &I = 1 to &COLL.Count
          &ERROR = &COLL.Item(&I);
          &TEXT = &ERROR.Text;
          /* do error processing */
       End-For;
       &COLL.DeleteAll();
    End-if;

    If there are multiple errors, all errors are logged to the PSMessages collection, not just the first occurrence of an error. As you correct each error, delete it from the PSMessages collection.

    The Text property of the PSMessage returns the text of the error message. At the end of this text is a contextual string that contains the name of the field that generated the error. The contextual string has the following syntax:

    {BusinessComponentName.[CollectionName(Row).[CollectionName(Row).[CollectionName⇒
    (Row)]]].PropertyName}

    For example, if you specified the incorrect format for a date field of the Component Interface named ABS_HIST, the Text property would contain the following string:

    Invalid Date {ABS_HIST.BEGIN_DT} (90), (1)

    The contextual string (by itself) is available using the Source property of the PSMessage.

    Note:

    If you’ve called your Component Interface from an Application Engine program, all errors are also logged in the Application Engine error log tables.

    See Error Handling, Source property: PSMessage class.