Workflow for subtracting value for custom field based on entered value

Hi,

I’ve created a custom Quantity field in Products Module to keep track of the stock for products. What i want to achieve is whenever a case is created and a product is used, i should be able to deduct it from the quantity field for that product. I tried creating a workflow but it only allows me to assign a value. How do i get the original value so that i can subtract from it?

Thanks…

@patelparth6111

Look at the examples here:

Thanks for your reply. But i think the examples for calculated field only shows editing the fields for the workflow module we select. I want to run workflow on Cases module and subtract from a field in Products module. Let me know if it makes sense.

You can use an add-on I made called PowerFields for that.

If you prefer to do it in code you can use a Logic hook which gives you access to all the related data through Beans objects.

1 Like

@pgr Thanks for your reply. I was able to implement what i need using logic hooks but the code is running everytime i make an update in the case. So even if don’t change anything the custom code executes and the product quantity keeps getting subtracted. Is there a way i can control it to run it only when a specific field is changed?

Here’s the custom code which runs using logic hook

require_once('modules/AOS_Products/AOS_Products_sugar.php');
require_once('modules/AOS_Products/AOS_Products.php');
class updateProductQuantity
{
    public $module = 'Cases';
    public function updateQuantity($bean, $event, $arguments)
    {
    	$GLOBALS['log']->error('updateQuantity Called');
      $GLOBALS['log']->error($bean->part1_c);
    	$p = new AOS_Products();
      $p->retrieve_by_string_fields(array('name' => $bean->part1_c ));
      $GLOBALS['log']->error($p->id);
      $GLOBALS['log']->error($p->quantity_c);
      $p->quantity_c = $p->quantity_c - $bean->part1_quantity_c;
      $productBean = BeanFactory::getBean('AOS_Products');
      $productBean->id = $p->id;
      $productBean->quantity_c = $p->quantity_c;
      $productBean->save();

    }
}

@patelparth6111
add condition:

if($bean->part1_c!=$bean->fetched_row['part1_c']){
// your code
}

and
You shouldn’t call require for module AOS_Products. The object included already. Your code can be shorted:

class updateProductQuantity
{
    public function updateQuantity($bean, $event, $arguments)
    {
        if($bean->part1_c!=$bean->fetched_row['part1_c']){
            $GLOBALS['log']->error('updateQuantity Called');
            $GLOBALS['log']->error($bean->part1_c);
            $p = new AOS_Products();
            $p->retrieve_by_string_fields(array('name' => $bean->part1_c ));
            $GLOBALS['log']->error($p->id);
            $GLOBALS['log']->error($p->quantity_c);
            $p->quantity_c = $p->quantity_c - $bean->part1_quantity_c;
            $p->save();
        }
    }
}

@p.konetskiy Thanks. This Worked!!

Another question
Is there a way to have a button in edit view of case module that would show up once a product is added. This button would let me add additional products.

I want to achieve something like this

Let’s say i have two fields as follows:
Product 1 : Test Product
Product 1 Quantity: 10
Then, i would like to have a button that would allow me to add Product 2, Product 3, …
Is this possible?

@patelparth6111

Yes, it’s possible.
Look at the solution https://github.com/salesagility/SuiteCRM/pull/8341
It’s repaired field type collection of SuiteCRM.
It’s my solution. If you will have question you will ask here.