You need to sign in to do that
Don't have an account?
Manish 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
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
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.
Apex Class: This will cover all scenarios.
I already tested this in my DE and everything looks good.
Please do let me know if it helps you.
Regards,
Mahesh
All Answers
Can you also paste the Trigger here. So that I can able to help you.
Regards,
Mahesh
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.
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.
Apex Class: This will cover all scenarios.
I already tested this in my DE and everything looks good.
Please do let me know if it helps you.
Regards,
Mahesh
Please check the below code.
After creating this class, create a trigger inorder to call this method in it.
Hope this helps you!
Best Regards,
Deepthi
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:
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:
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
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.
Are you using any IDE for writing the code? If so, please copy the code from IDE and paste it directly in the org.
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
The same code worked with my other dev org. Not sure, if some settings must be enabled.
Regards,
Manish
Could you please take screen shot of your error message and paste it here.
Regards,
Mahesh
Please find the screen shot attached below.
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
It's still the same error in the same line number.No matter, what's the varibale name.
Regards,
Manish