Customer Portal

Get Involved. Join the Conversation.

Topic

    eric montijo
    $hookData values refuse to display or be manipulated on...Answered
    Topic posted August 16, 2018 by eric montijoRed Ribbon: 250+ Points 
    78 Views, 3 Comments
    Title:
    $hookData values refuse to display or be manipulated on post_login
    Content:

    Hi everyone!

    Here's another hook data question. We have a need to set users with no organization set to a bucket organization of "forum use only". The ID for this organization in our test database is 13510. The code that I have should, theoretically, work, but I can't seem to figure out why it doesn't. The hook itself fires (see log message below), and it is pulling $hookData correctly (see print_r below), but for some reason when I go to actually use or manipulate those values in the code, it doesn't really do anything. Can anyone see where I'm going wrong?

    Hooks.php

     

    $rnHooks['post_login'] = array(
    'class' => 'ContactModel',
    'function' => 'setCommunityOrg',
    'filepath' => ''
    );
     
    ContactModel.php
    public function setCommunityOrg(&$hookData){
    $infodata = print_r($hookData, true);
    logmessage($infodata);
    logmessage("setCommunityOrg has fired. Contact = " . $hookData['contactID'] . ". Org ID = " . $hookData['orgID'] . ".");
    if ($hookData['orgID'] > 0) {
    logmessage("Organization ID was previously set. Org ID = " . $hookData['orgID']);
    } else {
        $hookData['orgID'] = 13510;
    logmessage("Organization ID is not set. Sucessfully changed Org ID to " . $hookData['orgID']);
    }
    }

    Debug Log Output:

    -------------------------------URI REQUEST: /ci/ajaxRequest/doLogin
    INTERFACE: datacore
    -------------------------------
    12:24:04 core/compatibility/optimized_includes.php::1578 Array
    (
    [returnValue] => RightNow\Libraries\ProfileData Object
    (
    [contactID] => 36280
    [login] => qom47885@bnuis.com
    [email] => qom47885@bnuis.com
    [firstName] => Fix
    [lastName] => Display Name Errors
    [disabled] => 
    [orgID] => 
    [orgLevel] => 
    [slai] => 
    [slac] => 
    [webAccess] => 
    [authToken] => [redacted]

    [ptaLoginUsed] => 
    [openLoginUsed] => 
    [loginStartTime] => 1534436643
    [forcefulLogoutTime] => 
    [socialUserID] => 
    [mapping:RightNow\Libraries\ProfileData:private] => Array
    (
    [c_id] => contactID
    [first_name] => firstName
    [last_name] => lastName
    [org_id] => orgID
    [web_access] => webAccess
    [cookie] => authToken
    [pta_login_used] => ptaLoginUsed
    [o_lvlN] => orgLevel
    [slai] => slai
    [slac] => slac
    [login] => login
    [email] => email
    [disabled] => disabled
    [openLoginUsed] => openLoginUsed
    [forcefulLogoutTime] => forcefulLogoutTime
    [loginStartTime] => loginStartTime
    [socialUserID] => socialUserID
    )

    )

    [data] => Array
    (
    [source] => LOCAL
    )

    )

    12:24:04 core/compatibility/optimized_includes.php::1578 setCommunityOrg has fired. Contact = . Org ID = .
    12:24:04 core/compatibility/optimized_includes.php::1578 Organization ID is not set. Sucessfully changed Org ID to 13510

    I've also tried using $hookData['data']->Organization->ID, but that returns nothing as well. Any help is greatly appreciated!

    Version:
    3.7 August 2017

    Best Comment

    eric montijo

    I finally got to manipulate the user's organization via the hook!

    It seems one of my first issues was that I forgot a colon (rookie move!) but another was that I was calling the hook data wrong. 

    I believe that the hookdata for pre_login and post_login use a different syntax for calling that data. I was finally able to successfully pull the contact's organization ID using $hookData['returnValue']->orgID. I was also able to pull the contact ID using the same syntax. This allowed me to use RNCPHP to fetch the contact and organization and set the fields correctly.

    Here's the code I used for the function for any weary travelers in the future:

     

    public function setCommunityOrg(&$hookData){
    $corg = $hookData['returnValue']->orgID;
    $contactID = $hookData['returnValue']->contactID;
    $contact = \RightNow\Connect\v1_3\Contact::fetch($contactID);
    logMessage("contact ID: " . $contactID . ". Org ID: " . $corg);
    if (isset($corg) && $corg != 13510) {
    logmessage("Organization ID was previously set. Org Name = " . $contact->Organization->Name);
    } else {
    $orgID = 13510;
    $contact->Organization = \RightNow\Connect\v1_3\Organization::fetch($orgID);
    $contact->save();
    logmessage("Organization ID is not set. Sucessfully changed Org ID to " . $contact->Organization->ID);
    }
    }

    Comment

     

    • Nisar

      Hi Eric,

      Could you try with the following?

      if ($hookData['orgID'] > 0) {
          logmessage("Organization ID was previously set. Org ID = " . $hookData['orgID']);
      } else {
          try {
              $orgID = 13510;
              $contact = \RightNow\Connect\v1_3\Contact::fetch($hookData['contactID']);
              $contact->Organization = \RightNow\Connect\v1_3\Organization::fetch($orgID);
              $contact->save();
              logMessage("Contact's organization has been set to ".$contact->Organization->ID);
          } catch (\Exception $err) {
              logMessage("Exception handling: ".$err->getMessage());
          }
      }
      

      I could be wrong, but I think that just updating the value of "$hookData['orgID']" won't suffice as it does not perform the assignment operation to the contact record.

    • eric montijo

      Great thinking, Nisar! Unfortunately, this code doesn't work. On account creation it errors out the form, and on login it stops the login before reload. It doesn't generate a debug log, either.

      Is there perhaps a security feature I'm overlooking since we're dealing with organization data? I'd set this in business rules, but I can't manipulate organization ID through rules.

      I'm all ears for an easier way to do this outside of hooks, as well :)

      THANK YOU!

    • eric montijo

      I finally got to manipulate the user's organization via the hook!

      It seems one of my first issues was that I forgot a colon (rookie move!) but another was that I was calling the hook data wrong. 

      I believe that the hookdata for pre_login and post_login use a different syntax for calling that data. I was finally able to successfully pull the contact's organization ID using $hookData['returnValue']->orgID. I was also able to pull the contact ID using the same syntax. This allowed me to use RNCPHP to fetch the contact and organization and set the fields correctly.

      Here's the code I used for the function for any weary travelers in the future:

       

      public function setCommunityOrg(&$hookData){
      $corg = $hookData['returnValue']->orgID;
      $contactID = $hookData['returnValue']->contactID;
      $contact = \RightNow\Connect\v1_3\Contact::fetch($contactID);
      logMessage("contact ID: " . $contactID . ". Org ID: " . $corg);
      if (isset($corg) && $corg != 13510) {
      logmessage("Organization ID was previously set. Org Name = " . $contact->Organization->Name);
      } else {
      $orgID = 13510;
      $contact->Organization = \RightNow\Connect\v1_3\Organization::fetch($orgID);
      $contact->save();
      logmessage("Organization ID is not set. Sucessfully changed Org ID to " . $contact->Organization->ID);
      }
      }