In this article, we will explore some SOQL queries that can be utilized during our daily Salesforce job responsibilities.
- Querying AsyncApexJobs: To query AsyncApexJob records in Salesforce, you can retrieve information about asynchronous Apex jobs such as batch jobs, future methods, and queueable jobs.
SELECT Id, ApexClass.Name, JobType, Status, CreatedDate, CompletedDate, MethodName, ExtendedStatus
FROM AsyncApexJob
WHERE CreatedDate = LAST_N_DAYS:7 and jobtype in ('ScheduledApex', 'BatchApexWorker', 'Future', 'Queueable')
- Querying Aggregate Data: Aggregate queries allow you to perform calculations on your data, such as counting records, summing values, finding minimum or maximum values, and averaging values.
SELECT COUNT() FROM Account
SELECT SUM(Amount) totalAmount FROM Opportunity
SELECT MAX(Amount) maxAmount FROM Opportunity
SELECT Industry, SUM(Amount) totalAmount FROM Account GROUP BY Industry
- Querying Attachments: Attachments are related to specific objects, such as Account, Contact, and Opportunity, using the standard ParentId field where you can specify the corresponding Account, Contact, or Opportunity record.
SELECT Id, Name, Parent.Name, Parent.Type
FROM Attachment
WHERE ParentId = <accountID>
- Querying Custom Labels: To query Custom Labels in Salesforce, you need to enable the “Use Tooling API” option in the Salesforce Developer Console or Salesforce inspector.
SELECT Id, MasterLabel, Name, value, NamespacePrefix
FROM CustomLabel
- Querying Custom Label Translations: To query the list of different language translations for a Custom label, you can use the ExternalStringLocalization. The ExternalStringId refers to the custom label’s ID, and you must enable the “Use Tooling API”
SELECT Id, ExternalStringId, ExternalString.Value, Value, Language
FROM ExternalStringLocalization
ORDER BY ExternalString.MasterLabel, Language
- Querying History Tracking: When you enable field history tracking for an object, the changes are stored in a special history Object dedicated to that particular object.
The naming convention for these objects is:
[standardObjectName]History e.g. AccountHistory, ContactHistory etc.
[customObjectName]__History e.g. Car__History, Brand__History etc.
//Standard Object
SELECT Id, AccountId, Field, OldValue, NewValue, CreatedDate
FROM AccountHistory
WHERE AccountId = <accountId>
ORDER BY CreatedDate DESC
//Custom Object
SELECT Id, Field, OldValue, NewValue, CreatedDate
FROM Brand__History
WHERE ParentId = <brandId>
ORDER BY CreatedDate DESC
//21 SOQL Queries - flutterant
- Querying Approval Process Information: You can query for approval processes associated with a specific object (like Opportunity, Case, etc.) using the ProcessInstance object.
SELECT Id, Status, TargetObjectId, ProcessDefinition.Name
FROM ProcessInstance
WHERE TargetObject.Type in ('Opportunity', 'Contract')
- Querying Approval Process History: In Salesforce, the ProcessInstanceHistory object is not directly quarriable via SOQL, Instead, you typically access information about process instance histories indirectly through related objects.
SELECT Id, Status, ProcessDefinition.Name, TargetObjectId,
(SELECT Id, ActorId, CreatedDate, Comments, StepStatus
FROM StepsAndWorkitems ORDER BY CreatedDate ASC)
FROM ProcessInstance
WHERE TargetObjectId = <targetObjd>
ORDER BY CreatedDate DESC
//21 SOQL Queries - flutterant
- Querying Users by Profile: To query a list of users belonging to a specific profile in Salesforce, you can use the User object
SELECT Id, Name, Profile.id, Profile.name, IsActive
FROM User
WHERE Profile.name = 'System Administrator'
- Querying Users by Permission Set: To query all users assigned to a specific Permission Set in Salesforce, you need to use the PermissionSetAssignment object.
SELECT AssigneeId, Assignee.Name, Assignee.Username, PermissionSet.name, PermissionSetId
FROM PermissionSetAssignment
WHERE PermissionSetId = <permissionSetId>
//21 SOQL Queries - flutterant
- Querying Public Groups of a User: To query the public groups that a specific user belongs to, you would use the GroupMember object in Salesforce. The below gives the directs groups, The group which contains the User as Group Member
SELECT Group.Name,UserOrGroup.name, Group.Type
FROM GroupMember
WHERE UserOrGroupId = <userId>
- Querying User LoginHistory: The LoginHistory object stores information about login events for all users in your Salesforce org.
SELECT Id, LoginTime, LoginType, LoginUrl, Platform, Application, Status, UserId, UserName
FROM LoginHistory
WHERE UserId = <usrId>
ORDER BY LoginTime DESC
- Querying List of Apex Classes: We can Query all list of apex classes using ApexClass which is a metadata object that represents Apex classes.
SELECT id, Name, ApiVersion, CreatedBy.name, NamespacePrefix
FROM ApexClass
- Querying List of Apex Triggers: The ApexTrigger stores information about triggers that you create in your Salesforce organization to perform custom actions before or after DML operations.
SELECT Id, Name, TableEnumOrId, ApiVersion, Status, NamespacePrefix
FROM ApexTrigger
- Querying List of Visualforce Pages: We can Query list of Visualforce Pages using ApexPage Object.
SELECT Id, Name, ApiVersion, MasterLabel, ControllerType, NamespacePrefix
FROM ApexPage
- Querying Lightning Components(Aura): The AuraDefinitionBundle object in Salesforce represents a bundle of Aura components.
SELECT Id, DeveloperName, NamespacePrefix, ApiVersion, Description, MasterLabel
FROM AuraDefinitionBundle
- Querying List of Debug logs: We can Query all debug logs in Salesforce Org using ApexLog Object.
SELECT id, LogUserId, DurationMilliseconds, LogUser.name,Status, Operation
FROM ApexLog
Order by StartTime desc
- Querying Org Wide Email Addresses: To query the Org Wide Email Address in Salesforce, you can use OrgWideEmailAddress object. This object allows you to manage and query the email addresses that are available for use by all users.
SELECT Id, DisplayName, Address
FROM OrgWideEmailAddress
- Querying Email Templates: We can query List of Email templates by EmailTemplate object in your Salesforce org.
SELECT Id, Name, DeveloperName, Subject, HtmlValue, Body, IsActive, TemplateType, Folder.Name
FROM EmailTemplate
- Querying Chatter Feed: To query Chatter feed items in Salesforce we can use FeedItem object. The FeedItem object represents entries in the Chatter feed, such as posts, comments, and likes.
SELECT Id, Body, ParentId, CommentCount, BestComment.CommentBody, LikeCount, Status, Type
FROM FeedItem
WHERE ParentId = <parentId>
//21 SOQL Queries - flutterant
- Querying Flow Details: To query information related to flows that are available in the Salesforce UI, you would typically use the FlowDefinition object. “Use Tooling API” should be enabled.
SELECT Id, Label, ApiName, IsActive, TriggerType, RecordTriggerType, ProcessType FROM FlowDefinitionView
//21 SOQL Queries - flutterant
List of various SOQL Quarriers that may useful for debugging your salesforce applications. Please add comments if you have any questions, and feel free to share this information with your Salesforce developers if you find it useful. Thanks!