6

I have Apex code that performs a callout to an external system to insert a record. After the callout is performed a DML operation stores the resulting external ID back in an sObject.

A super simplified version would look something like this:

HttpRequest req = new HttpRequest();
req.setEndpoint('http://www.fishofprey.com/');
req.setMethod('GET');
Http http = new Http();
HTTPResponse res = http.send(req);
res.getBody();
Account a1 = new Account(name='a');
insert a1;

The real call would do a POST and then use the result as part of creating the new record, but you get the idea.

This works fine in normal operation. However, when the org gets put into read-only mode the callout still occurs but the subsequent DML fails with the error

System.InvalidReadOnlyUserDmlException: Insert failed. First exception on row 0; first error: INSERT_UPDATE_DELETE_NOT_ALLOWED_DURING_MAINTENANCE, Updates can't be made during maintenance. Try again when maintenance is complete.

That's a bit of a problem, because the external system has the new record but Salesforce doesn't know about it.

How can I prevent this problem?

6

The answer came via @ca_peterson and the Winter '18 release notes.

Have apex that makes callouts that make changes to external systems? Go read https://releasenotes.docs.salesforce.com/en-us/winter18/release-notes/rn_apex_callouts_readonly.htm

From the linked release notes:

During a site switch, your Salesforce org is in read-only mode and no write operations are allowed in Salesforce. However, Apex callouts to external services aren't blocked in read-only mode. To avoid incorrect program behavior, we recommend that you prevent making callouts in read-only mode. To check whether the org is in read-only mode, call System.getApplicationReadWriteMode().

This is important, as your org will go into "read-only" mode

during some Salesforce maintenance activities, such as planned site switches and instance refreshes. As part of Continuous Site Switching, your Salesforce org is switched to its ready site approximately once every six months.

So while it isn't common, it can and does occur periodically.

You can still safely make callouts while in readonly mode, but you need to consider what happens afterwards. If you are just reading information to dump onto a page for the user then there is no harm in making the callout. But if you subsequently need to update Salesforce with the callout results then you will need to block it before the callout occurs.


Salesforce support can activate a "Read Only Application Test Mode" for you org. See Read Only Application Test Mode Read Only Application Test Mode

See also:

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.