Showing posts with label SOQL. Show all posts
Showing posts with label SOQL. Show all posts

Wednesday, December 16, 2020

Access Custom Metadata Type data without Using the SOQL in Salesforce

From Spring'21 onwards we can get the custom metadata record information without using the SOQL.

Demo:

For the demo, I created the Country and Amount(Country_And_Amount__mdt) Custom Metadata type object and records.

Country_And_Amount__mdt object.


Sample Code:

// Getting all metadata values 
Map<String, Country_And_Amount__mdt> mapValues = Country_And_Amount__mdt.getAll();

for(Country_And_Amount__mdt mdt : mapValues.values()) {
    System.debug('Name => '+mdt.Label);
    System.debug('DeveloperName => '+mdt.DeveloperName);
    System.debug('Amount__c => '+mdt.Amount__c);
}


// Getting metadata record based on value
Country_And_Amount__mdt objMdt = Country_And_Amount__mdt.getInstance('United_States');
System.debug('objMdt => '+objMdt);


Output:



Thursday, October 8, 2020

How to Query the Lookup Filter Information Using SOQL in Salesforce

 To Query the lookup filter information we can use the LookupFilter object.

Note: Select the Tooling API option when you querying the data

Example:

Select id, Active, IsOptional, DeveloperName, SourceObject, TargetEntityDefinition.DeveloperName, SourceFieldDefinitionId, SourceFieldDefinition.DeveloperName From lookupFilter
Result:


Monday, June 8, 2020

How to get Org Instance name using SOQL

SOQL Query
String strInstanceName = [Select instanceName From Organization LIMIT 1].instanceName;
System.debug('strInstanceName ===> '+strInstanceName);

Output

Thursday, April 2, 2020

How to query custom buttons/links info using SOQL in salesforce

Salesforce internally stores the all custom buttons and custom links and URL buttons info in Weblink object.

SOQL

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

you can filter Link type
URL
sControl
javascript
page
flow—Reserved for future use.

Output

Monday, March 9, 2020

Check Lead Status picklist values marked as converted or not In Apex

In Order to retrieve the Lead Status Picklist Values we have Standard Salesforce object "LeadStatus".

LeadStatus object represents the status of a Lead, such as Open, Qualified or Converted.

This object represents a value in the lead status picklist (see Lead Status Picklist). The lead status picklist provides additional information about the status of a Lead, such as whether a given status value represents a converted Lead. Query this object to retrieve the set of values in the lead status picklist, and then use that information while processing Lead objects to determine more information about a given lead.

SOQL
SELECT Id, MasterLabel FROM LeadStatus WHERE IsConverted = true

Result

Resource
LeadStatus

Monday, December 16, 2019

Use of VisualforceAccessMetrics object in the Salesforce

This object is used to query the metrics of the visualforce page in your org.
Using VisualforceAccessMetrics, you can track the number of views each Visualforce page in your org receives in a 24-hour time period. To find out how many views a page got over the course of multiple days, you can query multiple VisualforceAccessMetrics objects for the same ApexPageId.

Page views are tallied the day after the page is viewed, and each VisualforceAccessMetrics object is removed after 90 days.

To get the metrics of the visualforce page data, make a SOQL query in Workbench to get information from the VisualforceAccessMetrics object.

1. Log in to Workbench.
2. Select production/sandbox.
3. select 'SOQL Query'.

SOQL Query
SELECT ApexPageId, DailyPageViewCount, Id, ProfileId, MetricsDate, LogDate FROM VisualforceAccessMetrics

Output

Tuesday, November 19, 2019

How to get recently viewed information of user in org using SOQL

Salesforce stores the recently viewed information in RecentlyViewed object
RecentlyViewed represents records that the current user has recently viewed or referenced (by viewing a related record).

If you want to query the multiple sObject records recently viewed information.
Ex: I want to see the recently viewed account and contact records in org.
Select Id, Name, Type, LastViewedDate, Profile.Name FROM RecentlyViewed WHERE Type IN ('Account', 'Contact') ORDER BY LastViewedDate DESC



You can also use the LastViewdDate field on the sObject. 
Ex: I want to see only recently viewed Account records.
SELECT Id, Name, LastViewedDate FROM Account WHERE LastViewedDate != NULL ORDER BY LastViewedDate DESC limit 5


Resource
RecentlyViewed Object

Friday, October 11, 2019

How to get Scheduled Jobs information Using SOQL

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

SOQL Query

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

Result


Refereence
CornTrigger

Monday, September 23, 2019

How to check overall code coverage Percentage in Salesforce using SOQL

Note: Use Tooling API to Query.

SOQL Query
SELECT PercentCovered FROM ApexOrgWideCoverage

Result

Monday, August 26, 2019

How to get the Custom Notification Type Id Using SOQL in Salesforce

Note: Select Tooling API to query if you are using the Developer console select the tooling API.

SOQL:
SELECT Id, CustomNotifTypeName FROM CustomNotificationType

Result

Friday, August 23, 2019

How to query all Private Reports in Salesforce

SOQL Query
SELECT Id, Name, Owner.Name FROM Report USING SCOPE allPrivate

Result

Thursday, August 8, 2019

How to get activity history of a object using SOQL in Salesforce

ActivityHistory is a standard object is displayed in a related list of closed activities—past events and closed tasks—related to an object.

SOQL Query
SELECT Id, (SELECT Id, Subject, CreatedDate FROM ActivityHistories WHERE CreatedDate = LAST_N_DAYS: 90) FROM Account WHERE Id = '001G000001uoKEL'
Result

Resource
https://developer.salesforce.com/docs/atlas.en-us.object_reference.meta/object_reference/sforce_api_objects_activityhistory.htm

Thursday, August 1, 2019

How to get Current User Timezone Using SOQL

This simple SOQL query returns the current user timezone.

SOQL Query
SELECT Id, TimeZoneSidKey FROM User WHERE Id = '005B0000005lewT'
Result

Saturday, July 27, 2019

How to check Forecast is enabled or not for User using SOQL

This simple query checks the forecast is enabled or not user
SELECT Id, ForecastEnabled FROM User where Id = '005B0000005lewT'
Result

Resource
https://developer.salesforce.com/docs/atlas.en-us.api.meta/api/sforce_api_objects_user.htm

How to query Salesforce Logged in Users information Using SOQL

This simple SOQL query returns the User Information and logged information in salesforce.
It returns the type of sessions user have a logged-in system.

SOQL Query
Select Id, UsersId, Users.Name, UserType, SourceIp, SessionType, SessionSecurityLevel, ParentId, LoginType, LastModifiedDate, CreatedDate From AuthSession
Result

Resource
AuthSession SOAP API

Tuesday, June 25, 2019

How to query Lightning Components Using SOQL


SOQL Query
SELECT Id, DeveloperName FROM AuraDefinitionBundle WHERE DeveloperName = 'CreateNewCaseComponent'

Result

Thursday, June 13, 2019

How to retrieve list views of any object using SOQL

Example

SOQL Query
Select DeveloperName, sobjectType From ListView where SobjectType = 'Account'
Result:

How to check object keyprefix using SOQL

Example:

SOQL Query
SELECT DeveloperName, KeyPrefix FROM EntityDefinition WHERE IsCustomSetting = false and QualifiedApiName = 'ContentDocument'
Result

Saturday, February 9, 2019

How to Query all Object Names in org using SOQL

This post explains how to check all object names using SOQL.
Use below simple query to get all object names org.

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

Result

Resource
EntityDefinition Tooling API 

Happy Learning!!

Saturday, December 29, 2018

How to Enforce Field-Level Security Permissions for SOQL Queries?


This post explains how to enforce field level security permissions for SOQL queries,
In Spring' 19 release salesforce introducing this feature, at the time of writing this post this feature in Beta stage.
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. Although performing these checks was possible in earlier releases, this clause substantially reduces the verbosity and technical complexity in query operations.

How it Works?

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.

Example: 1
SELECT Id, Name, StageName, ClosedDate FROM Opportunity WITH SECURITY_ENFORCED
if the stage name is inaccessible to the user it throws an exception insufficient permissions. and no data return.

Example: 2
SELECT Id, (SELECT FirstName, LastName FROM Contacts), (SELECT Description, StageName, CloseDate FROM Opportunities) 
       FROM Account WITH SECURITY_ENFORCED
if the First Name, stage name is inaccessible to the user it throws an exception insufficient permissions. and no data return.

Resource
http://releasenotes.docs.salesforce.com/en-us/spring19/release-notes/rn_apex_select_with_security_enforced.htm