• Michał Piątek
  • NEWBIE
  • 0 Points
  • Member since 2021

  • Chatter
    Feed
  • 0
    Best Answers
  • 0
    Likes Received
  • 0
    Likes Given
  • 1
    Questions
  • 1
    Replies

Hello Saleforce community!

I have encountered really weird problem and can not move forward.

I am making internal callout to /services/data/v51.0/composite/tree/ContentDistribution to create ContentDistribution objects. This is workaround becasue I need these records before making callout,which causes uncommited work pending error.

This code does not work when used inside Apex and I am obtaining erorr message "This session is not valid for use with the REST API":

String baseUrl = URL.getSalesforceBaseUrl().toExternalForm();
        Http h = new Http();
        Httprequest req = new Httprequest();
        req.setMethod('POST');
        
        //req.setHeader('Authorization', 'OAuth' + UserInfo.getSessionId());       
		req.setHeader('Authorization', 'Bearer ' + UserInfo.getSessionId()); 
       
        
req.setHeader('ContentType','application/json');

req.setBody(JSON.serialize(new Map<String,List<DistClass>>{'records' => linksToInsert}));
req.setEndpoint(baseUrl + '/services/data/v51.0/composite/tree/ContentDistribution');

HttpResponse response = h.send(req);
However exact same code works just fine when:
-called from anonymous window
or
- when I print sessionId
System.debug(UserInfo.getOrganizationId().substring(0, 15) + ' ' +  UserInfo.getSessionId().substring(15));
and hardcode it like:
req.setHeader('Authorization', 'Bearer ' + '00D3A0000000001!ARkAQApQe_Omitted_yGifr.u7.';
I was not able to resolve that issue for 2 days. What is wrong here?

 

Hello Saleforce community!

I have encountered really weird problem and can not move forward.

I am making internal callout to /services/data/v51.0/composite/tree/ContentDistribution to create ContentDistribution objects. This is workaround becasue I need these records before making callout,which causes uncommited work pending error.

This code does not work when used inside Apex and I am obtaining erorr message "This session is not valid for use with the REST API":

String baseUrl = URL.getSalesforceBaseUrl().toExternalForm();
        Http h = new Http();
        Httprequest req = new Httprequest();
        req.setMethod('POST');
        
        //req.setHeader('Authorization', 'OAuth' + UserInfo.getSessionId());       
		req.setHeader('Authorization', 'Bearer ' + UserInfo.getSessionId()); 
       
        
req.setHeader('ContentType','application/json');

req.setBody(JSON.serialize(new Map<String,List<DistClass>>{'records' => linksToInsert}));
req.setEndpoint(baseUrl + '/services/data/v51.0/composite/tree/ContentDistribution');

HttpResponse response = h.send(req);
However exact same code works just fine when:
-called from anonymous window
or
- when I print sessionId
System.debug(UserInfo.getOrganizationId().substring(0, 15) + ' ' +  UserInfo.getSessionId().substring(15));
and hardcode it like:
req.setHeader('Authorization', 'Bearer ' + '00D3A0000000001!ARkAQApQe_Omitted_yGifr.u7.';
I was not able to resolve that issue for 2 days. What is wrong here?