Integrations and APIs for Service

Get Involved. Join the Conversation.

Topic

    Barrilito van Dijk
    How to add a contact via an incident_create Custom procesAnswered
    Topic posted March 18, 2015 by Barrilito van DijkBlack Diamond: 60,000+ Points 
    1007 Views, 18 Comments
    Title:
    How to add a contact via an incident_create Custom proces
    Content:

    Hi all,

    Anyone familiair adding a contact via an incident_create custom process? Running and triggering the process is not an issue, I can do anything in it I want, but adding a  new contact does not seem to work, the entire script hangs.

    See the attached code block for the main code used.

    Anyone know how to do this or why this should not be possible?

    Thank you in advance.

    Version:
    Nov14
    Code Snippet:

    Best Comment

    Barrilito van Dijk

    Hi Ilamukky and others,

    I handed the issue out to Oracle and have been helped well (thank you Shane!). The main issue seemed to be setting the type of the email primary contact to "0". I had tried that before and did not work / or make a difference. So, I think there were more issues. What I learned from it all is mainly that the code in the testing phase accepts only one contact. If the contact already exists while testing, then the code breaks. That is weird since creating the same contact with connect normally just appends a number like "0001, 0002, 0003 etc" to the contact. So, I think the error messages in the testphase are perhaps misleading so that you think it does not work, but actually there can be data like a contact that already exist and mess everything up. Anyway, Oracle pointed that out correctly which helped a lot.

    Here is the good working code for the FEB15 release. Enjoy!

     

    <?
    /**
     * CPMObjectEventHandler: TestCP
     * Package: RN
     * Objects: Incident
     * Actions: Create
     * Version: 1.1   // Connect for PHP version
     */
    use \RightNow\Connect\v1_1 as RNCPHP;    // ver. 1.1 of Connect for PHP API (same as Header before)
    use \RightNow\CPM\v1 as RNCPM;             // ver.1 interface of the Process designer


    /**
     * 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 TestCP implements RNCPM\ObjectEventHandler

    {

    public static function apply($runMode, $action, $incident, $cycle)
    {
     if($incident->Subject == "Test"){
      
      fwrite($f, "We got past Test\n\n");
              if ($cycle !== 0) return;
            
            if (RNCPM\ActionCreate === $action) 
            {
       
       try {
                 $contact = new RNCPHP\Contact();
                   
                    $contact->Emails = new RNCPHP\EmailArray();
        $contact->Emails[0] = new RNCPHP\Email();
        $contact->Emails[0]->AddressType=new RNCPHP\NamedIDOptList();
        $contact->Emails[0]->AddressType->ID = 0;
        $contact->Emails[0]->Address = "example@example.com";
                 
                    $contact->Login = "test_cp";
                    $contact->Name = new RNCPHP\PersonName();
                    $contact->Name->First = "FirstName";
                    $contact->Name->Last = "LastName";
                   
                    $contact->CRMModules = new RNCPHP\CRMModules();
                    $contact->CRMModules->Service = 1;
                    $contact->save();
        
        $incident->PrimaryContact = $contact;  
        $incident->Subject = 'new subject';
        $incident->save(RNCPHP\RNObject::SuppressAll);
      
       } catch(Exception $e) {
        fwrite($f, $e->getMessage()."\n\n");
       }

            }


     }


    } // apply()

    } // class incident_update
    /*
     The Test Harness
    */
    class TestCP_TestHarness implements RNCPM\ObjectEventHandler_TestHarness {
     
     static $inc_invented = NULL;
     
     public static function setup() {
     

      $incident = new RNCPHP\Incident;
      $incident->Subject = "Test";
      static::$inc_invented = $incident;
      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::$inc_invented);
     }
     
     public static function validate( $action, $object ) {
      
      echo "It worked";
      return true;
     }
     
     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::$nieuwsbrief_invented->destroy().
      // static::$nieuwsbrief_invented = NULL;
      
      return;
     }
    }

    Comment

    • Barrilito van Dijk
       Offcourse, otherwise if you upgrade your code won't work as well :)
    • Barrilito van Dijk
       Offcourse, otherwise if you upgrade your code won't work as well :)
    • Barrilito van Dijk

      Hi Ilamukky and others,

      I handed the issue out to Oracle and have been helped well (thank you Shane!). The main issue seemed to be setting the type of the email primary contact to "0". I had tried that before and did not work / or make a difference. So, I think there were more issues. What I learned from it all is mainly that the code in the testing phase accepts only one contact. If the contact already exists while testing, then the code breaks. That is weird since creating the same contact with connect normally just appends a number like "0001, 0002, 0003 etc" to the contact. So, I think the error messages in the testphase are perhaps misleading so that you think it does not work, but actually there can be data like a contact that already exist and mess everything up. Anyway, Oracle pointed that out correctly which helped a lot.

      Here is the good working code for the FEB15 release. Enjoy!

       

      <?
      /**
       * CPMObjectEventHandler: TestCP
       * Package: RN
       * Objects: Incident
       * Actions: Create
       * Version: 1.1   // Connect for PHP version
       */
      use \RightNow\Connect\v1_1 as RNCPHP;    // ver. 1.1 of Connect for PHP API (same as Header before)
      use \RightNow\CPM\v1 as RNCPM;             // ver.1 interface of the Process designer


      /**
       * 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 TestCP implements RNCPM\ObjectEventHandler

      {

      public static function apply($runMode, $action, $incident, $cycle)
      {
       if($incident->Subject == "Test"){
        
        fwrite($f, "We got past Test\n\n");
                if ($cycle !== 0) return;
              
              if (RNCPM\ActionCreate === $action) 
              {
         
         try {
                   $contact = new RNCPHP\Contact();
                     
                      $contact->Emails = new RNCPHP\EmailArray();
          $contact->Emails[0] = new RNCPHP\Email();
          $contact->Emails[0]->AddressType=new RNCPHP\NamedIDOptList();
          $contact->Emails[0]->AddressType->ID = 0;
          $contact->Emails[0]->Address = "example@example.com";
                   
                      $contact->Login = "test_cp";
                      $contact->Name = new RNCPHP\PersonName();
                      $contact->Name->First = "FirstName";
                      $contact->Name->Last = "LastName";
                     
                      $contact->CRMModules = new RNCPHP\CRMModules();
                      $contact->CRMModules->Service = 1;
                      $contact->save();
          
          $incident->PrimaryContact = $contact;  
          $incident->Subject = 'new subject';
          $incident->save(RNCPHP\RNObject::SuppressAll);
        
         } catch(Exception $e) {
          fwrite($f, $e->getMessage()."\n\n");
         }

              }


       }


      } // apply()

      } // class incident_update
      /*
       The Test Harness
      */
      class TestCP_TestHarness implements RNCPM\ObjectEventHandler_TestHarness {
       
       static $inc_invented = NULL;
       
       public static function setup() {
       

        $incident = new RNCPHP\Incident;
        $incident->Subject = "Test";
        static::$inc_invented = $incident;
        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::$inc_invented);
       }
       
       public static function validate( $action, $object ) {
        
        echo "It worked";
        return true;
       }
       
       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::$nieuwsbrief_invented->destroy().
        // static::$nieuwsbrief_invented = NULL;
        
        return;
       }
      }