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
Jacky LeeJacky Lee 

Named Credentials in Batch HTTP Requests work in Sandbox but not Production

I have a batch class with Database.AllowsCallouts that upon execute will invoke a helper method that makes an HTTP Request using Named Credentials. The issue I am facing is that the response returns 200 OK in my sandbox, but 409 Conflict in production (Dropbox API). My remote site settings and named credential configs are exactly the same between sandbox and production.

The funny thing is if I call the helper function in apex anonymous, it works fine. I've tried isolating the problem and I'm sure it's something strange going on with running in Batch context.

Here's my stack trace in sandbox:

And in production:
 

Jacky LeeJacky Lee

Formatting problems... Here they are:

Production log:

|NAMED_CREDENTIAL_REQUEST|NamedCallout[Name=DBH_Modify_Shared_Link, Endpoint=https://api.dropboxapi.com/2/sharing/modify_shared_link_settings, Method=POST, External Credential Type=EXTERNAL, HTTP Header Authorization=Method: Basic - Authorization Credential Hash: -1307829749, Retry on 401=True]
|NAMED_CREDENTIAL_RESPONSE|NamedCallout[Name=DBH_Modify_Shared_Link, Status Code=409]

Sandbox log:

NAMED_CREDENTIAL_REQUEST|NamedCallout[Name=DBH_Modify_Shared_Link, Endpoint=https://api.dropboxapi.com/2/sharing/modify_shared_link_settings, Method=POST, External Credential Type=EXTERNAL, HTTP Header Authorization=Method: Basic - Authorization Credential Hash: -1307829749, Retry on 401=True]
NAMED_CREDENTIAL_RESPONSE|NamedCallout[Name=DBH_Modify_Shared_Link, Status Code=200]
My HTTP Request snippet using named creds:
Http http = new Http();
            HttpRequest req = new HttpRequest();
            HttpResponse res = new HttpResponse();
            
            try {
                req.setEndpoint(DROPBOX_MODIFY_SHARED_LINK_ENDPOINT);
                req.setMethod('POST');
                req.setHeader('Authorization', 'Bearer ' + '{!$Credential.Password}');
                req.setHeader('Content-Type', 'application/json');
                
                req.setBody('{"url": "' + path + '", "settings": {"requested_visibility": "password", "audience": "password", ' +
                            '"access": "viewer", "link_password": "' + password + '"}}');
                req.setTimeout(120000);
                System.debug(req.getBody());
                res = http.send(req);
            } catch(Exception e) {
                System.debug(e);
            }
Is there something really simple I'm missing and completely overlooking?
 
SysAdmin 86SysAdmin 86
Did you ever get this figured out? I am having the exact same issue also with the Dropbox API.