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
afreen nisa 2afreen nisa 2 

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

Best Answer chosen by afreen nisa 2
Shaik Naga janiShaik Naga jani
HI afreen,

I think op1 has list<Opportunity> type is Object, but you are comparing with Id (single Id).
compare like this.
if(op1.contains(o)) {
  sum=sum+o.amount;
  System.debug('records loop');
}
Kindly mark this as solved if the reply was helpful.
Thanks
Shaik
 

All Answers

Shaik Naga janiShaik Naga jani
HI afreen,

I think op1 has list<Opportunity> type is Object, but you are comparing with Id (single Id).
compare like this.
if(op1.contains(o)) {
  sum=sum+o.amount;
  System.debug('records loop');
}
Kindly mark this as solved if the reply was helpful.
Thanks
Shaik
 
This was selected as the best answer
Abdul KhatriAbdul Khatri
how is op1 is defined?

what version of the apex class you are running this code under?
afreen nisa 2afreen nisa 2

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