General Technical Discussions for Service

Get Involved. Join the Conversation.

Topic

    Prasad Mokkapati
    I write below simple code in CPM to copy CC email address...Answered
    Topic posted October 28, 2015 by Prasad MokkapatiBlue Ribbon: 750+ Points 
    396 Views, 10 Comments
    Title:
    I write below simple code in CPM to copy CC email address from email header to CC field at incident....
    Content:

    I write below simple code in CPM to copy CC email address from email header to CC field at incident. at the time updating the incident this code is triggering 4 times at a time.means code is updating CC field with  prasad.mokkapati@gmail.com;prasad.mokkapati@gmail.com;prasad.mokkapati@gmail.com;prasad.mokkapati@gmail.com; instead of prasad.mokkapati@gmail.com

    below is the code

     

    <?
    /*
     * CPMObjectEventHandler: CaptureCC
     * Package: RN
     * Objects: Incident
     * Actions: Create, Update
     * Version: 1.2
     */
    use \RightNow\Connect\v1_2 as RNCPHP;

    use \RightNow\CPM\v1 as RNCPM;

    class CaptureCC
            implements RNCPM\ObjectEventHandler
    {
        public static function apply( $run_mode, $action, $obj, $n_cycles )

           $mailHeader=$obj->Threads[0]->MailHeader;
            
            $startPos=strpos($mailHeader,"CC:") +3; 
            
            $endPos=strpos($mailHeader,"Subject:",$startPos);
            $CCMails = substr($mailHeader,$startPos,($endPos-$startPos ));        
            $keyword = '>';
            $CCArray = explode($keyword, $CCMails);
            
            $CCMails="";
            $IncCC = $obj->CustomFields->c->alternateemail;
            $SplitKey = ';';
            $SplitIncCC = explode($SplitKey, $IncCC);
            $SplitCount = count($SplitIncCC);
            for ($x = 0; $x < count($CCArray)-1; $x++)
            {
            $posStart=strpos($CCArray[$x],"<") +1;

            $CCMail = substr($CCArray[$x],$posStart);
            $IncCC.=$CCMail.";";

            }
        
            $obj->CustomFields->c->alternateemail = $IncCC;
            $obj-> save();

            }
    }

    Can some one help me how to restrict this to execute multiple times.

     

    Thanks

    Prasad

    Best Comment

    Anurag Mittal

    Prasad,

    Custom fields are not available in the $obj object if it wasn't edited during the save/update that caused the custom process event to be fired. In your case CustomFields->c->alternateemail doesn't get modified when TechMail update your incident that's why $obj returns Null value.

    You have to use ROQL to fetch value of this custom fields. Look at this post for more understanding.

    ~Anurag

    Comment

     

    • Anurag Mittal

      Use $obj->save(RNCPHP\RNObject::SuppressAll) to stop CPM enter into self loop.

    • Bhagwan Singh Mer



      Prasad,



      Check the cpm code provided by Bishnu here. In the same post you can find OOTB way through which this requirement can be met through report as display



      Thanks,

      MB

    • Narendra Muttineni

      Use $obj->save(RNCPHP\RNObject::SuppressAll) to stop CPM enter into self loop. 

      and Use if($n_cycles !== 0) return; statement at the starting to stop the multiple recursive nature and check to see the Can Suppress check box is checked or not. if not please check the check box in Process Designer.

      Regards,

      Narendra 

    • Prasad Mokkapati

      now i am facing another problem here.

      i have 2 handlers for CC, one is for getting CC: at the time incident creation, second one for incident update to check CC: again in case of reply to same thread and update new CC: address at incident.

      problem is when incident is creating through email (TechMail) these both handlers are executing.

      can you please help me on how to restrict this execution.

      Thanks

      Prasad

       

    • Narendra Muttineni

      As per my knowledge, the create methode should be execute at the time of Incident creation. but you are saying it is executing both the methods. In this case, we can restrict it by using Rules. If you are using new version of RightNow, from rules you can call Object Event Handlers when the Incident is created.

      Regards,

      Narendra

    • Prasad Mokkapati

      Thanks Narendra,



      it was working for me, i found one strange behaviour in getting CC emails address from email header while updating Incident (reply to same thread). i wrote below code to get CC email address when incident is updating. here i will check incident header CC with Incident CC field, if any difference found then that new CC email will update at incident means append new address at incident level.below code is working as expected when updating record from workspace but same is not working from TechMail.



      means: incident CC have : test@gmail.com;test@yahoo.com;



      now i reply to same thread with cc:test@gmail.com



      result is: Incident CC is test@gmail.com instead of  test@gmail.com;test@yahoo.com; 



      but same is working from incident workspace record. can you please help where i am doing wrong.



       



      <?

      /*

       * CPMObjectEventHandler: CaptureCCUpdate

       * Package: RN

       * Objects: Incident

       * Actions: Update

       * Version: 1.2

       */

      use \RightNow\Connect\v1_2 as RNCPHP;



      use \RightNow\CPM\v1 as RNCPM;



      class CaptureCCUpdate

              implements RNCPM\ObjectEventHandler

      {

          public static function apply( $run_mode, $action, $obj, $n_cycles )

          {    

          if (RNCPM\ActionUpdate == $action)//Update operation for CC Part

          {

          $CCMail="";    

          $IncCCs = "";

              $mailHeader=$obj->Threads[0]->MailHeader;        

              $startPos=strpos($mailHeader,"CC:") +3;         

              $endPos=strpos($mailHeader,"Subject:",$startPos);

              $CCMails = substr($mailHeader,$startPos,($endPos-$startPos ));        

              $keyword = '>';

              $CCArray = explode($keyword, $CCMails);    

              $CCMails="";    

          $IncCC = $obj->CustomFields->c->alternateemail;

              $SplitIncCC = explode(';', $IncCC);

              $SplitCount = count($SplitIncCC);



              for ($x = 0; $x < count($CCArray)-1; $x++)

              {    

                  $c = 0;

                  $Start1=strpos($CCArray[$x],"<") +1;

                  $Val1 = substr($CCArray[$x],$Start1);

                      for ($i = 0; $i < $SplitCount-1; $i++)

                          {    

                              //val2

                              $Start=strpos($SplitIncCC[$i],"<") +1;

                              $Val2 = substr($SplitIncCC[$i],$Start2);

                              $string1 = strtolower($Val1);

                              $string2 = strtolower($Val2);                    

                              if(strcmp(trim($string1),trim($string2)) != 0)

                              {                    

                              ++$c;

                              }

                          }

                          if($c == $SplitCount-1)

                          {

                          $posStart=strpos($CCArray[$x],"<") +1;

                          $CCMails = substr($CCArray[$x],$posStart);

                          $CCMail.=$CCMails.";";

                          }            

                      }

              

              $IncCCs=$IncCC.$CCMail;

              $obj->Subject = $CCMails;

              $obj->CustomFields->c->alternateemail = $IncCCs;

              $obj-> save();



          }

              

      //return;        

          }

      }

    • Prasad Mokkapati

      in above code (using code in Incident CPM Update event handler i am using below line to get alternate email field value from incident.

      $IncCC = $obj->CustomFields->c->alternateemail;

      this piece of code is always returning null value when incident is updating through TechMail. 

      Can you please help why this is happening and how to resolve it.

      Thanks

      Prasad

    • Anurag Mittal

      Prasad,

      Custom fields are not available in the $obj object if it wasn't edited during the save/update that caused the custom process event to be fired. In your case CustomFields->c->alternateemail doesn't get modified when TechMail update your incident that's why $obj returns Null value.

      You have to use ROQL to fetch value of this custom fields. Look at this post for more understanding.

      ~Anurag

    • Prasad Mokkapati

      Thanks Anurag, this post solved my problem.

    • Barrilito van Dijk

      Hi Prasad, can you mark Anurag's answer as best answer please so that people can see the answer was given. Thanks.