-1

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.setMethod('POST');
        req.setEndpoint(url);
        req.setHeader('Content-type', 'application/json');
        req.setHeader('Authorization', 'Bearer ' + UserInfo.getSessionId());
        req.setBody(pageCreationReq);
        Http http = new Http();
        return http.send(req);
    }    

1 Answer 1

4

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.

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.