3

I have a rest resource (developed by a vendor) that I am calling in my batch apex class that posts some data. The code is as below and works in partial and dev sandboxes but not in full sandbox as the UserInfo.getSessionID() return null there by failing my batch apex class. I am wondering is there any other way of getting around it or would I need to add a connected app with in the same org and access it via consumer key and secret (seems a bit daft to be honest)?

    String sfdcURL = URL.getSalesforceBaseUrl().toExternalForm();
    String restAPIURL = sfdcURL + '/services/apexrest/***/**/**/**';      
    HttpRequest httpRequest = new HttpRequest();
    httpRequest.setEndpoint(restAPIURL); 
    httpRequest.setMethod('POST'); 
    httprequest.setHeader('Content-Type', 'application/json');
    httprequest.setHeader('Accept','application/json');
    httpRequest.setHeader('Authorization', 'OAuth '+UserInfo.getSessionID());        
    httpRequest.setHeader('Authorization', 'Bearer '+UserInfo.getSessionID());  
    httpRequest.setBody(json.serializePretty(PayLoad));
    System.debug('UserInfo.getSessionId() '+UserInfo.getSessionId());
    String response = '';
    try {  
        Http http = new Http();   
        HttpResponse httpResponse = http.send(httpRequest);  
        System.debug('>> Response of payload >> '+httpResponse.getStatusCode());
        response = 'Status Code: '+httpResponse.getStatusCode()+' Response: '+httpResponse.getBody();
    } catch(Exception e) {  
        System.debug('ERROR: '+ e.getMessage());  
        response = e.getMessage();
        sendEmail('Payload Sending to Kimble failed','Exception Occurred Payload Push',e.getMessage(),true,restAPIURL);
    }  
    System.debug(' ** response ** : ' + response );
4
  • Is there a significant difference between the Users running this batch class in your dev, partial and full copy sandbox?
    – Jayant Das
    Sep 28, 2018 at 15:34
  • No it's the same user that's running the batch class (i.e. me)
    – SFDC_Dev
    Sep 28, 2018 at 15:35
  • Not quite sure why would that be if the user is same. But if I see your code, looks like you are calling a REST API within your org? Is that the correct understanding? If that's the case, why do you even need to invoke it as an API call?
    – Jayant Das
    Sep 28, 2018 at 15:40
  • You're right I am calling a rest resource with in the org because I am trying to insert data that was developed by a 3rd party vendor and they proscribe us from performing DML on their objects and only allow us to do via rest resources hence my comment about it being daft to use consumer key and secret within the same org!!
    – SFDC_Dev
    Sep 28, 2018 at 15:44

1 Answer 1

6

UserInfo.getSessionId() now works in asynchronous code as of Winter '19. The sandbox that it's currently failing in is most likely in Summer '18. Once your sandbox is upgraded, it should work as expected.

API calls sometimes require a session ID and a URL. You can obtain that session ID using the System.UserInfo.getSessionId() method. This method previously returned null in asynchronous Apex, but it now returns a value whether it’s run synchronously or asynchronously.

5
  • Thanks @sfdcfox. You're absolutely correct that full sandbox (the failing) is in Summer '18. This will get upgraded on 13th Oct which is 2 weeks from now and my folks want to deploy end of next week. Sadly, I'm back to my question again is there a way to do it without the connected app thing?
    – SFDC_Dev
    Sep 28, 2018 at 16:21
  • @SFDC_Dev You could temporarily implement a SOAP login, I suppose, but that's almost as bad as the Named Credential route. It would be advisable to simply wait until the new release. Or you could probably use a Custom Setting and store a Session ID during a trigger transaction, but those sessions can expire with no way to restore them...
    – sfdcfox
    Sep 28, 2018 at 16:24
  • @SFDC_Dev why not use named Creds? salesforce.stackexchange.com/a/183692/19118 Sep 28, 2018 at 18:53
  • @sfdcfox As I understand 'SandboxPostCopy' is also asynchronous. Will the 'System.UserInfo.getSessionId()' method work with SandboxPostCopy implementation.
    – Vickal
    Feb 7, 2021 at 4:14
  • @Vickal Yes, it should work, but be aware that this interface uses Automated User, so you may also need to assign additional permissions depending on what you're trying to do.
    – sfdcfox
    Feb 7, 2021 at 4:48

You must log in to answer this question.

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