General Technical Discussions for Service

Get Involved. Join the Conversation.

Topic

    Bishnu Paudel
    Related incident not saved from Custom process event handler
    Topic posted April 23, 2014 by Bishnu PaudelSilver Trophy: 7,500+ Points, last edited April 24, 2014 
    465 Views, 12 Comments
    Title:
    Related incident not saved from Custom process event handler
    Content:

    hi all,

    I have a custom event handler for update event of a CUSTOM OBJECT which is related to incident object and will always have one incident per record. My script is simply trying to update the subject of related incident to some static text. The updated subject is accessible from rest of the code but it's not being saved in the server. I can't figure out what am I missing. Please help.

    Thanks,

    Bishnu

     

    Code Snippet:

    Comment

     

    • Varun Chandran

      Bishnu,

      What do you mean by non persistent in the server? Do you mean that the CPM is not firing on incident update?

      You need to assign the event handler to the RN incident object's Update action and deploy the custom process inorder to get it fired on incident update. While testing, the object will not be updated.

      Hope this helps

      Best

      Varun

       

    • Bishnu Paudel

      Hi Varun,

      Sorry, I should have been clear there. Not sure why I used the word 'persistent' :).

      I mean the inicdent is not saved at all. In other words , the subject of the incident is unchanged even after the custom process is executed. I can see the event is firing and the process is doing other things but just that inicdent is not updated.

      I hope it makes sense.

      Thanks,

      Bishnu

    • Varun Chandran

      Bishnu,

      I am not sure why you are using 

      $obj->incident_id->Subject instead of $obj->Subject as $obj will be the incident object.

      try 

      public static function apply( $run_mode, $action, $obj, $n_cycles )
          { 
           if($action==2){                   //update =2
              $subject="test subject";
              $inc = RNCPHP\Incident::fetch($obj->ID);       
              $inc->Subject=$subject;
              $inc->save(); 
            }         
      return ;
          }
       
      Best
       
      Varun
    • Bishnu Paudel

      Hi Varun,

      Sorry for the confusion again. if you look at my original question, I have mentioned that I am writing custom process for 'update' event of a custom object which will always be related to only one incident object.

      Thanks,

    • Varun Chandran

      I missed that part actually.. :)

      So you have to change the subject of the incident which is related to the custom object on its update. Correct me if i am wrong.

    • Bishnu Paudel

      Yes ! Exactly. :)

    • Varun Chandran

      If you have to change incident subject, you will have to access the incident object for that. You will have to follow the same procedure, in place of 

      $inc = RNCPHP\Incident::fetch($obj->ID);

      use  

      $inc = RNCPHP\Incident::fetch(<incident_id from custom_object>);  the rest is the same.    

       

       

    • Bishnu Paudel

      Thank you for you inputs Varun. I appreciate that. 

      I have already tried this and it's not working which is driving me crazy. To avoid the confusion, I only have one STudyPlanReuest custom object record and I am using the same for testing and updating. The results of var_dump are as expected.

      Here is my code :

      <?
      /*
       * CPMObjectEventHandler: StudyPlanRequestUpdate
       * Package: CO
       * Objects: CO\StudyPlanRequest
       * Actions: Update
       * Version: 1.2
       */
       
       //Purpose of the script: Automatically create a new incident of type 'study plan' and bind it with newly created StudyPlanRequest record.
       
      // This object procedure binds to v1_1 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 StudyPlanRequestUpdate
              implements RNCPM\ObjectEventHandler
      {
          public static function apply( $run_mode, $action, $obj, $n_cycles )
          { 
      $subject="test subject";
              $inc = RNCPHP\Incident::fetch($obj->incident_id->ID); 
      var_dump($inc->Subject);
              $inc->Subject=$subject;
      var_dump($inc->Subject);
              $inc->save(); 
       
      return ;
      }
       
       class StudyPlanRequestUpdate_TestHarness
              implements RNCPM\ObjectEventHandler_TestHarness
      {
          static $org_invented = NULL;
          public static function setup()
          {
        $studyplanform_id="184";
      $studyplanform = RNCPHP\CO\StudyPlanRequest::fetch($studyplanform_id);
             static::$org_invented = $studyplanform;
              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(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::$org_invented->destroy().
              static::$org_invented = NULL;
              return;
          }
      }

       

    • Bishnu Paudel

      I think fetching of incident object is not necessary because $obj->incident_id already refers to an incident object. But this didn't help at all.

    • Varun Chandran

      Wrap the apply function inside class StudyPlanRequestUpdate and try once.

      //class StudyPlanRequestUpdate starts here
      class StudyPlanRequestUpdate
              implements RNCPM\ObjectEventHandler
      {
          public static function apply( $run_mode, $action, $obj, $n_cycles )
          { 
      $subject="test subject";
              $inc = RNCPHP\Incident::fetch($obj->incident_id->ID); 
      var_dump($inc->Subject);
              $inc->Subject=$subject;
      var_dump($inc->Subject);
              $inc->save(); 
       
      return ;
      }
      }
      //class StudyPlanRequestUpdate ends here
       
      //class StudyPlanRequestUpdate_TestHarness starts here
      class StudyPlanRequestUpdate_TestHarness
              implements RNCPM\ObjectEventHandler_TestHarness
      {
      /*Code block*/
      }

       

    • Bishnu Paudel

      Hi Varun,

      The apply function is wrapped inside the class in my code. I have other functions as well so I missed the closing "}" for class when I copied the apply function.

      Thanks,

    • Bishnu Paudel

      What a shame! my handler was not bound to the custom object at all. Sorry I wasted your time.