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
Vishu Sri 6Vishu Sri 6 

Hi Guys, I want to write a trigger to restrict bankers to create opportunities worth more than 10000 dollars in a day.

Hi Guys, I want to write a trigger to restrict bankers to create opportunities worth more than 10000 dollars in a single day.
VamsiVamsi

Initially get a list of all the Opportuities inserted today with amount > 10,000 $. Finally throw an error if list size is greater than 1 
Vishu Sri 6Vishu Sri 6
Thanks Vamsi, I missed a word here, the total amount of all the opportunities should not exceed 10000 dollars. A error should be thrown in that case.
Ajay K DubediAjay K Dubedi
Hi Vishu,

 code to restrict users to not generate opportunity worth more than 10000 - 

public class OppAmountRestriction_Class {
    public static void restrictAmount(List<Opportunity> oppNew)
    {
            Decimal sumAmount=0;
            List<Opportunity> oppOld = new List<Opportunity>();
            oppOld = [Select Id,Name ,Amount from Opportunity where CreatedDate=today];
            if(oppOld.size()>0)
            {
                
                for(Opportunity op : oppOld)
                {
                    if(op.Amount!=null)
                    {
                        sumAmount += op.Amount;
                    }
                }
            }
            if(oppNew.size()>0)
            {
                for(Opportunity op : oppNew)
                {
                    if(op.Amount!=null)
                    {
                        sumAmount += op.Amount;
                    }
                    System.debug('sumAmount ----' + sumAmount);
                    if(sumAmount > 10000)
                    {
                        op.addError('opportunities worth more than 10000 dollars in a single day Exceed');
                    }
                }
            }
    }
}

if you found this answer helpful then please mark it as best answer so it can help others.
    
    Thanks,
    Ajay Dubedi
VamsiVamsi
In that case get the sum of amounts on all Opps created today using Aggregate query and then thorw an error on all the new records if amount is greater than 10 K $