You need to sign in to do that
Don't have an account?
Contains method in list
Hi all ,
I am creating an apex trigger(before insert,before update) to add the amount of opportunities created by a single user should not exceed n 1000 a single day. I have successfully created for inserting..
But for updating ,while taking sum it is adding the both the record to be updated's existing value and new to be updated value , which it shouldnt be doing and should take only the new updated value for calculating.
so i wrote a if loop with contains , but i am getting compiler error .Please help
list<opportunity> op2= [Select id,name,amount from opportunity where CreatedByID = :UserInfo.getUserId() and createdDate = TODAY];
decimal sum=0.0;
// Query for adding existing opportunity amount
if( op2.size() > 0)
{
for (opportunity o : op2)
{
if(op1.contains(o.id) == 1)
{
sum=sum+o.amount;
System.debug('records loop');
}
}
}
error: Compile Error: Method does not exist or incorrect signature: void contains(Id) from the type List<Opportunity> at line 17 column 21
please let me know what i am doing wrong.
Thanks in advance
I think op1 has list<Opportunity> type is Object, but you are comparing with Id (single Id).
compare like this. Kindly mark this as solved if the reply was helpful.
Thanks
Shaik
All Answers
I think op1 has list<Opportunity> type is Object, but you are comparing with Id (single Id).
compare like this. Kindly mark this as solved if the reply was helpful.
Thanks
Shaik
what version of the apex class you are running this code under?
Hi Abdul , Shaiks,
I forgot to paste the op1 definition.
I am able to run the trigger for before Insert but it doesnt work for update as it keeps adding the old and the new updated value to sum .
Apex version is 45.0
Any suggestion would be welcome.
Here is my code :
trigger UserAmountUpdate on Opportunity (before update, before insert) {
//All the new records getting insert or updated
List<opportunity> op1=trigger.new;
system.debug(op1);
//All the old records
list<opportunity> op2= [Select id,name,amount from opportunity where CreatedByID = :UserInfo.getUserId() and createdDate = TODAY];
decimal sum=0.0;
// Query for adding existing opportunity amount
if( op2.size() > 0)
{
for (opportunity o : op2)
{
if(op1.contains(o)==False) // This doesnt work as the op1 records have all the fields of opportunity and op2 has only the above.
{
sum=sum+o.amount;
System.debug('inside record loop');
System.debug('id' +o.id + 'sum'+sum);
}
}
}
// Query for adding trigger added amount
for (opportunity o1 : op1)
{
sum=sum+o1.amount;
if(sum > 1000)
{
o1.addError('You cannot create opportunities worth more than 1000 in a single day');
}
}
Thanks in advance,
Afreen