Monday 4 December 2017

Save Dynamic Values in Fields after saving SugarCRM/SuiteCRM

Sometimes requirement may arise like, you have to calculate a dynamic value and save ina field upon saving a record. For eg: If you want to calculate commission of amount dynamicaly, Generating reference Number dynamically, Retreive some data from other module and save it in a partcular field  .etc we can use this method...

So For this for eg: If we want to do this partcular thing in a module called Agreements then:
Navigate to CRM/custom/modules/Agreeements/

Create a file called logic_hooks.php

The Main Purpose of this file is to handle this type of functions....

So The content of this file will be like this:-

<?php

$hook_version = 1; 

$hook_array = Array(); 

$hook_array['before_save'] = Array(); // before saving the record

$hook_array['after_save'] = Array();  // after saving record

$hook_array['process_record'] = Array(); //while processing the records view & list

$hook_array['before_save'][] = Array(1, 'CalculateCommission', 'custom/modules/Agreement/auto_calcs.php','AutoCals', 'CalculateCommission'); 

$hook_array['after_save'][] = Array(1, 'Generateref', 'custom/modules/Agreement/auto_calcs.php','AutoCals', 'Generateref'); 


?>


Now as I mentioned the location "custom/modules/Agreement/auto_calcs.php"

Create  a file called auto_calcs.php... Create Main function as Autocals... This you can provide names as your own wish...  Here am giving you an example only...

For eg: $hook_array['before_save'][] = Array(1, 'Title ur Giving For Function/any name', 'Location of file where u r defining the function','Main function name', 'Sub Function Name');


So asper our values our autocalcs.php file will be looking like:-

<?php

class AutoCals {


 function CalculateCommission($bean, $event, $arguments){
global $db;
$id=$bean->id;
$sql2 ="$query";
$row2 =$bean->db->query($sql2);
$record =$bean->db->fetchByAssoc($row2);

$amount=$record['amount'];

$commission=$amount*(10/100);
  $bean->commission = $commission;


}


 function Generateref($bean, $event, $arguments){
    global $db;
    $sid=$bean->id;

    //Refno. Calculation
$newrefno="generated";


$sql5 ="UPDATE `table` SET `refno`='$newrefno' WHERE `id`='$sid'";
$row5 =$bean->db->query($sql5);

    }



}

?>

perfrom a quick Repair & Rebuild to affect the changes..

1 comment:

  1. Such a Resourceful Article.

    Keep Sharing your CRM Knowledge to help others.

    Hosted portal solutions

    ReplyDelete