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.