Example of an Object Event Handler Script

This example object event handler script adds a note to an organization when it is created in the Oracle database.

The types of objects and actions with which the script can function are indicated at the beginning of the script. The required test harness code can be found approximately halfway through the script.

Note: An additional sample script that uses client for URLs (cURL) to perform an action in an external database is provided in the Connect PHP API Developer Guide.

    <?
    /*
     * CPMObjectEventHandler: organization_create
     * Package: OracleServiceCloud
     * Objects: Organization
     * Actions: Create
     * Version: 1.1
     */
    // This object procedure binds to v1_1 of the Connect PHP API
    use \RightNow\Connect\v1_1 as RNCPHP;
    // This object procedure binds to the v1 interface of the process designer
    use \RightNow\CPM\v1 as RNCPM;
    /**
     * An Object Event Handler must provide two classes:
     *   - One with the same name as the CPMObjectEventHandler tag
     *     above that implements the ObjectEventHandler interface.
     *   - And one of the same name with a "_TestHarness" suffix
     *     that implements the ObjectEventHandler_TestHarness interface.
     *
     * Each method must have an implementation.
     */
    class organization_create
            implements RNCPM\ObjectEventHandler
    {
        public static function
        apply( $run_mode, $action, $obj, $n_cycles )
        {
            $note = new RNCPHP\Note;
            $note->Text = "\n".date(DATE_RSS)."\n"
                ."\nA note added by " . __CLASS__
                . '::' . __FUNCTION__ . "\n";
            if (is_null($obj->Notes))
            {
                $obj->Notes = new RNCPHP\NoteArray;
            }
            $obj->Notes[] = $note;
            $obj->Name = preg_replace( '/^[aA]n* /', 'The ', $obj->Name );
                         
            // Do not suppress because we want the update flow to run.
            $obj->save();
            return;
        } // apply()
    } // class organization_create
    /*
        The Test Harness
    */
    class organization_create_TestHarness
            implements RNCPM\ObjectEventHandler_TestHarness
    {
        static $org_invented = NULL;
        public static function setup()
        {
            // For this test, create a new organization as expected.
            $org = new RNCPHP\Organization;
            $org->Name = "An organization name";
            $org->Login = "a login";
            static::$org_invented = $org;
            return;
        }
        public static function
        fetchObject( $action, $object_type )
        {
            // Return the object that we want to test with.
            // You could also return an array of objects
            // to test more than one variation of an object.
            return(static::$org_invented);
        }
        public static function
        validate( $action, $object )
        {
            // Add one note.
            return(count($object->Notes) === 1);
        }
        public static function cleanup()
        {
            // Destroy every object invented by this test.
            // Not necessary since in test
            // mode and nothing is committed,
            // but good practice if only to
            // document the side effects of this test.
            static::$org_invented->destroy().
            static::$org_invented = NULL;
            return;
        }
    }