0

I am trying to update a Salesforce record but I have to update it using an older version of Salesforce API (v44) due to some new standard behavior changes salesforce made. I am doing this by making a REST callout to my salesforce org from Apex and I am successfully able to test it using workbench Rest Explorer. When I make this call using Apex or Execute anonymous, I get this error:

"[{"message":"Access from current IP address is not allowed","errorCode":"INSUFFICIENT_ACCESS"}]"

The environment I am working in is very locked down and does have an IP Range restriction, but I am already in the network while making this callout and as I said earlier I was able to make the callout using Workbench.

Where can I look to fix this error?

The code for the callout:

String SF_URL = URL.getSalesforceBaseUrl().toExternalForm();
    String restAPIURL = SF_URL + '/services/data/v44.0/sobjects/Account/' + accId + '?_HttpMethod=PATCH';

    Http http = new Http();
    HttpRequest request = new HttpRequest();
    request.setEndpoint(restAPIURL);
    request.setHeader('X-HTTP-Method-Override','PATCH');
    request.setMethod('POST');

    request.setHeader('Content-Type', 'application/json;charset=UTF-8');
    request.setHeader('Authorization', 'OAuth ' + UserInfo.getSessionId());
    // Set the body as a JSON object
    request.setBody('{"OwnerId":"' + AccOwner + '"}');

    HttpResponse response = http.send(request);
0

Workbench does not run on the Salesforce core platform; it runs on Heroku. For that reason, Workbench, the Salesforce org itself, and any connections you make locally using an application like Postman come from different IP addresses, all of which must be allowed for the user under which you are authenticating.

It sounds like your org has whitelisted Workbench, but not Salesforce's own IP ranges. You cannot fix this in code; you must update your org's configuration.

0

Please check the Login IP ranges in the profile of the User you're using to make this callout. When you execute it from Workbench, it runs in your User's context.

  • when I am running it from apex, I am in the same location as when I run it from Workbench. Doesn't that mean the IP range should be fine b.c I am already in the network. – Muhammad Bajwa May 14 at 14:47
  • Yes, But in what User's context the APEX code runs from ? For eg, if the Apex is a Visualforce Controller, and the VF Page runs for Portal users, then the Portal User's profile should have your IP Whitelisted. – Ajinkya Phadnis May 15 at 6:33

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.