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

    • ilamukky

      Hello, I tried your code and the contact has been created... I used version 1.2 of Connect for PHP API

       * Version: 1.2
       */
      
      use \RightNow\Connect\v1_2 as RNCPHP;    // ver. 1.2 of Connect for PHP API
      use \RightNow\CPM\v1 as RNCPM;           // ver.1 interface of the Process designer
      

       

      I see the contact created, but without Actions available (no permission for Open, Delete, Print)

    • Barrilito van Dijk

      Hi ilamukky.

      Thank you very much more your effort, that is realy appreciated!

      You actually see the contact been created?  Can you recheck for me please that you are sure the contact did not already existed please. :)

      But if so, you can not open the contact or anything? So, you ended up with a contact which you can lookup, but you can not do aything with it?  Can you add this contact to an incident or is that not possible too?

       

    • Barrilito van Dijk

      ps Ilamukky, I forgot to ask, instead of using version 1.2, what is the outcome using version 1 or 1.1. ?

    • ilamukky

      Yes the contact has been newly created, it didn't exist before...

      in the meantime I resolved the issue,adding this part:

      $contact->CRMModules = new RNCPHP\CRMModules();
      $contact->CRMModules->Service = 1;
      

      to specify the Contact State (required field) and now contact is available for all operations.

      Let me know, I hope this will work for you

       

      Now I try with v.1... i will update later

    • ilamukky

      Yes, it is working also with Connect v.1.

      I noticed that: if the contact is new, it is created; if it is existing, it is created adding a number at the end of the email.. in your example, example@example.com and example@example.com.0001 have been both created

      EDIT: another thing! If the contact is already existing during the test harness, I receive an error. Instead, if it is existing when CP runs, the behaviour is as said above. 

    • Barrilito van Dijk

      Hi Ilamukky,

      Unfortunately it is still not working, I am trying hard here... :)

      ps. the added number 0001 is default behaviour if you create a contact by Connect PHP if it exists...

      I will get back to you in a while if I have some more test results here. Thanks!

    • Barrilito van Dijk

      Hi Ilamukky,

      My conclusion is that the code runs well until the point where I save the contact.

      I tried:

      $contact->save(RNCPHP\RNObject::SuppressAll);

      or

      $contact->save();

      But it breaks at this point. Any suggestions???

    • ilamukky

      What error do you see? Do you get the error running the code or in test harness?

      I didn't place the final "return;" command

    • Barrilito van Dijk

      Hi, I don't see ANY errors, that is the downside of this all. I will try it without the return command. I am offline for about a little hour now, but when I am back I will dive into it again. Is it possible for you to show your full code perhaps? I will get back to you in about an hour.

      Thanks Ilamukky!

    • ilamukky

      Ok!! Here my code (without Test Harness)

       

      <?
      /**
       * CPMObjectEventHandler: TestCP
       * Package: RN
       * Objects: Incident
       * Actions: Create
       * Version: 1.1  
       */

      use \RightNow\Connect\v1_1 as RNCPHP; 
      use \RightNow\CPM\v1 as RNCPM;     


      /**
       * -------------------------- Handler class for CPM --------------------------
       */
      class TestCP implements RNCPM\ObjectEventHandler
      {

          public static function apply($runMode, $action, $incident, $cycle)
          {
              if ($cycle !== 0) return;
               
              if (RNCPM\ActionCreate == $action)  
              {
                      $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 = "example7@example.com";
                      
                      $contact->Login = "test_cp";
                      $contact->Name = new RNCPHP\PersonName();
                      $contact->Name->First = "FirstName";
                      $contact->Name->Last = "LastName";
                      
                      $contact->CRMModules->Service = 1;

                      $contact->save();

              }
          }
      }

    • Barrilito van Dijk

      Hi,

      Bad news: it still does not work, it keeps on hanging on the save action. Even when I copy your code. I can save the incident, for example if I change the cubject and save it, but I can not make a contact and save it... as if I don't have the rights. I will look further... If I can't find it I guess I will send in a service request at Oracle...

    • Barrilito van Dijk

      ps Ilamukky, on what version are you testing this? I am using feb15.

    • ilamukky

      November 2012 ... we are migrating to Feb2015

    • Barrilito van Dijk

      Nov12, that is an oldy cheeky haha.

      Too bad that we are on such different versions. I guess I have to send a service request to my Oracle friends as I have no other ideas wink

      Thank you very much for all your effort Ilamukky, I appreciate it a lot!!

    • ilamukky

      Ok!! Let us know if you resolve