Setting New Opportunity Revenue Line Item Assigned To and Teams

When creating a new Opportunity (Opp), the Revenue Line Item (RLI) is also created at the same time. Currently, the RLI Assigned To and Teams are set to the values of the person creating the records, but we would like the RLI Assigned To and Teams to be equal to the Opportunity Assigned To and Teams. We'd only like this on initial RLI Creation because we'd like to have the ability to change it and make it different, if needed, later. 

I was thinking of a SugarBPM process initiated on a new RLI Record Creation, but I can't seem to find the right way to set the RLI Assigned To and Teams fields to the values in the Opportunity. 

Maybe there's an entirely different way to do this, but we are new at Sugar and haven't done much in the customization way other than some SugarLogic formulas for fields and very rudimentary SugarBPM processes. 

Thanks for any help!

  • From Manipulating Teams PRogrammatically > https://support.sugarcrm.com/Documentation/Sugar_Developer/Sugar_Developer_Guide_10.0/Architecture/Teams/Manipulating_Teams_Programmatically/

    It doesn't seem like you set the Teams by just setting the team_set_id as in the great example above, but you need to set individual Team IDs via the replace() method. Is there a way to set the whole RLI "replace" array with something from the Opportunity team set? Thanks in advance for helping me rationalize the example above with the Manipulating Teams Programmatically code!  VO

  • Hi Vince!  Nice way to put up the question there  Joy 

    If you want to follow the programmatic way, you can create an after save logic hook as mentioned in the documentation - and you can replace the teams in Line Items something like:

    // Set Opportunity's teams in Line Items
    $teamSetBean = new TeamSet();
    $bean->load_relationship('teams');
    $bean->teams->setSaved(false); // Manually reset TeamSet state for save
    $opportunityTeamIds = $teamSetBean->getTeamIds($opportunity->team_set_id);

    // Set Teamset
    $bean->teams->replace($opportunityTeamIds);

    // Set Primary Team
    $bean->team_id = $opportunity->team_id;
  • Thanks Neeraja! You have been a great help!

    I believe I needed to add the step to get the bean for $opportunity before it could be referenced. 

    I tested the following out and the RLI continues to have both the Teams from the Opportunity, plus any additional Default Teams from the user creating the RLI. So I'm trying to figure that out now. It seems like the RLI Teams need to be "removed" or emptied out before they are replaced with the Opportunity's Teams. 

    <?php

    class CopyTeamAssignedTo
    {
    function execute($bean, $event, $arguments)
    {
    if (empty($bean->fetched_row) && // Do this only for new records
    !empty($bean->opportunity_id)) // Ensure there is an opportunity
    {
    // Get / Set Opportunity
    $opportunity = BeanFactory::getBean('Opportunities', $bean->opportunity_id);

    // Set Opportunity's teams in Line Items
    $teamSetBean = new TeamSet();
    $bean->load_relationship('teams');
    $bean->teams->setSaved(false); // Manually reset TeamSet state for save
    $opportunityTeamIds = $teamSetBean->getTeamIds($opportunity->team_set_id);

    // Set Teamset
    $bean->teams->replace($opportunityTeamIds);

    // Set Primary Team
    $bean->team_id = $opportunity->team_id;
    }
    }
    }

    Fullscreen
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    <?php
    class CopyTeamAssignedTo
    {
    function execute($bean, $event, $arguments)
    {
    if (empty($bean->fetched_row) && // Do this only for new records
    !empty($bean->opportunity_id)) // Ensure there is an opportunity
    {
    // Get / Set Opportunity
    $opportunity = BeanFactory::getBean('Opportunities', $bean->opportunity_id);
    // Set Opportunity's teams in Line Items
    $teamSetBean = new TeamSet();
    $bean->load_relationship('teams');
    $bean->teams->setSaved(false); // Manually reset TeamSet state for save
    $opportunityTeamIds = $teamSetBean->getTeamIds($opportunity->team_set_id);
    // Set Teamset
    $bean->teams->replace($opportunityTeamIds);
    XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX

  • RLI continues to have both the Teams from the Opportunity, plus any additional Default Teams from the user creating the RLI

    Hi Vince,

    Appreciate your patience and effort in trying to get this code work.

    I noticed that I sent the code in a hurry without testing, my bad. The reason why the original team is still attached is because of the order of setting team_id and teams. The team_id should be set first, so that it doesn't get added during replace. I also removed the "teams->setSaved(false)", since you're probably using this in a before_save hook.

    This version should work, please try it out:

    Fullscreen
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    <?php
    /**
    * Replaces Line Item teams from Opportunity
    *
    * Class CopyTeamAssignedTo
    */
    class CopyTeamAssignedTo
    {
    function execute($bean, $event, $arguments)
    {
    if (empty($bean->fetched_row) && // Do this only for new records
    !empty($bean->opportunity_id)) // Ensure there is an opportunity
    {
    // Get / Set Opportunity
    $opportunity = BeanFactory::getBean('Opportunities', $bean->opportunity_id);
    // Set Opportunity's teams in Line Items
    $teamSetBean = new TeamSet();
    $bean->load_relationship('teams');
    $opportunityTeamIds = $teamSetBean->getTeamIds($opportunity->team_set_id);
    XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX

1 2