AnsweredAssumed Answered

Auto creation of opportunity when relating an Account to Project

Question asked by biulas on Mar 23, 2016
Latest reply on Mar 23, 2016 by Francesca Shiekh

Hello community!

 

I struggling for a few weeks now with some lines of code using beans and logic hooks.

 

Resuming, I've stripped my instance to use only accounts, opportunities, projects and contacts.

 

Basically I would like to:

 

     - whenever I'm in detailed view of an account record, when I go to project subpanel and click select a project, upon selecting, a new record in opportunities is created, populating some common fields from the project record.

     - likewise, whenever I'm in detailed view of project, upon selecting an account from subpanel, a related opportunity record should be created, with some fields populated from project. It should be simple using logic hooks or beanfactory but I'm clueless on this.

 

Now, I've created the hook with following code:

 

<?php
if(!isset($hook_array['after_relationship_add']) || !is_array($hook_array['after_relationship_add'])) {
  $hook_array['after_relationship_add'] = array();
}
$hook_array['after_relationship_add'][] = Array(11, 'createOpp', custom/modules/Project/oppHook.php','oppHook','createOpp');

 

And then the class oppHook, which has already a test field to autopopulate based on a field from the related ID:

 

<?php

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

class oppHook {
  function createOpp($projectBean, $event, $arguments) {
  if($arguments['related_module'] == "Accounts") {
  $oppBean = BeanFactory::newBean('Opportunity');
  $oppBean->name = $projectBean->name . " - " . $arguments["related_bean"]->name;
  $oppBean->load_relationship("accounts");
  $oppBean->load_relationship("project");
  $oppBean->accounts->add($arguments["related_bean"]);
  $oppBean->project->add($projectBean);
  $oppBean->estimated_start_date = $projectBean->data_de_inicio_c;
  $oppBean->save();
  }
  }
}

?>

 

Unitl now I've only managed to automate the creation of the opportunity, but no relation of field populated.

 

Getting crazy and without nails. HELP!  Many thanks to all of you!!!

Outcomes