SOQL – Salesforce Object Query Language

–Use the Salesforce Object Query Language (SOQL) to construct simple but powerful query strings for the queryString parameter in the Query() call. Similar to the SELECT command in Structured Query Language (SQL), SOQL allows you to specify the source object (such as Account), a list of fields to retrieve, and conditions for selecting rows in the source object.

**Note:

SOQL does not support all advanced features of the SQL SELECT command. For example, you cannot use SOQL to perform arbitrary join operations, use wildcards in field lists, or use calculation expressions.

Sample SOQL Queries:

How to query user information from Role?
SELECT Id, Name FROM User WHERE UserRole.Name = ‘Executive’

How to Query Opportunity Team Member
SELECT Name, TeamMemberRole, User.Email FROM OpportunityTeamMember

How to query setup objects in Developer console in Salesforce?
Enable “Use Tooling API” to query setup objects in Developer console in Salesforce.

tooling api

How to get Batch Apex status using SOQL?
SELECT Status, ExtendedStatus FROM AsyncApexJob WHERE Id = ‘7779000000k2e5T’

SetupEntityAccess
SetupEntityAccess represents the enabled setup entity access settings (such as for Apex classes) for the parent PermissionSet. To grant users access to an entity, associate the appropriate SetupEntityAccess record with a PermissionSet that’s assigned to a user. This object is available in API version 25.0 and later.

Sample SOQL:
SELECT Id, ParentId, Parent.Name, SetupEntityId FROM SetupEntityAccess

How to fetch report name and format using SOQL in Salesforce?
Sample SOQL:

SELECT Name, Format FROM Report

here
Name – Name of the report.
Format – Format of the report(Tabular, Summary, Matrix, MultiBlock(Joined report)).

How to get the batch status using Apex in Salesforce?
Sample Code:

Batch_Class_Name obj = new Batch_Class_Name();
Id batchId = Database.executeBatch();

AsyncApexJob job = [SELECT Id, Status, JobItemsProcessed, TotalJobItems, NumberOfErrors FROM AsyncApexJob WHERE ID =: batchId];

How can I get a list of records where two fields have the same or different values using SOQL?
1. Create a formula field

IF(Field1__c != Field2__c, 1, 0)

2. Write a SOQL as mentioned below

SELECT Id from Account WHERE FormulaField__c = 1

How to fetch records form recycle bin in Salesforce?
“ALL ROWS” keyword is used to fetch records form recycle bin in Salesforce

SELECT Id, AccountId FROM Contact WHERE IsDeleted = true ALL ROWS

How to fetch Campaign information from Lead in Salesforce?

SELECT Id, (SELECT Id FROM CampaignMembers) FROM lead

How to get all the trigger names from Salesforce organization?

SELECT Id, Name FROM ApexTrigger

SOQL to find code coverage in Salesforce

SELECT ApexClassOrTrigger.Name, NumLinesCovered, NumLinesUncovered FROM ApexCodeCoverageAggregate ORDER BY ApexClassOrTrigger.Name ASC

How to find number of lines covered and uncovered in Apex class using SOQL?

SELECT ApexClassOrTrigger.Name, NumLinesCovered, NumLinesUncovered FROM ApexCodeCoverageAggregate ORDER BY ApexClassOrTrigger.Name ASC

How to get Product Name from Opportunity Line item in SOQL?

SELECT ProductCode__c, PricebookEntry.Product2.Name FROM OpportunityLineItem

How to get Product Id from Opportunity Line item in SOQL?

SELECT PricebookEntry.Product2Id FROM OpportunityLineItem

PricebookEntry.Product2Id will give you the Product Id associated with that Opportunity Product(Opportunity Line Item).

If the above workaround didn’t help you to fix the issue, use the below workaround.

Set the Apex class version 30.0 and more and then execute the below SOQL.
SELECT Product2Id FROM OpportunityLineItem

How to get Account Name from Contact in SOQL?
SELECT Id, Name, Account.Name FROM Contact

Cheers!!!

Leave a Comment

Your email address will not be published. Required fields are marked *

Select Language »