function readOnly(count){ }
Starting November 20, the site will be set to read-only. On December 4, 2023,
forum discussions will move to the Trailblazer Community.
+ Start a Discussion
Josh GitogoJosh Gitogo 

How to create an apex trigger that updates a record by subtracting from a sample request?

I am trying to create an apex trigger that subtracts the quantity from an inventory field of a custom object when the status of a seperate custom object's record's picklist is selected as shipped. I am new to apex programming and am not sure where to start.
Eduardo AndradeEduardo Andrade

Hi, it would be something like this:

trigger Subtracts on CustomObject__c (before insert, before update) {
    
	List<(CustomObject__c> list_to_upsert = new List<(CustomObject__c>();

	for(CustomObject__c co : Trigger.New) {
		if(co.status == 'shipped'){
			co.inventory = co.inventory - 1;
			list_to_upsert.add(co)
		}
	}

	if(!list_to_upsert.IsEmpty()){
		upsert list_to_upsert;
	}
}

I've recomend you learn about triggers in the Trigger module on Trailhead.
HARSHIL U PARIKHHARSHIL U PARIKH
I am wondering how it would update the record before even it makes it to the database based on the trigger above but I can be further more educated..
Anyway, based on your requirement, when you write a before trigger then you might not need any DML or LISTS inside the trigger since trigger by itself would change that record before insert or before update.

try using following trigger,
Trigger Subtracts on CustomObject__c (before insert, before update) 
{
    If(Trigger.IsInsert || Trigger.IsUpdate)
    {
        for(CustomObject__c co : Trigger.New) 
        {
            If(co.status__c == 'shipped' && co.inventory__c != null && co.Quantity__c != null)
            {
                co.inventory__c = co.inventory__c - Quantity__c;
            }
        }
    }
}
In place of Custom_Object__c just write your object name.
And make sure the Status__c, inventory__c, and Quantity__c fields are same as yours. I mean their API names are same as yours.

If it solves the query then please mark it as Best Answer and it will help other people with similar issue!
Thank You sir!
 
Josh GitogoJosh Gitogo
I belive this is the code that I need however I am still running into a problem. When I enter the code this error pops up :Arithmetic expressions must use numeric arguments. Do you know how to fix this?
trigger AutoInventory on Sol_sence_Sample__c (before insert, before update) 
{
    If(Trigger.IsInsert || Trigger.IsUpdate)
    {
        for(Sol_sence_Sample__c co : Trigger.New)
        {
            If(co.Shipped__c == 'checked' && co.sol_sence_inventory__c != null && co.Quantity__c != null)
            {
                	co.Sol_sence_Inventory__c = co.Sol_sence_Inventory__c - co.Quantity__c;
            }
        }
    }
}
HARSHIL U PARIKHHARSHIL U PARIKH
Question?
Is the sol_sence_inventory__c and Quantity__c are both a NUMBER data type field? If yes, then are they both have decimal as 2 or 0?
Also let me know which line the error pops up in?

Anyway, I have just made a use of Integer.ValueOf() function inside the trigger. See if it works.
trigger AutoInventory on Sol_sence_Sample__c (before insert, before update) 
{
    If(Trigger.IsInsert || Trigger.IsUpdate)
    {
        for(Sol_sence_Sample__c co : Trigger.New)
        {
            If(co.Shipped__c == 'checked' && co.sol_sence_inventory__c != null && co.Quantity__c != null)
            {
                    co.Sol_sence_Inventory__c = Integer.ValueOf(co.Sol_sence_Inventory__c - co.Quantity__c);
            }
        }
    }
}