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
Manish Anand 10Manish Anand 10 

Issues with Triggers in Apex Classes

Hi,
My requirement is -A user shouldn't be able to create opportunites more than worth 100k in a day. I am writing a trigger in the apex class for this.Below is the code.
public class TriggerClass
{
  public void checkOpps(List<Opportunity> ops)
  {
    Double Total_Amount=0;
    for(Opportunity o1: [Select Amount from Opportunity where createdDate=Today AND CreatedByID=:UserInfo.getuserID()])
    {
      Total_Amount += o1.Amount;
    }
     for (opportunity o2:ops)
    {
    Total_Amount+=o2.amount;
    }
    If(Total_Amount>1000000)
    {
     o2.addError('You have exceeded Daily Limits');
     }
     }
     }

It thorws a compile error:  Compile Error: Variable does not exist: Amount 
When I comment the line-Total_Amount += o1.Amount, it gives an error-Loop variable must be an SObject or list of Opportunity  (for the first For loop).
What I am missing here.
Note- I know this can be done in many other ways, but I am interested in knowing reason for these errors in the above code
Best Answer chosen by Manish Anand 10
Mahesh DMahesh D
Hi Manish,

Please find the below trigger and apex class:

Trigger: It should be in the before insert as we don't want to insert the record if it exceeds.
trigger OpportunityTrigger on Opportunity (before insert) {
	TriggerClass tc = new TriggerClass();
	tc.checkOpps(Trigger.new);
}

Apex Class: This will cover all scenarios.
public class TriggerClass {
    public void checkOpps(List<Opportunity> ops) {
        Double totalAmount = 0;
        for(Opportunity o1: [Select Id, Amount from Opportunity where createdDate = Today AND CreatedByID=:UserInfo.getuserID()]) {
            if(o1.Amount != null)
                totalAmount += o1.Amount;
        }
        
        for (opportunity o2:ops) {
            if(o2.amount != null)
                totalAmount +=o2.Amount;
        }
        
        If(totalAmount > 1000000) {
            for (opportunity o2:ops) {
                o2.addError('You have exceeded Daily Limits');
            }
        }
    }
}

I already tested this in my DE and everything looks good.

Please do let me know if it helps you.

Regards,
Mahesh

All Answers

Mahesh DMahesh D
Hi Manish,

Can you also paste the Trigger here. So that I can able to help you.

Regards,
Mahesh
Pankaj_GanwaniPankaj_Ganwani
Hi Manish,

I have changed your code a bit. You will have to wrap Total_Amount>1000000 within for loop as o2 variable is not defined outside of for loop:

trigger OppTrigger on Opportunity (after insert) 
{
    Double Total_Amount=0;
    for(Opportunity o1: [Select Amount from Opportunity where createdDate=Today AND CreatedByID=:UserInfo.getuserID()])
    {
      Total_Amount += o1.Amount;
    }
     for (opportunity o2:Trigger.new)
    {
    Total_Amount+=o2.amount;
    
    If(Total_Amount>1000000)
    {
     o2.addError('You have exceeded Daily Limits');
     }
    }
    
     }

This code is saved in my org without any fail.
Mahesh DMahesh D
Hi Manish,

Please find the below trigger and apex class:

Trigger: It should be in the before insert as we don't want to insert the record if it exceeds.
trigger OpportunityTrigger on Opportunity (before insert) {
	TriggerClass tc = new TriggerClass();
	tc.checkOpps(Trigger.new);
}

Apex Class: This will cover all scenarios.
public class TriggerClass {
    public void checkOpps(List<Opportunity> ops) {
        Double totalAmount = 0;
        for(Opportunity o1: [Select Id, Amount from Opportunity where createdDate = Today AND CreatedByID=:UserInfo.getuserID()]) {
            if(o1.Amount != null)
                totalAmount += o1.Amount;
        }
        
        for (opportunity o2:ops) {
            if(o2.amount != null)
                totalAmount +=o2.Amount;
        }
        
        If(totalAmount > 1000000) {
            for (opportunity o2:ops) {
                o2.addError('You have exceeded Daily Limits');
            }
        }
    }
}

I already tested this in my DE and everything looks good.

Please do let me know if it helps you.

Regards,
Mahesh
This was selected as the best answer
DeepthiDeepthi (Salesforce Developers) 
Hello Manish,

Please check the below code.
 
public class TriggerClass
{
  public static void checkOpps(List<Opportunity> ops)
  {
    Double Total_Amount=0;
    for(Opportunity o1: [Select Amount from Opportunity where CreatedDate=Today AND CreatedByID=:UserInfo.getUserID()])
    {
      Total_Amount += o1.Amount;
    }
    for (Opportunity o2:ops)
    {
        Total_Amount+=o2.Amount;
         If(Total_Amount>1000000)
        {
             o2.addError('You have exceeded Daily Limits');
        }
      }
    }
}

After creating this class, create a trigger inorder to call this method in it.
trigger dailyOppLimit on Opportunity (after insert, after update) {
    TriggerClass.checkOpps(Trigger.new);
}

Hope this helps you!
Best Regards,
Deepthi
Mahesh DMahesh D
Hi Manish,

One more thing here,

if you requirement is not to insert even a single record in the batch if it crosses the 100K then you can use below trigger:
 
public class TriggerClass {
    public void checkOpps(List<Opportunity> ops) {
        Double totalAmount = 0;
        for(Opportunity o1: [Select Id, Amount from Opportunity where createdDate = Today AND CreatedByID=:UserInfo.getuserID()]) {
            if(o1.Amount != null)
                totalAmount += o1.Amount;
        }
        
        for (opportunity o2:ops) {
            if(o2.amount != null)
                totalAmount +=o2.Amount;
        }
        
        If(totalAmount > 100000) {
            for (opportunity o2:ops) {
                o2.addError('You have exceeded Daily Limits');
            }
        }
    }
}

If your requirement is to allow upto 100k and show error for all the records in a batch after it reaches 100K then use the below trigger:
 
public class TriggerClass {
    public void checkOpps(List<Opportunity> ops) {
        Double totalAmount = 0;
        for(Opportunity o1: [Select Id, Amount from Opportunity where createdDate = Today AND CreatedByID=:UserInfo.getuserID()]) {
            if(o1.Amount != null)
                totalAmount += o1.Amount;
        }
        
        for (opportunity o2:ops) {
            if(o2.Amount != null)
                totalAmount +=o2.Amount;
            If(totalAmount > 100000)
                 o2.addError('You have exceeded Daily Limits');
        }
    }
}


Here we have to consider we points:

(1) As it is a kind of validation check, which should be done before scenario.
(2) Amount should have a null check because if you are not adding the products as part of Opportunity creation then it will show null pointer exception.

Please do let me know if it helps you.

Regards,
Mahesh

 
Manish Anand 10Manish Anand 10
Thanks Pankaj,Mahesh and Deepthi,
I pasted and executed each of yours code. But the same error for each of the code-  "Variable does not exist: Amount",
for the line-Total_Amount += o1.Amount;
What's the fun, we are missing here?Is it something to do with my dev org settings?

Regards,
Manish.
 
Pankaj_GanwaniPankaj_Ganwani
Hi Manish,

Are you using any IDE for writing the code? If so, please copy the code from IDE and paste it directly in the org.
Mahesh DMahesh D
Hi Manish,

I am not sure why you are getting this error.

Can you please paste the code from my post and take a print screen of error message.

Bcoz, Both the solution which I provided, I verified it in my DE environment and it is working properly.

Regards,
Mahesh
Manish Anand 10Manish Anand 10
Hi Pankaj and Mahesh,

The same code worked with my other dev org. Not sure, if some settings must be enabled.

Regards,
Manish
Mahesh DMahesh D
Hi Manish,

Could you please take screen shot of your error message and paste it here.

Regards,
Mahesh
Manish Anand 10Manish Anand 10
Hi Mahesh,

Please find the screen shot attached below.
User-added image
Mahesh DMahesh D
Hi Manish,

Here are few of the things which you can do.

Option 1: 
Please copy the o1.Amount from line 5 and paste it into line 6 because sometimes

o1 
ol
both looks same.

Option 2:

Comment both Line 5 and Line 6, first try to save it. If it saves then try using a different variable in the place of o1 to opp.

Regards,
Mahesh
 
Manish Anand 10Manish Anand 10
Hi Mahesh,

It's still the same error in the same line number.No matter, what's the varibale name.

Regards,
Manish
Sanu VermaSanu Verma
Hey if i want tht thng by using aggregate soql then any one suggest me  how to do ..?