function readOnly(count){ }
Starting November 20, the site will be set to read-only. On December 4, 2023,
forum discussions will move to the Trailblazer Community.
+ Start a Discussion
Per Støvring SørensenPer Støvring Sørensen 

'Raw' URL to use REST API

I want to use a 'raw' URL to send a REST API call to SFDC e.g.:
https://my_salesforce_instance.com/services/data/v47.0/query?q=SELECT+id,name+FROM+user+WHERE+Alias='ebrow'&Bearer=<Token Value>
but I am getting "Invalid Session ID". Naturally I have verified the <Token Value> using PowerShell to initiate the call.
Is there a way/syntax where I can contain all the required information in a URL string so I can do the call directly from my browser?
Alain CabonAlain Cabon
Why don't you use: sfdx force:data:soql:query -u TARGETUSERNAME -q QUERY

The TARGETUSERNAME can be an ALIAS by opening a browser with force:auth:web:login previously.

C:\> sfdx auth:web:login -a SETALIAS -r INSTANCEURL

Example :

C:\>sfdx force:auth:web:login -a SANDBOX1 --instanceurl=https://test.salesforce.com  ( open a browser, enter your user/password and allow )

Successfully authorized <your user name> with org ID <your id>
You may now close the browser

C:\>sfdx force:org:list  (quite slow, check, connected for the alias name)

C:\>sfdx force:data:soql:query -u SANDBOX1 -q "select id,name from account limit 10"
 
  • auth:web:login  Authorizes a Salesforce org by opening a browser so you can log in through salesforce.com.
  • data:soql:query Executes a SOQL query.

Installation of the SFDX CLI:  https://developer.salesforce.com/tools/sfdxcli
 
Per Støvring SørensenPer Støvring Sørensen
Thanks, But I need to be able to execute this from a browser. If from a shell, I do have a PowerShell script that does it.
Alain CabonAlain Cabon
From a browser, but with a connected user on Salesforce.

1) You need Salesforce API Library for JavaScript applications (both on Node.js and web browser) http://jsforce.github.io/​​​​​​​ (http://jsforce.github.io/)

https://github.com/jsforce/jsforce


2) From a browser, but with an already connected user on Salesforce ( just UserInfo.getSessionID() in Apex )
//Get layouts where field is used
    public List<String> getLayoutUsage() { 
        setWhereUsed();

        List<String> retVal = new List<String>();
        
        //Pull layout rows
        String response = getHTTP('/services/data/v37.0/tooling/query/?q=SELECT+EntityDefinitionID,LayoutType,ManageableState,Name,NamespacePrefix,TableEnumOrID+FROM+Layout+WHERE+TableEnumOrId=\'' + queryBy + '\'');
        
        //Pull URL for each layout
		Map<String, Object> resultMap = (Map<String, Object>) JSON.deserializeUntyped(response);
        List<Object> records = (List<Object>) resultMap.get('records');
...

}
 
//Submit an HTTP GET request
    public String getHTTP(String svcURL) {
        //HTTP objects
        HttpRequest req = new HttpRequest();
        req.setHeader('Authorization', 'Bearer ' + UserInfo.getSessionID());
        req.setHeader('Content-Type', 'application/json');
        
        String domainUrl = URL.getSalesforceBaseUrl().toExternalForm();
        
        req.setEndpoint(domainUrl + svcURL);
        req.setMethod('GET'); 
                
        Http h = new Http();
        HttpResponse res = h.send(req);
	
        return res.getBody();
    }

https://github.com/regularcoder/deepfield/blob/master/classes/DeepFieldController.cls


2) The problem of the workbench (PHP) is that it is a connected app already known by all the orgs that can use a OAUTH process login directly.

private function oauthProcessLogin($code, $hostName, $apiVersion, $startUrl) {

https://github.com/forceworkbench/forceworkbench/blob/master/workbench/controllers/LoginController.php

 https://github.com/forceworkbench/forceworkbench/tree/master/workbench/soapclient