Auto creation of Opportunity record

Hello guys,

Congratulations on a wonderful tool that I’ve managed to costumize to my event company needs. Since I’m not to much PHP literate, I’ve managed to do all my workflow customizations viva workflow module, including the feature I’m requesting bellow, so that I could accommodate my needs.

I only lack one issue and I truly hope that someone can help.I’ve search thru out the forum and web but there are numerous posts with so outdated information. :frowning:

I’ve stripped suitecrm 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.

I’m ready even to contribute or deal a fee to create this script.

Many thanks for the time! :slight_smile:

Hi,

I don’t think it would be possible to create that functionality just using workflows. You would really need to add a logic hook that runs when a relationship is added to create the new Opportunity bean.

I can try to talk you through how to do this if you wish, though if you’re not confident coding you may be best filling in our enquiry form and we can put you in contact with one of our partners who can carry out this work for you.

1 Like

Hello,

Thanks for the quick reply.

I already tried the inquiry but I suspect that my personal budget has already been surpassed with other subjects.

Would you be so kind to walk me trought on this? On the simpliest way possible way with less trouble possible to you… I’m computer literate and I do write some lines of code. I really do need help, getting desperate.

Many thanks for your feedback :slight_smile:

First you need to create a file in custom/Extension/modules/Project/Ext/LogicHooks

You can call the file whatever you want and it should contain the 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');

This tells SuiteCRM that whenever a relationship is added to an account we want to call the method createOpp from the class oppHook which is located at custom/modules/Project/oppHook.

Now we need to create this class. The code should looks something like this:


<?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->save();
        }
    }
}

The logic hook will be called when anything is related to a project so the if statement in the example above checks that it is an account that is being related. The next lines create a new opportunity with a name that combines the project and account’s name and saves it.

Hopefully this example will give you an idea of what you need to write.

After you’ve written your code you will need to run a repair and rebuild so Suite can update it’s list of logic hooks that it needs to run.

1 Like

First of all I can’t thank you enough for your help! Honestly! I’m finally I’m getting somewhere. A tear just dropped from my eye :smiley:

So I’ve implemented the code. As it stands as soon has I go to Project and select a Account, the opportunity is created, but with no data, and no relation created to that account or project. It just creates the record.

It creates opp record but with the database ID in the name and no info copied from the Project.

Thank you so much again for your time and effort.

Waiting for your feedback.

to relate it to the account and project you’ll need to add the following to your code:


$oppBean->load_relationship("accounts");
$oppBean->load_relationship("project");

$oppBean->accounts->add($arguments["related_bean"]);
$oppBean->project->add($projectBean);

I can’t see why what I provided wouldn’t set the name. To set other fields you need to add lines like the following (correcting the text in italic to the correct names)

$oppBean->field_name = $projectBean->field_name;

1 Like

The opportunity is being created with the name same has the project database ID record.

So just to be sure you’re on the right page.

  • I’ve create a file called logic_hooks.php with the code you gave me to that file and placed it in dir custom/Extension/modules/Project/Ext/.

  • Created the function file with the code you gave and added the info of your last post and placed it in dir custom/modules/Project/. I added just one field to test the population but no go.

<?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();
        }
    }
}

?>

So with these changes, opp is being created but still only with the ID of the database on the name, no relations created.

I attached the files for your convenience. I want to facilitate your help as much as I can.

Thank you so much for your help ewanmcrobert I can’t thank you enought. :slight_smile: