How to authenticate to Salesforce APIs without client Id and secret

There are multiple ways to authenticate to Salesforce SOAP and REST APIs. Usually the recommended way is to authenticate to REST API is by using OAuth and creating a connected app in Salesforce. Please check the article Using curl to authenticate to Salesforce REST API to see an example.

There is an easy way to bypass OAuth and directly use username+password combination to authenticate against Salesforce SOAP and REST APIs. It is by making SOAP login call. It will return an session Id that you can use against Salesforce SOAP and REST APIs as access token.

Getting access token with username, password and security token

You need make a HTTP call using below format. This is basically sending a login SOAP request. Make sure to use your username, password and security token in line 12 and 13. Also if you are accessing a Salesforce sandbox base part of the URL changes to https://test.salesforce.com

You can see that the API response contains sessionId tag. You can use this sessionId to call Salesforce SOAP or REST apis. Checkout below example of accessing Salesforce Rest API with the session Id. Make sure to use your session ID in authorization header and your base endpoint instead of https://salesforcecodestest-dev-ed.my.salesforce.com.

This method allows you to access Salesforce REST and SOAP apis without a client ID and secret. This is intended for server to server communication. If you use it in a client side app, you will see CORS errors.

Retrieving metadata from Salesforce using package.xml and SFDX CLI

Salesforce ecosystem provides a number of tools to retrieve metadata from connected Salesforce environment. There is an option in Salesforce extension pack for vscode to pull metdata from your connected Salesforce environment. But sometimes you still need to pull metadata from unreleated environments for purposes like committing to a git repository or deploying.

Metadata type names can be tricky and it usually difficult to create a package.xml without some googling. Please checkout article on How to create package.xml in Salesforce - With Samples

Retrieving metadata using package.xml

Once you have package.xml, you are only couple of commands away from retrieving metadata with it. Please follow below steps,

  1. Make sure you have SFDX CLI tool installed. If not download and install it from SFDX CLI Downlaod
  2. Make sure your CLI is connected to the environment from where you need to retrieve metadata. If not use below command to authenticate your Salesforce environment

    sfdx force:auth:web:login -r "https://yourSalesforceEnvironmentUrlHere.com" -a aliasNameHere

  3. Copy package.xml to your command line location and create a folder with name "metadata" to store the metadata and run below command

    sfdx force:mdapi:retrieve -r metadata -k ./package.xml -w 5 -u aliasNameHere

You will be able to see a progress status and SFDX should retrieve code and put in your metadata folder as a zip file


Package.xml samples to retrieve Salesforce metadata

Here we will go through how we can create a package.xml to retrieve metadata from Salesforce. Common metadata type samples are also included.

Metadata type names can be tricky and it usually difficult to create a package.xml without some googling. Here we provide a sample package.xml with examples of common types.

Once you have a package.xml, it is relatively easy to retrieve metadata using it and deploy to other environments. There are multiple options for that- Force.com Migration tool/ANT (Outdated), SFDX (Recommended)- How to retrieve code using SFDX CLI.

Package.xml sample with common metadata types

When you are creating package.xml, copy below simple package.xml file as a starting point

Now copy relevant types from below xml and add to your package.xml. Then go to your org and add members tags for the elements you want to retrieve


Setting up Skill based routing in Salesforce Omni-Channel

Salesforce Omni-channel feature is used to route work to agents in real time based on the routing configuration you define. You can use this feature to route cases, leads, chats, social posts, custom objects etc to the users you need.

Configuring simple Omni-channel setup to route records to agents is complex. It becomes even more complex when you are trying to route records to users/agents based on their skills. Here we outline the steps needed for simple omni-channel routing and skill based omni-channel routing

Simple Omni-Channel Routing

  1. Enable Omni Channel
    • Setup => Check "Enable Omni-Channel" checkbox
  2. Create a Service Channel for your object (can select case, lead, custom object etc)
    • Setup => Service Channel => Click "New" and create a service channel for the object you need
  3. Create Routing configuration
    • Setup => Routing Configuration => New
    • Routing Models (Least Active => Person with least number of cases, Most Available => Can give different capacity for agents - eg junior agents get half capacity)
  4. Create queues and assign routing configuration from previous step
  5. Create Presence Configuration
    • Setup => Presence Configuration => New
    • Can specify max capacity per agent
  6. Create Presence Statuses
    • Setup => Presence Status => New
    • Examples - busy, available etc
  7. Assign statuses to profile
    • Go to the profile and assign statuses to the profile
  8. Add Omni-channel component to app
    • Create/Edit any console app and add "Omni-Channel" component to the utility bar
  9. Go to "Omni Supervisor" tab and you will be able to see status of agents

End to end walkthrough ( Credits to Salesforce Exclusive channel)



Omni-Channel configuration with Skill based routing

  1. Enable Omni Channel
    • Setup => Check "Enable Omni-Channel" checkbox and "Enable Skill-based routing" checkbox
  2. Create a Service Channel for your object (can be case, lead, custom object etc)
    • Setup => Service Channel => Click "New" and create a service channel for the object you need
  3. Create Skills
    • Setup => Skills => New
    • Usually languages and product family names are used as skills
  4. Create Service Resource
    • Created from "Service Resource" tab. This basically registers users as agents who are ready to work on records
    • Service resources can be assigned with skill in "Skills" related list with optional start date and end date
  5. Route cases to agents
    • Routing using Apex
      • Create trigger/process builder that runs apex when a new case is created
      • Insert PendingServiceRouting record in IsReadyForRouting value as false
      • Create "SkillRequirement" record with skills required as junction record between PendingServiceRouting and skill. Use case description/other fields to find skills required
      • Change IsReadyForRouting value to true on PendingServiceRouting records
    • Routing using Flow
      • Create an auto launched flow that gets called on case creation
      • Use "Routing" action and pass (record Id, configuration Id and skill Ids). Salesforce will take care of routing.
      • If flow based routing is used routing configuration also needs to be created
  6. Create Presence Configuration
    • Setup => Presence Configuration => New
    • Can specify max capacity per agent
  7. Create Presence Statuses
    • Setup => Presence Status => New
    • Examples - busy, available etc
  8. Assign statuses to profile
    • Go to the profile and assign statuses to the profile
  9. Add Omni-channel component to app
    • Create/Edit any console app and add "Omni-Channel" component to the utility bar
  10. Go to "Omni Supervisor" tab and you will be able to see status of agents

End to end walkthrough ( Credits to Salesforce Exclusive channel)


Using Lightning Message Service to communicate between lightning components and visualforce

Lightning message service allows you to communicate between all UI technologies of Salesforce (LWC, Aura and Visualforce). This is the recommended way to communicate between lightning components and Visualforce.

Lighting message service communication works in below three steps

  1. Create a new MessageChannel metadata file and deploy to your org
  2. Publish a message from one of the three UI technologies
  3. Subscribe to the message in any of the three UI technologies

1) Creating new MessageChannel

Currently Salesforce do not provide any option in the UI to create MessageChannel. So you have to deploy it as metadata. isExposed setting in metadata allow you to enable communicate between different namespaces/managed packages.

Copy below code metadata to messageChannels folder in your code folder. Name the file as CarSelectionChannel.messageChannel-meta.xml


If you have sfdx installed, you can use below line to deploy the file.

sfdx force:source:deploy -p "pathToFile" -u targetOrgAliasHere

2) Publish event on the MessageChannel

This step varies depending on the whether you are publishing it from LWC or Aura or Visualforce. Our example scenario outlines subscribing and publishing from all three technologies.

3) Subscribing to events on MessageChannel

This step also varies depending on the whether you are publishing it from LWC or Aura or Visualforce. Our example scenario outlines subscribing and listening from all three technologies.

Visualforce Example

Here we use sforce.one methods. This do not work as a stand alone visualforce page. Make sure you are inside lightning experience. ie create a visualforce page with below code, create a tab for that visualforce page and check from lightning experience.

Aura Example

Here we use lightning:messageChannel in the component to subscribe to event.

LWC Example

Here we import @salesforce/messageChannel/CarSelectionChannel__c in JavaScript controller. Also we use subscribe, unsubscribe methods imported from lightning/messageService to subscribe to event.

Please note that if you are just publishing message to channel and not listening to it, you just need to import publish method and the channel. Then you can use the code in handleCarSelect method to publish message to the channel.

Create app builder page to see the result in one page

In order to confirm that the event is getting published and received in all three technologies, put above visualforce, aura and lwc components in a new App builder page and add the page to any app. It should looks like the screenshot at starting of this post.

All ways to communicate between lightning components (Aura and LWC)

There are a lot of different ways to communicate between different types of lightning components (lwc to lwc, lwc to aura, aura to lwc etc). In this blog we will cover all different ways of communicating between different types of lwc and aura components.

Communication patterns between Lightning components


Interactions with Visualforce Page

Still some features like PDF rendering are only supported in Visualforce. There is a chance you might need to interact between lightning components and visualforce components. Some examples are listed below,

  • Embedding visualforce page inside aura [COMING SOON]
  • Embedding aura app inside visualforce [COMING SOON]
  • Embedding visualforce page inside lwc [COMING SOON]
  • Embedding lwc inside visualforce page [COMING SOON]

Query all fields in an object using SOQL

Currently Salesforce does not allow you to query all fields in an object using wild card selectors. Even though it is usually not a best practice to query all fields in an object, sometimes it is useful to query all fields for debugging purposes

Simple way to query all fields in a object in single line is like below,

Database.query('SELECT ' + String.join(new List(Schema.getGlobalDescribe().get('Contact').getDescribe().fields.getMap().keySet()), ',') + ' FROM Contact');


This will be very handy while debugging. But please note that this is not the optimal way of doing this. If you are doing it in multiple places in code, it is recommended to cache the describe calls. An example of caching Schema.getGlobalDescribe() can be found in the blog How to get RecordType Id efficiently in Apex with caching

Script to deploy source/SFDX format code to your sandbox

Salesforce has two differnt formats to store code/metadata now. Conventional Metadata format and new Source/SFDX format.

Usually you can tell the format from folder structure and file format. If code is following some folder structure like force-app/main/default usually it will be in source format. But is it not guaranteed. A better way to confirm is to check object and field metadata files. If field metadata is included inside a .object file, that means that the code is in metadata format. In source/SFDX format, field metadata will be separated into separate files

Script to deploy source format code to sandboxes

Even if we use scratch orgs for development, we still need to use sandboxes for testing, UAT, training etc. We need to convert code in source format to metadata format before can properly deploy the whole codebase to these sandboxes or production. Use below shell script to make this metadata conversion and deployment easy.

How to use

  • Create a shell file with .sh extension inside your project folder and copy paste above code.
  • Make the file executable with sudo chmod u+x pathToTheShellFileHere.
  • Authenticate your sandboxes/production using sfdx force:auth:web:login command
  • Now to deploy you can navigate to the project folder and run ./shellFileName.sh. It will ask for alias of the environment that you want to deploy to. Enter the alias and hit enter. Deployment starts.

Please note that this shell script works in Mac terminal and Linux shell only. If you are using windows, you can try to use bash shell there or write windows versions of the commands into a .bat file.

How to get RecordType Id efficiently in Apex with caching

It is very common needing to get recordtypeId when we are working in Salesforce. There are multiple ways to do it. Here we will go through different ways and find the best way.

Different approaches

  1. SOQL against RecordType standard object

    [SELECT Id, Name FROM RecordType WHERE Name = 'Master' AND SObjectType = 'Account']

    This approach count against the number of SOQL queries.

  2. Get recordtype ID in apex without caching

    Since this approach do not cache operations like Schema.getGlobalDescribe() and sotype.getDescribe().getRecordTypeInfosByName() performance will not be great

  3. Get recordtype ID in apex with caching USE THIS APPROACH

    Here we cache expensive schema describe operations. Allowing multiple calls to the same method to perform efficient and fast in same transaction.

    You can get recordtype Id using the syntax RecordTypeUtilWithCache.getRecordTypeId('Account', 'RecordTypeNameHere');