How do I set role in Opportunities Contacts relationship in before_ or after_relationship_add logic hook?

Question as per title in Sugar 7.6

  • What do you mean by 'set role'? Do you mean get the role?

    You can get the role of a user in a logic hook using:

    $rolesArray = ACLRole::getUserRoleNames($current_user->id);

    Alternatively ACLRole::getUserRoles($current_user->id);

    If you want to set the role, why would you want to set the role in a relationship logic hook? What are you trying to achieve?

  • In the Opportunities Contacts relationship (metadata/opportunities_contactsMetaData.php) there is a special relationship field 

    array('name' => 'contact_role',     'type' => 'varchar', 'len' => '50'),

    When I add a relationship the Role field is empty by default. To change it I need to Edit the row in the subpanel.

    Instead I would like to set the role in one of the logic hooks choosing one of the options depending on certain conditions. 

    Otherwise it often happens that users forget to edit and set the field and the logic hook could do the job for them automatically. 

  • You could use the DBManagerFactory - http://support.sugarcrm.com/Documentation/Sugar_Developer/Sugar_Developer_Guide_7.7/Data_Framework/Database/DBManagerFac… .

    In custom/modules/Opportunities/logic_hooks.php:

    <?php
        $hook_version = 1;
        $hook_array = Array();

        $hook_array['after_relationship_add'] = Array();
        $hook_array['after_relationship_add'][] = Array(
            //Processing index. For sorting the array.
            1,
          
            //Label. A string value to identify the hook.
            'after_relationship_add example',
          
            //The PHP file where your class is located.
            'custom/modules/Opportunities/logic_hooks_class.php',
          
            //The class the method is in.
            'logic_hooks_class',
          
            //The method to call.
            'after_relationship_add_method'
        );

    ?>

    In custom/modules/Opportunities/logic_hooks_class.php:

    <?php

    if (!defined('sugarEntry') || !sugarEntry)
        die('Not A Valid Entry Point');

    class logic_hooks_class {

        function after_relationship_add_method($bean, $event, $arguments) {
            if ($arguments['related_module'] == 'Contacts' && $arguments['relationship'] == 'opportunities_contacts') {
                $sql = "update opportunities_contacts set opportunities_contacts.contact_role = 'Executive Sponsor' where opportunity_id = '" . $bean->id . "' and contact_id = '" . $arguments['related_id'] . "' and deleted != 1";
                $result = $GLOBALS['db']->query($sql);
            }
        }

    }

    ?>

    This will set it to Executive Sponsor each time, but obviously you can put your own logic there.

    Let me know how you get on.

  • Thanks Alan!

    I though there might be a way to achieve the same using the Bean instead of direct db query but thanks anyway!

  • Glad you liked the answer. It is probably $bean->opportunities_contacts->contact_role = 'Executive Sponsor'. But sometimes life is too short to use the bean. ;-)