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
Saravana RavikumarSaravana Ravikumar 

How to query custom objects in Apex ? Is there any special things I need to do to query 'custom objects or fields' compared to querying 'standard objects or fields' ??

SwethaSwetha (Salesforce Developers) 
HI Ravi,
I don't think there are any special things you need to do to query custom objects or fields compared to querying standard objects or fields. Just make sure that you have the necessary permissions to access the custom object and fields that you want to query.

Steps:
> You would define the custom object that you want to query in Apex. You can do this by creating a custom object in Salesforce or by using an existing custom object.

> Then write a SOQL query to retrieve the data from the custom object. The syntax for querying custom objects is the same as for standard objects. For example, to retrieve all records from a custom object called "MyCustomObject__c", you can use the following query:
List<MyCustomObject__c> customObjects = [SELECT Id, Name, CustomField__c FROM MyCustomObject__c];

> Iterate over the results of the query to access the data. You can use a for loop to iterate over the list of custom objects. For example:
for (MyCustomObject__c customObject : customObjects) {
    System.debug(customObject.Name);
}

Related: https://developer.salesforce.com/forums/?id=906F00000008mvmIAA

https://www.forcetalks.com/salesforce-topic/how-to-query-for-custom-object-in-salesforce/

If this information helps, please mark the answer as best. Thank you
Arun Kumar 1141Arun Kumar 1141
Hi @Saravana,

In order to access custom objects or custom fields in salesforce you need to add  '__c'  to the name of your custom objects or fields.

For E.g. :
 
List<CustomObject__c> customObjects = [SELECT Id, Name, CustomField__c FROM CustomObject__c WHERE CustomField__c = 'Some Value'];

Hope this helps.

Thank you.​​​​​​​