No such column ‘Owner’ on entity ‘Lead’

apex-codesalesforce

i am getting the following error

Error: Compile Error: No such column 'Owner' on entity 'Lead'. If you are attempting to use a custom field, be sure to append the '__c' after the custom field name. Please reference your WSDL or the describe call for the appropriate names. at line 8 column 17

trigger Lead_Assignment on Lead (after insert) {

     List<Lead> le=trigger.new;
     User us=le.get(0).CreatedBy;
    List<User> u=[SELECT id from USER WHERE At_Work__c=true];    
    List<integer> ii=new List<Integer>();
    for(User uu:u){
    Integer xx= [SELECT count() from Lead WHERE Owner=:uu];


    }



}

there is a field with name Owner in Lead Object why i am getting this error please help in solving this error.

Best Answer

I think you would be better off with a query similar to below:

Integer xx= [SELECT count() from Lead WHERE OwnerId=:uu.Id];

The Owner field on the lead object is actually a reference to the owner user object. Typically this gets populated if you do a query similar to below:

[SELECT id, Owner.Name, Owner.Email FROM Lead]

And to access the Owner fields you would access the lead object owner property:

Lead myLead = [SELECT Id, Owner.Name, Owner.Email FROM Lead limit 1];
System.debug(myLead.Owner.Email);

However, there is a larger issue here. It isn't best practice to query within a loop like this. You are better off using an aggregate query like:

 AggregateResult[] results = [SELECT COUNT(ID), OwnerId FROM LEAD GROUP BY OwnerId WHERE OwnerId IN :listOfUserIds]
Related Topic