SOQL Queries for FLS, Objects,List Views,Recently Viewed & Code Coverage etc.,

This blog post helps to understand how and what are the different types of SOQL queries for fetching various types of information from salesforce….

Web Design & Marketing » Contradicting Clouds | Paul Pretorius

1. Query for Field Level Security

Use the WITH SECURITY_ENFORCED clause to enable checking for the field- and object-level security permissions on SOQL SELECT queries, including subqueries and cross-object relationships.

If fields or objects referenced in the SELECT clause using WITH SECURITY_ENFORCED are inaccessible to the user, an exception is thrown, and no data is returned.
SELECT Id, Name, StageName, ClosedDate FROM Opportunity WITH SECURITY_ENFORCED

If above Query throws an exception insufficient permissions. and no data return due to user not have permission to query this then use below type of query.

SELECT Id, (SELECT FirstName, LastName FROM Contacts), (SELECT Description, StageName, CloseDate FROM Opportunities) 
       FROM Account WITH SECURITY_ENFORCED

2. Query Number of Objects list

To fetch all objects with names in your org, use below query.

SELECT MasterLabel, PluralLabel, KeyPrefix, DeveloperName, QualifiedApiName, NamespacePrefix FROM EntityDefinition WHERE IsCustomSetting = false Order by MasterLabel

Incase if you want to get Keyprefix of object

SELECT DeveloperName, KeyPrefix FROM EntityDefinition WHERE IsCustomSetting = false and QualifiedApiName = ‘ContentDocument’

3. Query to get List Views

Select DeveloperName, sobjectType From ListView where SobjectType = ‘Account’

4. Recently Viewed Records

If i want to see who viewed recently any account in your org.

Select Id, Name, Type, LastViewedDate, Profile.Name FROM RecentlyViewed WHERE Type ='Account' ORDER BY LastViewedDate DESC

5. Query Custom Buttons, Links & URLs

If you want to query custom buttons and custom links and URL buttons  you can from info in Weblink object

Select Id, Name, LinkType, PageOrSobjectType From WebLink where NamespacePrefix = ”

you can filter Link type
URL
sControl
javascript
page

6. Query Scheduled Jobs in your org.

All the scheduled jobs are stored in CronTrigger object in Salesforce.

SELECT ID, CronExpression, CronJobDetail.Name, CronJobDetailId, EndTime, NextFireTime, PreviousFireTime, StartTime, State, TimesTriggered FROM CronTrigger

 

7. Query Activity History on a Record

SELECT Id, (SELECT Id, Subject, CreatedDate FROM ActivityHistories WHERE CreatedDate = LAST_N_DAYS: 30) FROM Account WHERE Id = '001G000003Luh8''

8. Org wide Code Coverage

SELECT PercentCovered FROM ApexOrgWideCoverage

Thanks

Jayakrishna

 

Facebook Page: Jayakrishna sfdc Blog – https://www.facebook.com/groups/615517438930811/

Twitter: https://twitter.com/jayakrishnadoc

Blog/website:  https://jayakrishnasfdc.wordpress.com

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google photo

You are commenting using your Google account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s