• SeanOng
  • NEWBIE
  • 5 Points
  • Member since 2017
  • Technical Architect

  • Chatter
    Feed
  • 0
    Best Answers
  • 0
    Likes Received
  • 0
    Likes Given
  • 6
    Questions
  • 6
    Replies
Hi all

I'm trying to call the REST API from within Apex Code within my own Org. This is to retreive the List View soql, which appears to only be achievable using this method.

I'm using this library someone wrote: https://github.com/shrutis22/Record-Lookup/blob/master/src/classes/ListViewAPI.cls

This is the code in question from that library:
public static ListViewsResponse getListViews( String sObjectName ) {
        Http http = new Http();
        HttpRequest httpReq = new  HttpRequest();
        
        String orgDomain = Url.getSalesforceBaseUrl().toExternalForm();
        
        String endpoint = orgDomain + '/services/data/v37.0/sobjects/' + sObjectName + '/listviews';
        
        httpReq.setEndpoint( endpoint );
        httpReq.setMethod( 'GET' );
        httpReq.setHeader( 'Content-Type', 'application/json; charset=UTF-8' );
        httpReq.setHeader( 'Accept', 'application/json' );
        
        String sessionId = 'Bearer ' + UserInfo.getSessionId();
        
        httpReq.setHeader( 'Authorization', sessionId );
        
        HttpResponse httpRes = http.send( httpReq );
        
        ListViewsResponse listViewResp = ( ListViewsResponse ) JSON.deserialize( httpRes.getBody(), ListViewsResponse.class );
        
        return listViewResp;
    }
This is how I call it:
@AuraEnabled
  public static List<LocationRecord> getLocationRecords(String sObjectType, String listViewDevName) {
    ListView lv = [Select Id From ListView Where sObjectType = :sObjectType And DeveloperName = :listViewDevName Limit 1];

    String soql = ListViewAPI.getListViewQuery(listViewDevName, sObjectType);
    System.debug('XXX = '+soql);
  }

However, in the http response, I'm getting the following:
[{"message":"This session is not valid for use with the REST API","errorCode":"INVALID_SESSION_ID"}]
I've added things like Remote Sites to my own Org.

Many thanks for your help.

 
Hi Expert Devs!

I've got the following call successfully done in POSTMAN using the x-www-form-urlencoded:

User-added image

I'm trying to replicate this in Salesforce but I'm getting a 400 error code (invalid request). Do you know how I can successfully replicate the POSTMAN call in Salesforce?
    MYOB_Integration__c myobIntg = MYOB_Integration__c.getOrgDefaults();
    clientId = myobIntg.Client_ID__c;
    clientSecret = myobIntg.Client_Secret__c;
    refreshToken = myobIntg.Refresh_Token__c;
    authUrl = myobIntg.Authorise_URL__c;
    cfUrl = myobIntg.Company_File_URL__c;
    DateTime lastSync = myobIntg.Last_Account_Sync__c!=null?myobIntg.Last_Account_Sync__c:System.now();
    Map<String, String> authJson = new Map<String, String>();
    // Get the Access Token using the Refresh Token.
    Http http = new Http();
    HttpRequest request = new HttpRequest();
    request.setEndpoint(authUrl);
    request.setMethod('POST');    
    request.setHeader('Content-Type', 'application/x-www-form-urlencoded');
    String payload = 'client_id='+EncodingUtil.urlEncode(clientId,'UTF-8')+'&client_secret='+EncodingUtil.urlEncode(clientSecret,'UTF-8')+'&refresh_token='+EncodingUtil.urlEncode(refreshToken,'UTF-8')+'&grant_type=refresh_token';
    System.debug(payload);
    
    request.setBody(payload);
    HttpResponse response = http.send(request);
    // Parse the JSON response
    if (response.getStatusCode() != 200) {
      System.debug('The status code returned was not expected: ' + response.getStatusCode() + ' ' + response.getStatus());
    } else {
      System.debug(response.getBody());
    }

 
Hi Dev Experts!

I've created a Java Script button to set a Case (if it is a Sub Tab) to be opened as a "Primary Tab". It works perfectly except for the Icon on the Tab. When I run by code, the Briefcase Icon is as follows:
User-added image
Whereas the Case icon looks like this:
User-added image
In the code below, I'm using "/img/icon/cases16.png" to get the Case Icon. When I check the URL of the "Yellow Briefcase", I get /s.gif but when I open this directly, no images appear. Does anyone know the URL I should be using to get the "Yellow Briefcase"?
{!REQUIRESCRIPT("/soap/ajax/33.0/connection.js")}
{!REQUIRESCRIPT("/soap/ajax/33.0/apex.js")}
{!REQUIRESCRIPT("/support/console/33.0/integration.js")}

var openSuccess = function openSuccess(result) {
  // Callback function of openPrimaryTab
  if (result.success == true) {
    //alert('Primary tab successfully opened');
    sforce.console.setTabIcon('/img/icon/cases16.png', result.id);
  } else {
    //alert('Primary tab cannot be opened');
  }
};

sforce.console.openPrimaryTab(null, "/{!Case.Id}", true, "{!Case.CaseNumber}", openSuccess);

 
Hi Developers

According to Visualforce Guide, there is a "title" attribute on the apex:selectOption to show a tooltip on mouseover:
https://developer.salesforce.com/docs/atlas.en-us.pages.meta/pages/pages_compref_selectOption.htm

I've also seen this on StackExchange:
https://salesforce.stackexchange.com/questions/33825/help-text-when-mouseover-on-picklist-values/82415

However, I've been unable to get the tooltip to appear, am I missing something? Code is as follows:
<apex:form> 
  <apex:selectList size="3" id="typeSelect" value="{!selValue}" >
    <apex:selectOption title="aaaaa" itemValue="aaaaa" itemLabel="aaaaa"/>
    <apex:selectOption title="bbbbb" itemValue="bbbbb" itemLabel="bbbbb"/>
    <apex:selectOption title="ccccc" itemValue="ccccc" itemLabel="ccccc"/>
    <apex:selectOption title="ddddd" itemValue="ddddd" itemLabel="ddddd"/>
  </apex:selectList>
</apex:form>

 
Hi Developers,

I've followed this Article to display a blob as a PDF in Visualforce and it works fine in Chrome and Firefox:
https://help.salesforce.com/articleView?id=000044612&type=1

However, this doesn't work for IE10 and IE11. I've tried several techniques such as lowering the Security Settings so that ActiveX controls are executed, etc, but none so far has worked. Has anyone found a solution to this for IE?
Reposted from: https://success.salesforce.com/answers?id=9063A000000pFYdQAM

I've configured the following External Data Source using Salesforce Connect. The preference is to use the "Named Principal" Identity Type so only one set of credentials have to be maintained. However, there is a requirement for the backend System to know which is the User performing the OData Request.

User-added image

The functionality desired is shown in the following REST call below (see diagram) - is it possible for the configuration of External Data Sources to define an additional Custom HTTP Header, in this case Username?

The preference will be for this to be done out-of-the-box without having to implement Custom Adapters.

User-added image

I did some further research about "Named Credentials": https://releasenotes.docs.salesforce.com/en-us/winter16/release-notes/rn_security_named_credential_callout_options.htm

However, according to the document, the functionality "Merge Fields in HTTP Header" is not available - "These options aren’t available if you reference the named credential from an external data source."
Hi all

I'm trying to call the REST API from within Apex Code within my own Org. This is to retreive the List View soql, which appears to only be achievable using this method.

I'm using this library someone wrote: https://github.com/shrutis22/Record-Lookup/blob/master/src/classes/ListViewAPI.cls

This is the code in question from that library:
public static ListViewsResponse getListViews( String sObjectName ) {
        Http http = new Http();
        HttpRequest httpReq = new  HttpRequest();
        
        String orgDomain = Url.getSalesforceBaseUrl().toExternalForm();
        
        String endpoint = orgDomain + '/services/data/v37.0/sobjects/' + sObjectName + '/listviews';
        
        httpReq.setEndpoint( endpoint );
        httpReq.setMethod( 'GET' );
        httpReq.setHeader( 'Content-Type', 'application/json; charset=UTF-8' );
        httpReq.setHeader( 'Accept', 'application/json' );
        
        String sessionId = 'Bearer ' + UserInfo.getSessionId();
        
        httpReq.setHeader( 'Authorization', sessionId );
        
        HttpResponse httpRes = http.send( httpReq );
        
        ListViewsResponse listViewResp = ( ListViewsResponse ) JSON.deserialize( httpRes.getBody(), ListViewsResponse.class );
        
        return listViewResp;
    }
This is how I call it:
@AuraEnabled
  public static List<LocationRecord> getLocationRecords(String sObjectType, String listViewDevName) {
    ListView lv = [Select Id From ListView Where sObjectType = :sObjectType And DeveloperName = :listViewDevName Limit 1];

    String soql = ListViewAPI.getListViewQuery(listViewDevName, sObjectType);
    System.debug('XXX = '+soql);
  }

However, in the http response, I'm getting the following:
[{"message":"This session is not valid for use with the REST API","errorCode":"INVALID_SESSION_ID"}]
I've added things like Remote Sites to my own Org.

Many thanks for your help.

 
Hi Expert Devs!

I've got the following call successfully done in POSTMAN using the x-www-form-urlencoded:

User-added image

I'm trying to replicate this in Salesforce but I'm getting a 400 error code (invalid request). Do you know how I can successfully replicate the POSTMAN call in Salesforce?
    MYOB_Integration__c myobIntg = MYOB_Integration__c.getOrgDefaults();
    clientId = myobIntg.Client_ID__c;
    clientSecret = myobIntg.Client_Secret__c;
    refreshToken = myobIntg.Refresh_Token__c;
    authUrl = myobIntg.Authorise_URL__c;
    cfUrl = myobIntg.Company_File_URL__c;
    DateTime lastSync = myobIntg.Last_Account_Sync__c!=null?myobIntg.Last_Account_Sync__c:System.now();
    Map<String, String> authJson = new Map<String, String>();
    // Get the Access Token using the Refresh Token.
    Http http = new Http();
    HttpRequest request = new HttpRequest();
    request.setEndpoint(authUrl);
    request.setMethod('POST');    
    request.setHeader('Content-Type', 'application/x-www-form-urlencoded');
    String payload = 'client_id='+EncodingUtil.urlEncode(clientId,'UTF-8')+'&client_secret='+EncodingUtil.urlEncode(clientSecret,'UTF-8')+'&refresh_token='+EncodingUtil.urlEncode(refreshToken,'UTF-8')+'&grant_type=refresh_token';
    System.debug(payload);
    
    request.setBody(payload);
    HttpResponse response = http.send(request);
    // Parse the JSON response
    if (response.getStatusCode() != 200) {
      System.debug('The status code returned was not expected: ' + response.getStatusCode() + ' ' + response.getStatus());
    } else {
      System.debug(response.getBody());
    }

 
Hi Dev Experts!

I've created a Java Script button to set a Case (if it is a Sub Tab) to be opened as a "Primary Tab". It works perfectly except for the Icon on the Tab. When I run by code, the Briefcase Icon is as follows:
User-added image
Whereas the Case icon looks like this:
User-added image
In the code below, I'm using "/img/icon/cases16.png" to get the Case Icon. When I check the URL of the "Yellow Briefcase", I get /s.gif but when I open this directly, no images appear. Does anyone know the URL I should be using to get the "Yellow Briefcase"?
{!REQUIRESCRIPT("/soap/ajax/33.0/connection.js")}
{!REQUIRESCRIPT("/soap/ajax/33.0/apex.js")}
{!REQUIRESCRIPT("/support/console/33.0/integration.js")}

var openSuccess = function openSuccess(result) {
  // Callback function of openPrimaryTab
  if (result.success == true) {
    //alert('Primary tab successfully opened');
    sforce.console.setTabIcon('/img/icon/cases16.png', result.id);
  } else {
    //alert('Primary tab cannot be opened');
  }
};

sforce.console.openPrimaryTab(null, "/{!Case.Id}", true, "{!Case.CaseNumber}", openSuccess);

 
Hi Developers

According to Visualforce Guide, there is a "title" attribute on the apex:selectOption to show a tooltip on mouseover:
https://developer.salesforce.com/docs/atlas.en-us.pages.meta/pages/pages_compref_selectOption.htm

I've also seen this on StackExchange:
https://salesforce.stackexchange.com/questions/33825/help-text-when-mouseover-on-picklist-values/82415

However, I've been unable to get the tooltip to appear, am I missing something? Code is as follows:
<apex:form> 
  <apex:selectList size="3" id="typeSelect" value="{!selValue}" >
    <apex:selectOption title="aaaaa" itemValue="aaaaa" itemLabel="aaaaa"/>
    <apex:selectOption title="bbbbb" itemValue="bbbbb" itemLabel="bbbbb"/>
    <apex:selectOption title="ccccc" itemValue="ccccc" itemLabel="ccccc"/>
    <apex:selectOption title="ddddd" itemValue="ddddd" itemLabel="ddddd"/>
  </apex:selectList>
</apex:form>