Creating a logic hook to update the 'assigned_to' field to current_user

Hi,

When copying and saving a quote, the products show the assigned to as the original user and not the user that copied the quote.

I'm new to Sugar CRM and am trying to create a logic hook to update the assigned_to field to the current user for each product line in a quote when saving the record.

This is my attempt but did not work, any suggestions would be welcome.

Thank You,

<?php

$dependencies['products']['populate_assigned_user'] = array(
'hooks' => array("edit"), //not including save so that the value isn't stored in the DB
'trigger' => 'greaterThan(strlen(related($quotes,"assigned_user_name")),0)', //Optional, the trigger for the dependency. Defaults to 'true'.
'onload' => 'true', //Whether or not to trigger the dependencies when the page is loaded
'actions' => array(
array(
'name' => 'SetValue',
'params' => array(
'target' => 'assigned_user_id',
'value' => 'related($quotes,"assigned_user_id")'

)
),
array(
'name' => 'SetValue',
'params' => array(
'target' => 'assigned_user_name',
'value' => 'related($quotes,"assigned_user_name")'

)
)
)
);

  • Hi 

    I believe your code is not working due to a capitalization error, it should be $dependencies['Products'] instead of $dependencies['products'].

    I tried a similar code locally and it's working fine. Just a small correction, this is a dependency action, not a logic hook. 

    To use it you should paste it in the following path, and then run a Quick Repair and Rebuild: 

    custom/Extension/modules/Products/Ext/Dependencies/inherit_assigned_to.php

    Fullscreen
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    <?php
    $dependencies['Products']['populate_assigned_user'] = array(
    'hooks' => ['all'],
    'trigger' => 'greaterThan(strlen(related($quotes,"assigned_user_name")),0)',
    'onload' => 'true',
    'actions' => array(
    array(
    'name' => 'SetValue',
    'params' => array(
    'target' => 'assigned_user_id',
    'value' => 'related($quotes,"assigned_user_id")'
    )
    ),
    array(
    'name' => 'SetValue',
    'params' => array(
    'target' => 'assigned_user_name',
    'value' => 'related($quotes,"assigned_user_name")'
    )
    )
    XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX

    Let me know how it goes. 

    Cheers


    André

  • Thank you This works 100%. Do you know how I could set the value as the $current_user->id?

  • You can specify at the dependency "SetValye" the formula:

    'value' => 'related($quotes, 'modified_user_id')',

    André Lopes
    Lampada Global
    Skype: andre.lampada
  • Hi @André Lopes Am I am to set the value to the current user signed into CRM?

  • From the code previously provided

    Fullscreen
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    <?php
    $dependencies['Products']['populate_assigned_user'] = array(
    'hooks' => ['all'],
    'trigger' => 'greaterThan(strlen(related($quotes,"assigned_user_name")),0)',
    'onload' => 'true',
    'actions' => array(
    array(
    'name' => 'SetValue',
    'params' => array(
    'target' => 'assigned_user_id',
    'value' => 'related($quotes,"modified_user_id")'
    )
    ),
    array(
    'name' => 'SetValue',
    'params' => array(
    'target' => 'assigned_user_name',
    'value' => 'related($quotes,"modified_by_name")'
    )
    )
    XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX

    André Lopes
    Lampada Global
    Skype: andre.lampada