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
Albert RaulAlbert Raul 

Help me with the error.

public class FindOpp {    
   public static void function(){
      Date sDate = Date.today().addMonths(-1);
      Date eDate = Date.today();
      List<Account> accList = [SELECT Id, Name, (SELECT Id FROM Opportunity WHERE IsClosed = true AND StageName = 'Closed Won' AND CloseDate >= :sDate AND CloseDate <= :eDate) FROM Account];
     List<Account> AccountData = new List<Account>();
     for (Account acc : accList) {    
         if (acc.Opportunities.size() >= 10) {        
            AccountData.add(acc);    
            }
      }
      for (Account acc : AccountData) {    
          system.debug(acc.Name);
      }    
   }
}

ERROR at Row:1:Column:34 Didn't understand relationship 'Opportunitiy' in FROM part of query call. If you are attempting to use a custom relationship, be sure to append the '__r' after the custom relationship name. Please reference your WSDL or the describe call for the appropriate names.
Arun Kumar 1141Arun Kumar 1141
Hi Albert,

You should use Opportunities in sub query instead of Opportunity in sub query if you want to avoid this problem.

use this code

[SELECT Id, Name, (SELECT Id FROM Opportunities WHERE IsClosed = true AND StageName = 'Closed Won' AND CloseDate >= :sDate AND CloseDate <= :eDate) FROM Account];

Please mark it as best answer if it helps you

Thanks


 
SubratSubrat (Salesforce Developers) 
Hello Albert,

To fix the error, update the relationship name in the query from "Opportunitiy" to "Opportunities" as follows:
List<Account> accList = [SELECT Id, Name, (SELECT Id FROM Opportunities WHERE IsClosed = true AND StageName = 'Closed Won' AND CloseDate >= :sDate AND CloseDate <= :eDate) FROM Account];
By making this adjustment, the query should recognize the correct relationship and allow you to retrieve the desired information.

If this helps , please mark this as Best Answer.
Thank you.
Shuvam PatraShuvam Patra

The error you're encountering is due to a typo in your SOQL query. The object name should be 'Opportunities', not 'Opportunitiy'.

Here is the corrected code:

public class FindOpp {

public static void function(){

Date sDate = Date.today().addMonths(-1);

Date eDate = Date.today();

List<Account> accList = [SELECT Id, Name, (SELECT Id FROM Opportunities WHERE IsClosed = true AND StageName = 'Closed Won' AND CloseDate >= :sDate AND CloseDate <= :eDate) FROM Account];

List<Account> AccountData = new List<Account>();

for (Account acc : accList) {

if (acc.Opportunities.size() >= 10) {

AccountData.add(acc);

}

}

for (Account acc : AccountData) {

system.debug(http://acc.Name);

}

}

}

This code is doing the following:

  1. Fetching all accounts and their respective opportunities that were closed as 'Won' in the last month.
  2. Checking if any of these accounts have 10 or more such opportunities.
  3. If they do, the account is added to a separate list, AccountData.
  4. Finally, it logs the names of the accounts in AccountData.

Remember to always double-check the object and field names in your SOQL queries to avoid such errors.