Pliz, guide me in logic hook

Hi.

I was write hook, to fill in custom module field “probability” from dropdown “sales stage” (like in standard module opportunity);
But it nothing to do, guide me, plz.
hook:

<?php $hook_version = 1; $hook_array = Array(); $hook_array['before_save'] = Array(); /* push persent from sale stage*/ $hook_array['before_save'][] = Array( 1, 'probability field define', 'custom/modules/MR001_Rent_Oport/probabpers.php', 'probabpers', 'pushpersent' ); ?>

probabpers.php:

<?php if(!defined('sugarEntry') ||!sugarEntry) die('Not A Valid Entry Point'); class probabpers{ function pushpersent ($bean, $event, $arguments){ document.getElementsByName('probability')[0].value = document.getElementsByName('sales_stage')[0].value; SUGAR.util.callOnChangeListers(document.getElementsByName('probability')[0]); } } ?>

hi, try …

off course, you must adapt this example:


function pushpersent ($bean, $event, $arguments){

$opportunity_id = "......";  //get with your relationship 
include_once("modules/Opportunities/Opportunity.php");
$opportunity = new Opportunity();
$opportunity->retrieve($opportunity_id);

$bean->probability = $opportunity->probability;

}

Thanks, i`m try and reply :slight_smile:

Something going wrong.

function pushpersent ($bean, $event, $arguments){

$opid = $bean->id;

include_once("modules/Opportunities/Opportunity.php");

$opportunity = new Opportunity();

$opportunity-> $opid;

$bean->probability = $opportunity->sales_stage;

}
not working.
Help pliz.

I don’t know how much use it is to you, but I’ll just post it.

When I’m creating logic hooks I want to test the code I’ve been writing, and preferably see test-data or get a printed version of the object I’m working with. However, SuiteCRM seems to ignore all output from the file that runs when you create a logic hook, so I made up this little hack to make sure that I can turn on or off test-data as I deem necessary. In the function that is run for a specific logic hook, add a boolean variable $debug, and at the end of all the code for your function, add


if ($debug) {
  // here you can echo your test-data and do whatever you want.
  // I like using print_r to quickly get an understanding of the object I'm working on.
  // when printing objects, arrays, and stuff like this with print_r, I recommend echoing a <pre>-tag first, so it will look pretty.
  echo '<pre>';
  print_r($bean);
  exit("<br>");
  // The <br> inside the exit is not important, it's just what I've been using for some testing.
}

This way, I can toggle the debug output by switching the $debug-variable between true and false.

As I said, I don’t know if this is of any help to you, but it might help you debug your hook. Good luck!

Please be more specific on what you are trying to achieve.

What kind of logic hook is it? (before_save, after_save, … which type?)
On what module are you running the logic hook?

What are you trying to do with the following code:

$opid = $bean->id;
include_once("modules/Opportunities/Opportunity.php");
$opportunity = new Opportunity();
$opportunity-> $opid;

From what module is the bean?
if you want to retrieve an opportunity whose id is $opid you should use the method retrieve: $opportunity->retrieve($opid) or are you creating a new opportunity whose id is $opid? In that case you shoud do: $opportunity->id = $opid;

Possibly, you should also save the opportunity at the end, but this depends also on the module you are running the logic hook and the type of logic hook you are running: $opportunity->save();

1 Like

Thank you.

Some error was found, but hook steel not working. Its custom module based on opportunity.
I`m try to write in field 'probability ’ from field ‘sales stage’. (like in standard module opportunity)
When user click on Save hook mast retrieve field ‘sales_stage’ and give it to ‘probability’ field .

Hook;

<?php $hook_version = 1; $hook_array = Array(); // position, file, function $hook_array['before_save'] = Array(); $hook_array['before_save'][] = Array(1, 'probability field define', 'custom/modules/MR001_Rent_Oport/probabpers.php', 'probabpers', 'pushpersent'); PHP; function pushpersent ($bean, $event, $arguments){ $opid = $bean->id; $sale_stage = $bean->sales_stage; include_once("modules/Opportunities/Opportunity.php"); $opportunity = new Opportunity(); $opportunity->retrieve($opid); $bean->probability = $sale_stage; }

You haven’t defined the type of logic hook so your logic hook will never fire.

Normally this is done (for example for a before_save logic hook) in this way:

a. create a file called: custom/modules/MR001_Rent_Oport/logic_hooks.php
and paste the following code inside this file:

    $hook_version = 1;
    $hook_array = Array();

    $hook_array['before_save'] = Array();
    $hook_array['before_save'][] = Array(
        //Processing index. For sorting the array.
        1,
       
        //Label. A string value to identify the hook.
        'probability field define',
       
        //The PHP file where your class is located.
        'custom/modules/MR001_Rent_Oport/probabpers.php',
       
        //The class the method is in.
        'probabpers',                           //  <----- In your code I don't see where you have defined the class
       
        //The method to call.
        'pushpersent'
    );

b. create a file called: custom/modules/MR001_Rent_Oport/probabpers.php
and paste the following code inside this file:

<?php
    if (!defined('sugarEntry') || !sugarEntry) die('Not A Valid Entry Point');
    class probabpers
    {
        function pushpersent($bean, $event, $arguments)
        {
           $bean->probability = $bean->sales_stage;
        }
    }
?>

From your code I don’t understand what you are trying to achieve. In any case your code contains redundant parts so I removed them.
If you expalin what you are trying to do I can try to help more.
Please post a picture of the definition of both your fields sales_stage and probability.

Maybe you are trying to do something like:

<?php
    if (!defined('sugarEntry') || !sugarEntry) die('Not A Valid Entry Point');
    class probabpers
    {
        function pushpersent($bean, $event, $arguments)
        {
           switch ($bean->sales_stage)
           {
              CASE 'stage 1':   // use appropriate value here
                $bean->probability = 10; // use appropriate value here
                break;
              CASE 'stage 2':   // use appropriate value here
                $bean->probability = 30; // use appropriate value here
                break;
              CASE 'stage 3':   // use appropriate value here
                $bean->probability = 50; // use appropriate value here
                break;
              CASE 'stage 4':   // use appropriate value here
                $bean->probability = 70; // use appropriate value here
                break;
              CASE 'stage 5':   // use appropriate value here
                $bean->probability = 90; // use appropriate value here
                break;
              CASE 'stage 6':   // use appropriate value here
                $bean->probability = 100; // use appropriate value here
                break;
              default':   // use appropriate value here
                break;
           }
        }
    }
?>

For reference have a look at: http://support.sugarcrm.com/Documentation/Sugar_Developer/Sugar_Developer_Guide_6.5/Module_Framework/Logic_Hooks/Module_Hooks/before_save/