I have the following query:

Opportunity opp = [SELECT Id, Name, OwnerID, accountID, CreatedBy from Opportunity where CreatedDate<:Date.today() and CreatedBy.UserRoleId=:Inside_Sales.Id  ];

And this is the error I'm receiving:

No such column 'CreatedBy' on entity 'Opportunity'. 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.

I need to make the query so it only looks for opportunities that where created by users with a specific role. How can I use the CreatedBy field in a query?

1 Answer 1


SELECT Id, Name, OwnerID, accountID, CreatedBy

This syntax is invalid. If you just need the user Id, use CreatedById, or if you want a specific field from the user object, use the dot-notation (e.g. CreatedBy.Name).

  • How can I access the user Role ID? Would it be CreatedBy.Name.UserRoleId? Aug 21, 2017 at 0:11
  • @VictorEcheverría Just CreatedBy.UserRoleId. Coincidentally, you're apparently filtering on just a single user role Id, so do you really need this? CreatedBy.UserRoleId will be the same as Inside_Sales.Id.
    – sfdcfox
    Aug 21, 2017 at 0:12

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .