Salesforce Certified Platform Developer II — Question 58

A developer writes the following code:
public with sharing class OrderController() public PaqeReference sendOrder() Order_c order = new Order_c insert order;
ExternalOrder externalOrder = new ExternalOrder(order); Http h = new Http(); HttpRequest req = new HttpRequest(); req.setEndpoint('https://www.example.org/v1/orders'); req.setMethod('POST'); req.setBody(JSON.serialize(externalOrder));
HttpResponse res = h.send(req); order = (ExternalOrder)JSON.deserialize(res.getBody(),ExternalOrder.class);
While testing the code, the developer receives the following error message:
System.CalloutException : You have uncommitted work pending
What should the developer do? (Choose two.)

Answer options

Correct answer: B, C

Explanation

The correct actions are B and C because Salesforce does not allow DML operations and callouts to be mixed in the same transaction. Ensuring that all callouts are completed before executing DML statements (B) prevents the error. Moving the web service callout into an @future method (C) allows it to run asynchronously, thus avoiding the issue. Options A and D do not resolve the problem as they do not adhere to the constraints of Salesforce's transaction model.