SOQL stands for Salesforce Object Query Langauge.
It is similar to SOSL except it can only search one object at a time. It does support nested queries though.
Something you can do is create a list and then simply populate it with a SOQL statement. Like so:
// fetching the Records via SOQL List<apex_invoice__c> InvoiceList = new List<apex_invoice__c>(); InvoiceList = [SELECT Id, Name, APEX_Customer__r.Name, APEX_Status__c FROM APEX_Invoice__c WHERE createdDate = today AND APEX_Customer__r.Name != 'Test']; // SOQL query for given criteria // Printing the fetched records System.debug('We have total '+InvoiceList.size()+' Records in List');
If you’re creating a record, sometime’s you’ll want to relate it to a parent record. SOQL can be used to do this:
// Now create the invoice record and relate it with the Customer object // Before executing this, please create a Customer Records with Name 'Customer // Creation Test' APEX_Invoice__c objInvoice = new APEX_Invoice__c(); // Relating Invoice to customer via id field of Customer object objInvoice.APEX_Customer__c = [SELECT id FROM APEX_Customer__c WHERE Name = 'Customer Creation Test' LIMIT 1].id; objInvoice.APEX_Status__c = 'Pending'; insert objInvoice; //Creating Invoice System.debug('Newly Created Invoice'+objInvoice); //Newly created invoice
Here is an example of a nested SOQL query:
// Fetching Child Records using SOQL List<apex_customer__c> ListCustomers = [SELECT Name, Id, (SELECT id, Name FROM Invoices__r) FROM APEX_Customer__c WHERE Name = 'ABC Customer']; // Query for fetching the Child records along with Parent System.debug('ListCustomers '+ListCustomers); // Parent Record List<apex_invoice__c> ListOfInvoices = ListCustomers.Invoices__r; // By this notation, you could fetch the child records and save it in List System.debug('ListOfInvoices values of Child '+ListOfInvoices); // Child records
You’ll notice that this is how to list a list of child records along with a parent record.