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
Siddhant Singh 14Siddhant Singh 14 

expecting ' }' but was 'for'


public class TestTriggerss {
    
     for(Opportunity cp:[Select (Select Id from OpportunityContactRoles ), (SELECT product2.name from OpportunityLineItems) from Opportunity where id=0065g000006qGQZAA2])
     {
            System.debug(cp.ContactId);
            System.debug(cp.Pricebook2Id);
     }
}
 
Best Answer chosen by Siddhant Singh 14
sreenath reddy 21sreenath reddy 21
Hi siddhant
You must put your logic inside a constructor, method or static block, it's depends you requirement.
Below code  i am writing in constructor.
public class TestTriggerss {
    public TestTriggerss(){
        for(Opportunity cp:[Select (Select Id from OpportunityContactRoles ), (SELECT product2.name from OpportunityLineItems) from Opportunity              where id=0065g000006qGQZAA2])
        {
            System.debug(cp.ContactId);
            System.debug(cp.Pricebook2Id);
         }
    }
}

All Answers

Siddhant Singh 14Siddhant Singh 14
I am getting expecting ' }' but was 'for' error but I am not sure what's wrong, Please answer ASAP
sreenath reddy 21sreenath reddy 21
Hi siddhant
You must put your logic inside a constructor, method or static block, it's depends you requirement.
Below code  i am writing in constructor.
public class TestTriggerss {
    public TestTriggerss(){
        for(Opportunity cp:[Select (Select Id from OpportunityContactRoles ), (SELECT product2.name from OpportunityLineItems) from Opportunity              where id=0065g000006qGQZAA2])
        {
            System.debug(cp.ContactId);
            System.debug(cp.Pricebook2Id);
         }
    }
}
This was selected as the best answer
Test Dev 81Test Dev 81
public class TestTriggerss {
    public void myMethod(){
     for(Opportunity cp:[Select (Select Id from OpportunityContactRoles ), (SELECT product2.name from OpportunityLineItems) from Opportunity where id=0065g000006qGQZAA2])
     {
            System.debug(cp.ContactId);
            System.debug(cp.Pricebook2Id);
     }
}
}

 
CharuDuttCharuDutt
Hii Siddhant Singh
You Forgot Give Your Class Method Name
public class TestTriggerss {
   public Static Void MyMethod(){
     for(Opportunity cp:[Select (Select Id from OpportunityContactRoles ),
                         (SELECT product2.name from OpportunityLineItems) from Opportunity])
     {
            System.debug(cp.ContactId);
            System.debug(cp.Pricebook2Id);
     }
  }
}
Please Mark it As Best Answer If It Helps
Thank You! 

 
Suraj Tripathi 47Suraj Tripathi 47

Hi Siddhant Singh ,

Every logic should be written inside method or constructor,but you haven't done so,that's why you are getting this error:

Here is the solution:

public class TestTriggerss {
    public TestTriggerss(){
        for(Opportunity cp:[Select (Select Id from OpportunityContactRoles ), (SELECT product2.name from OpportunityLineItems) from Opportunity where id='0065g000006qGQZAA2'])
        {
            System.debug(cp.ContactId);
            System.debug(cp.Pricebook2Id);
         }
    }
}

Please Mark it as Best Answer if it helps!

Thanks