1

I'd like to use salesforce rest api in Apex Controller class. Main purpose to use rest api in Apex is to fetch listview colums details(colums order and colums field name) and output visualforce page with xlsx format in visualforce. when I try this sample code in Visualforce using fetch api, it works fine, but in Apex class, it fails. Error Message is shown. FATAL_ERROR System.CalloutException: no protocol: Url:[delegate=https://cs75.salesforce.com]/services/data/v49.0/sobjects/Account/listviews/00B5F0000059jK4UAI/describe

anybody konows how to solve it? thanks for advance.

Sample code is as below.

public class ListViewSampleController {
    public Id listviewId {get; set;}

    public ListViewSampleController(ApexPages.StandardSetController controller) {
        this.listviewId = [SELECT Id FROM ListView WHERE SObjectType = 'Account' AND DeveloperName = 'ALL'].Id;
        fetchListViewDetail();
        
    }
    
    public void fetchListViewDetail() {
        HttpRequest req = new HttpRequest();
        
        req.setEndpoint(URL.getSalesforceBaseUrl() + '/services/data/v49.0/sobjects/Account/listviews/' + this.listviewId + '/describe');
        req.setMethod('GET');
        req.setHeader('Authorization', 'OAuth ' + UserInfo.getSessionId());
        Http http = new Http();
        HTTPResponse res = http.send(req);
        System.debug(res.getBody());
    }
}

UPDATE: I found a solution. This code works well

change like below

req.setEndpoint(Url.getOrgDomainUrl().toExternalForm() + '/services/data/v49.0/sobjects/Account/listviews/' + this.listviewId + '/describe');
2
  • 1
    if its working fine with you add it as a solution and accept it as an answer.
    – Ysr Shk
    Nov 5, 2020 at 8:00
  • 1
    thank you. I add my solution.
    – gabriel
    Nov 5, 2020 at 9:28

1 Answer 1

3

This is a solution. you don't need a remote site setting when you use 'URL.getOrgDomainUrl' instead of 'URL.getSalesforceBaseUrl'.

req.setEndpoint(URL.getOrgDomainUrl().toExternalForm() + '/services/data/v49.0/sobjects/Account/listviews/' + this.listviewId + '/describe');

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .