Comments

  • 1-10 of 10
  • eric montijo

    You could use JS to add an onsubmit or onclick event listener to the form or submit button. Here's some JS I've written from a project that might help. This is probably not plug-and-play code, but it should be a good start:

     

    <script>
    function y(){
    // grab the element needed
    var getcheckbox = document.getElementsByName("Contact.CustomFields.c.reg_from_community");
    // If you're using getElementsByName, remember it returns an array of all matching elements, so you may want to test to
    // see which element in the list it is. If you have a unique ID, you can more easily use getElementByID.
    var checkbox = getcheckbox[0];
    // these if statements determine if the browser should use addEventListener (most browsers) or attachEvent (predominantly IE)
    // in place of "myFunction" put the name of the function you want to fire on submit. You can use submit, click, pretty much 
    // any action that you can fire JS on in the HTML tag
    if (checkbox.addEventListener) {
    checkbox.addEventListener("click", myFunction);
    } else if (checkbox.attachEvent) {
    checkbox.attachEvent("click", myFunction);
    }
    // make sure to fire it, you can call the function earlier on during pageload, place it after the form, depends on your scope
    y();
    </script>
  • 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);
    }
    }
  • 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 just wanted to give an update: 

    Going off my gut suspicions, I built a Create Account form within CP that was separate from the Campaign. The hook fired, and Email was copied to Login. Happy day!

     

    Final result: Campaigns are DEFINITELY not full-featured in terms of manipulating contact data. Best to create a form in CP to handle that :)

     

    THANK YOU ALL FOR YOUR WONDERFUL HELP!

  • eric montijo
    Anuj Behl said:

    You need to replace the '+' with a '.' (Concatenate operator in php is a dot(.))

    if you want to see the $hookData results on the immediate screen, do an exit after you print, like.

    print_r($hookData);
    
    exit();
    

     

    View original

    Hey Anuj!

    Thanks for catching that. I changed the + to ., and added an exit() immediately after print_r ($hookData);. I saw no display, and there was no log in the debug logs.

    I'm really wondering if this has to do with the fact that this is a Campaign at this point. I have a request out to support to verify. Not looking forward to having to recreate the whole process if so :)

  • eric montijo
    Anuj Behl said:
     

    Hi Eric,

    No worries on the hook being added twice, I didn't notice that one was create and other update.

    Regarding it being the pre_contact hook, as SJ also suggested that all data is there within the hookdata variable. The code below which you said is not working will work with a pre_contact hook because if will be saved after you update it. No need for you to fetch the data using Connect PHP or manually save it. The contact is going to be saved anyway, the purpose of the hook will be to inject your data (login, in your case) before it saves.

    public function setLoginforcontact(&$hookData)
         {   
             $hookData['data']->Login = $hookData['data']->Emails[0]->Address;
         }
    

    Also, if you still see it not working, try to debug it by printing the $hookData variable. Check what's inside and in what format is the data stored and expected.

    -Anuj

    View original

    Hi Anuj!

    I have in my setLoginforcontact function within ContactModel.php these two lines now:

    logMessage("The good news is setLoginforcontact is firing :)"+$hookData["data"]->Emails[0]->Address);
    print "hookData = $hookData";

    After submitting the form and checking, there are no logs presented in the debug logs :( Am I missing something for logging?

    Thank you for your reply! 

  • eric montijo

    Okay, so, I checked my logs and after submitting the registration form again, it seems I have no new logs (even when ensuring I was running in Development mode).

    Making sure PHP logs show under Debug Logs on the site admin page? (or http://[deployment name]/ci/admin/logs/debug)

     

  • eric montijo
    SJ said:

    Eric,

    Are you getting the email address? Can you try logging that information, to see if the data is being passed correctly, and the hook is getting called?

     

    Thanks,

    SJ

    View original

    Hi SJ!

    I know you're probably busy. Thank you for your help.

    I'm having a hard time finding where PHP logs are stored in the file structure. I've put a line of code in ContactModel once the hook (hopefully) fires:

    logMessage("The good news is setLoginforcontact is firing :)");

    But I can't even see where to check this log. Usually there's a logs folder, but I can't find it in the structure.

    Also, do you think this could potentially be tied to the fact that the form is generated through a Campaign?

  • eric montijo

    Anuj Behl said:


    Hi Eric,



    Why do you have the same hook defined twice?



    And, also it would be better to use pre_contact_create hook instead of a post.


    View original


    Hi Anuj!



    Thanks for taking the time to respond. I used two hooks based on this article (that SJ posted above: https://cloudcustomerconnect.oracle.com/posts/b376145c99). I think it's to ensure that every time a contact update fires that it reinforces the login field to be the primary email address? I could imagine it being a pain if you added or changed your primary address that it would not also change your login. I could be totally wrong about this :P



    Also, since the contact is immediately created to generate the incident, and since the account can't be used to login until we approve that incident, wouldn't it make more sense to fire this hook after the contact is created? Otherwise, wouldn't there be no data to pull from the contact's email field, since the contact hasn't been made yet? Again, I could be 100% wrong, just trying to clarify. 



    Thank you for the advice!

  • eric montijo

    SJ said:


    Eric,



    You are missing an ampersand (&) in your function definition.



    Use 




    public function setLoginforcontact(&$hookData)


     



    Also, if all you need is to retrieve the email address, you do not need to extend the contact model. The hookData contains all the required information. Check this below post in the community :-



    https://cloudcustomerconnect.oracle.com/posts/b376145c99



     



    Hope this helps.



    Thanks,



    SJ


    View original


    SJ,



     



    Thank you for a great answer! The article you link is actually where I began. However, I went back, removed what I had for function setLoginforcontact, added the ampersand, and still no result. Here's what I've got now that still doesn't populate the Login field. Is there anything else you can think of that I might be missing?





    public function setLoginforcontact(&$hookData)
    {
    $hookData['data']->Login = $hookData['data']->Emails[0]->Address;
    }