Workflow - assign contact note to account

Before investigating logic hooks further, could someone please confirm whether a note created from the main menu, and manually linked to a contact, could then be related to an account via a workflow?

Thanks

yes, but you’ll will not able to relate to the contact’s account, check my example, this has a condition to assign to a selected account only if the notes is related to a contact

best regards

1 Like

Thanks Mike for for the prompt response.

So something like a logic hook using sql where clause on contact parent ID?

you can avoid (and should) using SQL in logic hooks, you should is the bean http://support.sugarcrm.com/Documentation/Sugar_Developer/Sugar_Developer_Guide_6.5/Application_Framework/SugarBean/Fetching_Relationships/

best regards

1 Like

Got it working, cheers Mike.

If you like to, your can share your code in the forum so other people can benefit from it in the future

Best regards

Situation: Notes created under a Contact are automatically assigned to the parent Account. I’d like users to be able to batch add Notes from the main menu as it’ll be quicker than finding each contact and adding Notes there. The issue is the user than has to manually relate the Note to an Account - time consuming and possible to add Note to different account to the Contact. Only problem I see is when there are two Contacts with same name but I don’t have that issue (yet).

Goal: To automatically assign the correct account to the note based on the chosen contact.

So after ruling out a workflow, I saw that logic hooks could do it and with Mike’s code example, here’s what I’ve ended with.

/custom/modules/Notes/logic_hook.php


<?php
// Do not store anything in this file that is not part of the array or the hook version.  This file will
// be automatically rebuilt in the future.
 $hook_version = 1;
$hook_array = Array();
// position, file, function
$hook_array['before_save'] = Array();
$hook_array['before_save'][] = Array(1, 'Update Contact Link', 'custom/modules/Notes/contactLinks.php','contactLinks', 'contactLinks1');
?>

The logic hook is run before save and “‘custom/modules/Notes/contactLinks.php’” is the link to the php file I created that contains the class (contactLinks) and the Function(contactLinks1). The function is where the code goes.

/custom/modules/Notes/contactLinks.php


<?php
if (!defined('sugarEntry') || !sugarEntry) die('Not A Valid Entry Point');
    class contactLinks
    {
        function contactLinks1($bean, $event, $arguments)
        {
        $bean->name = $bean->activitytype_c ;

//Load Contact
$bean1 = BeanFactory::getBean('Contacts', $bean->contact_id);

//If relationship is loaded
if ($bean1->load_relationship('accounts'))
{
    //Fetch related beans
    $relatedBeans = $bean1->accounts->getBeans();

    $parentBean = false;
    if (!empty($relatedBeans))
    {
        //order the results
        reset($relatedBeans);

        //first record in the list is the parent
        $parentBean = current($relatedBeans);
        $bean->parent_id  = $parentBean->id ;
    }
}

        }
    }
?>

The code “$bean->name = $bean->activitytype_c ;” takes the activity name and sets it as the “name” field for the Note (so under notes list view you see the different activities)

The code below it is what Mike linked to. As I ended up with two $beans, I had change the $bean in the code to $bean1.
So $bean refers to the note and $bean1 to the Contact. $parentbean is the Account and utimately what we need to get the parent ID.

The Note stores the contact details (as the crm user must choose this) so by loading Contacts, we can load the relationship to Accounts and thus get the Account name we are after.


//Load Contact
$bean1 = BeanFactory::getBean('Contacts', $bean->contact_id);

So in the above code, I used the account id from the Note ($bean->contact_id) to link the correct Contact in $bean1

I didn’t change any of the code (which through a relationship assigns Account to $parentbean) below that (apart from changing $bean to $bean1) and just added the last line to add the parent id to my Note below


$bean->parent_id  = $parentBean->id ;

A couple things off the top of my head as I’ve been trying so many things with SuiteCRM that steps meld into each other.

I think you have to repair and rebuild when first creating the new file related to the logic hook but after that can just make code changes. I do something simple to check that my changes are live like “$bean->description = ‘test test’ ;” (this overwrites the description field in the Note when you save) so you’re not wondering if your code is wrong or it’s just not applying it.

I’m not assigning “Relate Type” anywhere so assume Account is default dropdown option. Otherwise you’d set something like this in the code snippet above:


$bean->parent_id  = $parentBean->id ;
$bean->parent_type  = 'Account' ; <- I think it's single quotes

I use bitnami linux and had to set execute bit on the file I created contactLinks.php. May need to check permissions as well.

I think that’s it, hopefully that helps someone!

1 Like