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
UndertowUndertow 

How do I make a SOQL query from a custom object data using apex?

How do I make a SOQL query from a custom object data using apex ?

 

I have custom object named News and I want to get all the added news a webpage.

 

This probably very simple to do, but I just couldn't find any examples or advise how to do this.

 

 

Best Answer chosen by Admin (Salesforce Developers) 
imuino2imuino2

If i understand, what you want to do is to retrieve data from a custom object to show it on a page.

Try something like this

 

List<News__c> myNews = [Select Id, Name, myCustomField__c From News__c];

 

That would retrieve all the news on the news object.

 

you can use a Limit or Where Clause to restrict the query

 

 

List<News__c> myNews = [Select Id, Name, myCustomField__c From News__c Limit 100];

Would retrieve the first 100 rows.

 

 

List<News__c> myNews = [Select Id, Name, myCustomField__c From News__c Where Id=: myIdVariable];

Would select the new with the given id where myIdVariable is a variable of type Id containing a News Id.

 

Hope this helps.

Ignacio.

All Answers

imuino2imuino2

If i understand, what you want to do is to retrieve data from a custom object to show it on a page.

Try something like this

 

List<News__c> myNews = [Select Id, Name, myCustomField__c From News__c];

 

That would retrieve all the news on the news object.

 

you can use a Limit or Where Clause to restrict the query

 

 

List<News__c> myNews = [Select Id, Name, myCustomField__c From News__c Limit 100];

Would retrieve the first 100 rows.

 

 

List<News__c> myNews = [Select Id, Name, myCustomField__c From News__c Where Id=: myIdVariable];

Would select the new with the given id where myIdVariable is a variable of type Id containing a News Id.

 

Hope this helps.

Ignacio.

This was selected as the best answer
UndertowUndertow

Thank you! That was exactly what I was lookin for.