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?


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 '17 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 '17 at 0:12

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.