Integrations and APIs for Service

Get Involved. Join the Conversation.

Topic

    Earl Taylor
    Synchronizing the SLA field with a Custom Object
    Topic posted December 11, 2018 by Earl Taylor, tagged Connect PHP, Custom Process Models - CPMs 
    26 Views
    Title:
    Synchronizing the SLA field with a Custom Object
    Summary:
    We want to change an existing script to sync with the SLA container for the orgs table
    Content:

    We have a script that updates our Custom Object fields from the Organization table. This has been working great, but now we need to add a field on the Custom Object and have the SLA container object synch with this new field. this new field is used to identify end user support status, when a partner opens an incident for the end user.

    We were hoping to just be able to add a line of code in this process to do this. The new field name in the CompanyName$CN Custom Object is "sup_tier"

    We have the SLA Container showing in the Oragnization Work space, but the value displayed is not actually present in the orgs table itself.

    Does anyone have an idea how this can be done? The SLA tables are a pain to deal with as it is. Any help would be appreciated.

    The current code is as follows:

    <?php

    /*
    * CPMObjectEventHandler: organization_create_update
    * Package: OracleServiceCloud
    * Objects: Organization
    * Actions: Create, Update
    * Version: 1.2
    * Purpose: Pushing Org data to CompanyName$CN CBO
    */

    // This object procedure binds to v1_2 of the Connect PHP API
    use \RightNow\Connect\v1_2 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_update implements RNCPM\ObjectEventHandler
    {
        public static function apply($run_mode, $action, $obj, $n_cycles)
        {
            $debug = true;
           
                self::debugJsOutput($debug, $obj->ID, "***********\n");
                self::debugJsOutput($debug, $obj->ID, "***********\n");
                self::debugJsOutput($debug, $obj->ID, date("Y-m-d H:i:s") . "\n");
                self::debugJsOutput($debug, $obj->ID, "***********\n");
                self::debugJsOutput($debug, $obj->ID, "- " . __CLASS__ . "::" . __FUNCTION__ . "\n");
                self::debugJsOutput($debug, $obj->ID, "-- Action: " . ( $action === RNCPM\ActionUpdate ? "Update" : "Create" ) . "\n");
                self::debugJsOutput($debug, $obj->ID, "-- Cycle number: " . $n_cycles . "\n");
                self::debugJsOutput($debug, $obj->ID, "-- run_mode: " . $run_mode . "\n");
               
                if($run_mode == 1)
                {
                    $obj = RNCPHP\Organization::fetch($obj->ID);
                    self::debugJsOutput($debug, $obj->ID, "-- LookupName = " . $obj->LookupName . "\n");
                   
                    if(!is_null($obj->CustomFields->CompanyName->CN))
                    {
                        $CN = RNCPHP\CompanyName\CN::fetch($obj->CustomFields->CompanyName->CN->ID);
                        self::debugJsOutput($debug, $obj->ID, "-- CN = " . $obj->CustomFields->CompanyName->CN->ID . "\n");
                    }
                    else
                    {
                        $CN = new RNCPHP\CompanyName\CN();
                    }
                     
        if(!is_null($obj->CustomFields->c->sfdc_acctno))
                    {
                        $CN->SFDC_no = $obj->CustomFields->c->sfdc_acctno;
                    }
                    else
                    {
                        $CN->SFDC_no = 'FAKE-' . rand(100000,999999);
                    }
                       
                    $CN->Comp_name = $obj->LookupName;
        $CN->save();
                    self::debugJsOutput($debug, $obj->ID, "-- CN ID = " . $CN->ID . "\n");
                   
                    $md = $obj::getMetadata();
                    $cf_type_name = $md->CustomFields->type_name;
                    $md2 = $cf_type_name::getMetadata();
                    $obj->CustomFields->CompanyName = new $md2->CompanyName->type_name;
                    $obj->CustomFields->CompanyName->CN = $CN->ID;
                    //$obj->save();
        $obj->save(RNCPHP\RNObject::SuppressExternalEvents);
                   
                    self::debugJsOutput($debug, $obj->ID, "-- Saved.\n");
        
        $obj = RNCPHP\Organization::fetch($obj->ID);
                    self::debugJsOutput($debug, $obj->ID, "-- LookupName = " . $obj->LookupName . "\n");
                   
                }
           
            try
            {
                return true;
            }
            catch(Exception $e)
            {
                self::debugJsOutput($debug, $obj->ID, "-- Exception: " . $e->getMessage() . "\n");
            }
        } // apply()
       
        private static function debugJsOutput($debug, $id, $data)
        {
            if(!is_dir("/tmp/CompanyName"))
                mkdir("/tmp/CompanyName");
           
            ob_start();
           
            echo 'Org' . $id . ' ';
           
            if(is_object($data))
            {
                echo "<pre>";
                print_r($data);
                echo "</pre>";
            }
            else
            {
                echo $data;
            }
           
            if($debug)
            {
                echo ob_get_clean();
            }
            else
            {
                file_put_contents("/tmp/CompanyName/cpm_organization_" . date("Ymd") . ".log", ob_get_clean(), FILE_APPEND);
            }
      
        }
    } // class organization_create


    /*
    The Test Harness
    */
    class organization_create_update_TestHarness implements RNCPM\ObjectEventHandler_TestHarness
    {
        static $org_invented = NULL;
        static $CN_invented = NULL;
     
        public static function setup()
        {
            //$CN = new RNCPHP\CompanyName\CN;
      $org = new RNCPHP\Organization;
            static::$org_invented = $org;
            //static::$CN_invented = $CN;
            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::$CN_invented);
      return(static::$org_invented);
        }

        public static function
        validate( $action, $object )
        {
            #return(!is_null(static::$CN_invented->ID));
            return $object;
        }

        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.
            return;
        }
    }

     

    Thanks

    Earl Taylor

    Version:
    Feb 2018 version