This is a public Forum  publicRSS

Topic

    JJ
    CPM - Validation failed on create of a Incident
    Topic posted December 7, 2017 by JJPro, last edited December 7, 2017 
    37 Views, 21 Comments
    Title:
    CPM - Validation failed on create of a Incident
    Content:

    Please see image for the error and the code block for the code - any reason for this?

    Code Block:

    Answer

    • Gursimran Singh Saini

      Are you able to execute this block of code in a custom script?

      Also, maybe you should use SuppressAll with Incident save under ActionUpdate.

    • JJ

      Yes - if I dump into a model and call it via a controller - it creates an incident just fine against my name.

      Added SuppressAll and the error still happens

    • JJ

    • Suresh Thirukoti

      Unless you have specific reason, why not return true from Validate() method in Test Harness...

      ~Suresh

    • Venkateswar Bollapalli

      You are creating two incidents in setup(). In the validate() method, you may have to check the CTC for both Incidents.

      Something like

      if (assert($incident->CustomFields->CTU->CTC == 1265) || assert($incident->CustomFields->CTU->CTC == 1264))

                  {

                      echo "Create test passed\n";
                      return true;
                  }
                  else
                  {
                      echo "Create test FAILED\n";
                  }           

       

       

       

    • JJ

      I still get the same error - the error is on creation of the test incidents

      Strangely, the line number points to an empty line

    • JJ

      This error seems to be around the following line. Removing it moves to an error further down the page but it's one that I think relates to this line anyway

      self::$incidentOneId = $incidentOne->ID;

      If I change it to the following, the above error disappears but I then get this

      $incidentOneId = $incidentOne->ID;

       

    • Venkateswar Bollapalli

      I just tried this in a test site.

      When I tested the CPM directly without creating system attribute (UTC->CTC), I saw the same error that you posted here ( 'Validation failed on create of a Incident' ).

      But when I tested the same CPM after creating the system attribute, I am not seeing the error. The test harness passed.

      So, I believe, the error is with the line '$incidentOne->CustomFields->CTU->CTC = 1264' trying to set value to an attribute that doesn't exist in the site.

      Please verify that the 'CTU->CTC' of type Integer exists on Incident in the site that you are testing.

      Regards, Venkat B

       

       

    • JJ

      I copied the example from http://cxdeveloper.com/article/cpms-101-introduction-custom-process-models - this worked fine

      Changed all references from contact to incident and it falls over. Absolutely stumped

      I made sure I gave it a primary contact via

      $cont = RNCPHP\Contact::fetch(123495);
      $incidentOne->PrimaryContact = $cont;

      $cont = RNCPHP\Contact::fetch(123495);
      $incidentTwo->PrimaryContact = $cont;

    • JJ

      This is what I get looking at the type - but even if I remove that line where it saves, it still errors

      CTU.CTC is a custom object with a relationship to the Incident table

    • JJ

      Here is my basic code

      <?php

      /**
       * CPMObjectEventHandler: MinimalIncidentHandler
       * Package: RN
       * Objects: Incident
       * Actions: Create, Update
       * Version: 1.3
       * Purpose: Minimal CPM handler for incident create and update.
       */
      use \RightNow\Connect\v1_3 as RNCPHP;
      use \RightNow\CPM\v1 as RNCPM;

      /**
       * Handler class for CPM
       */
      class MinimalIncidentHandler implements RNCPM\ObjectEventHandler
      {

          /**
           * Apply CPM logic to object.
           * @param int $runMode
           * @param int $action
           * @param object $incident
           * @param int $cycles
           */
          public static function apply($runMode, $action, $incident, $cycle)
          {
              if ($cycle !== 0) return;
              
              if (RNCPM\ActionUpdate == $action)
              {
                  $incident->save();
              }
              elseif (RNCPM\ActionCreate == $action)
              {
                  $incident->save();
              }
          }

      }

      /**
       * CPM test harness
       */
      class MinimalIncidentHandler_TestHarness
              implements RNCPM\ObjectEventHandler_TestHarness
      {

          static $incidentOneId = null,
                  $incidentTwoId = null;

          /**
           * Set up test cases.
           */
          public static function setup()
          {
              // First test
              $incidentOne = new RNCPHP\Incident;
              $cont = RNCPHP\Contact::fetch(38467);
              $incidentOne->PrimaryContact = $cont;        
              $incidentOne->Subject = '123456';        
              $incidentOne->save();
              self::$incidentOneId = $incidentOne->ID;

              // Second test
              $incidentTwo = new RNCPHP\Incident;
              $cont = RNCPHP\Contact::fetch(38467);
              $incidentTwo->PrimaryContact = $cont; 
              $incidentTwo->Subject = '654321';   
              $incidentTwo->save();
              self::$incidentTwoId = $incidentTwo->ID;
          }

          /**
           * 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.
           * @param int $action
           * @param class $object_type
           * @return object | array
           */
          public static function fetchObject($action, $object_type)
          {
              $incidentOne = $object_type::fetch(self::$incidentOneId);
              $incidentTwo = $object_type::fetch(self::$incidentTwoId);
              return array($incidentOne, $incidentTwo);
          }

          /**
           * Validate test cases
           * @param int $action
           * @param object $incident
           * @return bool
           */
          public static function validate($action, $incident)
          {
              if (RNCPM\ActionUpdate == $action)
              {
                  if (assert($incident->Subject== "123456"))
                  {
                      echo "Update test passed\n";
                      return true;
                  }
                  else
                  {
                      echo "Update test FAILED\n";
                  }
              }
              elseif (RNCPM\ActionCreate == $action)
              {
                  if (assert($incident->Subject == "654321"))
                  {
                      echo "Create test passed\n";
                      return true;
                  }
                  else
                  {
                      echo "Create test FAILED\n";
                  }
              }
              else
              {
                  echo "Invalid action";
              }
              return false;
          }

          /**
           * Destroy every object created 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.
           */
          public static function cleanup()
          {
              if (self::$incidentOneId)
              {
                  $incidentOne = RNCPHP\Incident::fetch(self::$incidentOneId);
                  $incidentOne->destroy();
                  self::$incidentOneId = null;
              }

              if (self::$incidentTwoId)
              {
                  $incidentTwo = RNCPHP\Incident::fetch(self::$incidentTwoId);
                  $incidentTwo->destroy();
                  self::$incidentTwoId = null;
              }
          }

      }

      ?>

    • Venkateswar Bollapalli

      Validate() method will be tested for both create and update actions. Please match $Incident->Subject that is being validated in the validate method.

      I updated your code with the following lines and tested. It worked.

      $incidentOne->Subject = '123456'; 

      $incidentTwo->Subject = '123456';  

      if (assert($incident->Subject== "123456"))

      if (assert($incident->Subject == "123456"))

      Pls give a try.

      Regards, Venkat B

       

    • JJ

      When saving the custom field below, it doesn't.

      $incidentOne->CustomFields->CTU->CTC = 1264;

      Back story - there is a custom object called CTU.CTC which has the relationship set as below. This is where the custom field in the incident has come from. With this in mind - is there anything else I need to have done?

    • Suresh Thirukoti

      Then its Foreign Key so it should be like $incidentOne->CustomFields->CTU->CTC = RNCPHP\CTU\CTC::fetch(1264);

      ~Suresh

    • JJ

      The error is still in the apply() function despite using RNCPHP\CTU\CTC::fetch(1264);

       if (RNCPM\ActionUpdate == $action)
              {
                  try{
                      $incident->CustomFields->CTU->CTC = RNCPHP\CTU\CTC::fetch(1264);
                      $incident->save();
                  }
                  catch (Exception $err ){
                      echo $err->getMessage();
                  }
              }
              elseif (RNCPM\ActionCreate == $action)
              {
                  try{
                      $incident->CustomFields->CTU->CTC = RNCPHP\CTU\CTC::fetch(1264);
                      $incident->save();
                  }
                  catch (Exception $err ){
                      echo $err->getMessage();
                  }
              }