Got the System.CalloutException: You have uncommitted work pending. Please commit or rollback before calling out But already completed the DML operations before calling http callout.

   global Response createVFPage(
        .... params) {
        // DML operations

        // create VF page
        String markupContent = configureDefaultSections(navigationJsonName, pageTitle);
        Map<String, String> pageCreationReq = new Map<String, String>{
        String httpReqContent = JSON.serialize(pageCreationReq);
        res = sendVFPageCreationRequest(httpReqContent);

        if ((res.getStatusCode() == 200) || (res.getStatusCode() == 201)) {
            return true;
        } else {
            return JSON.serialize(res.getBody());

    private HTTPResponse sendVFPageCreationRequest(String pageCreationReq) {
        String url = salesforceHost + '/services/data/v29.0/sobjects/ApexPage';
        HttpRequest req = new HttpRequest();
        req.setHeader('Content-type', 'application/json');
        req.setHeader('Authorization', 'Bearer ' + UserInfo.getSessionId());
        Http http = new Http();
        return http.send(req);

1 Answer 1


In salesforce you cannot do dml and then callout. Once the connection is made with database, that has to be closed immediately. So platform prevents any blocking operation to continue. Callout time is not predictable.

In order to solve this, you need to make callout first and then make any dmls which you want.

If the dml is very much necessary then you need to change the context E.g: Do the dml and get the result. Go back to the page and then initiate another context and make callout there.

You must log in to answer this question.

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