1

I spent the entire day re-writing all my tests because of issues I was running into in this question from yesterday. For the most part I am done, but am running into a:

System.CalloutException: You have uncommitted work pending. Please commit or rollback before calling out

...exception which I can't resolve. The exception occurs in this chunk of code:

@RemoteAction
public static void ImageSave(
    ImageSaveFormModel form) {
    vNOpportunityPhoto__c photo = [
        SELECT  ResizedFile__r.Extension__c,
                ResizedFile__r.FileSize__c,
                ResizedFile__r.S3Key__c,
                ThumbnailFile__r.Extension__c,
                ThumbnailFile__r.FileSize__c,
                ThumbnailFile__r.S3Key__c
        FROM    vNOpportunityPhoto__c
        WHERE   (Id = :form.OpportunityPhotoId)
    ];

    //  The exception occurs here
    vNKrakenService.KrakenResponse resizedOptimizeWaitResponse = vNKrakenClient.OptimizeWait(form.ResizedBlob, photo.ResizedFile__r.S3Key__c);

    if (!resizedOptimizeWaitResponse.Success) {
        System.debug('Resized optimization failed, error: ' + resizedOptimizeWaitResponse.Error);

        return;
    }

    blob resizedBlob = vNKrakenClient.DownloadFile(resizedOptimizeWaitResponse);

    vNKrakenService.KrakenResponse thumbnailOptimizeWaitResponse = vNKrakenClient.OptimizeWait(resizedBlob, photo.ThumbnailFile__r.S3Key__c, 140, 140);

    if (!thumbnailOptimizeWaitResponse.Success) {
        System.debug('Thumbnail optimization failed, error: ' + thumbnailOptimizeWaitResponse.Error);

        return;
    }

    blob thumbnailBlob = vNKrakenClient.DownloadFile(thumbnailOptimizeWaitResponse);
    boolean resizesPutResult = vNAwsS3Client.PutObject(photo.ResizedFile__r.S3Key__c, resizedBlob);

    if (!resizesPutResult) {
        System.debug('Resize put failed, bailing out.');

        return;
    }

    boolean thumbnailPutResult = vNAwsS3Client.PutObject(photo.ThumbnailFile__r.S3Key__c, thumbnailBlob);

    if (!thumbnailPutResult) {
        System.debug('Thumbnail put failed, bailing out.');

        return;
    }

    if (!Test.isRunningTest()) {
        photo.ResizedFile__r.FileSize__c = resizedBlob.size();
        photo.ThumbnailFile__r.FileSize__c = thumbnailBlob.size();

        update photo.ResizedFile__r;
        update photo.ThumbnailFile__r;
    }
}

...however, there are no DML operations occurring anywhere in the call stack, UNLESS the test engine is deciding that the SOQL queries are a DML operation? The test is like this, and the test class mocks up fake objects using an @TestSetup method:

@IsTest
public static void ImageSave() {
    System.debug('OpportunityPhotosControllerTests.ImageSave');

    vNOpportunityPhoto__c opportunityPhoto = [
        SELECT  Id
        FROM    vNOpportunityPhoto__c
        LIMIT   1
    ];

    vNOpportunityPhotosController.ImageSaveFormModel imageSaveForm = new vNOpportunityPhotosController.ImageSaveFormModel();

    imageSaveForm.OpportunityPhotoId = opportunityPhoto.Id;
    imageSaveForm.Resized64 = '/9j';

    Test.setMock(HttpCalloutMock.class, new vNTestsHttpMocks());
    Test.startTest();

    vNOpportunityPhotosController.ImageSave(imageSaveForm);

    Test.stopTest();
}

What can I do to resolve this? I've been trying to get unit tests to work for three days now only to be blocked by the callout exception which is a lie... There are no triggers in the app, and the workflows that exist will not be triggered, and even if they were they only send out emails.

Update 1

Per @Santanu Boral's suggestion I moved the Test.setMock() to be after Test.startTest(), not before it. It didn't help, and I really don't think the order of the Test class methods matters. Here's a screenshot of the log file after the change which is identical to before the change.

enter image description here

Update 2

So, I went through and disabled all workflow rules on Opportunity. When I did that the exception disappeared, there were no errors in the log, yet the classes refused to save to the server. No exception, no error, and no save, thanks Salesforce for working so well...

Anyway, I then reactivated each workflow rule one by one until the exception reappeared again on the very last rule. This rule just sends out an email and performs no DML operations.

At this point I have no idea what to do or say and I'm just very angry and frustrated. All I know is that the day my company leaves Salesforce will be a glorious day of happiness. Until then I'm stuck pretty much incapable of doing anything.

1
  • I have updated my answer based on email alert Commented Aug 11, 2016 at 4:44

1 Answer 1

5

Can you please make a small changes in your test class.

Test.setMock(HttpCalloutMock.class, new vNTestsHttpMocks()); will be after Test.startTest();

Also. try to create separate test methods for testing those callouts (i.e. one for resizedOptimizeWait, other for thumbnailsOptimizeWait).

Update

Workaround 1

If the workflow email alert is giving the issue both for normal execution as well as from test class then, remove it from the workflow and send email through your code via @future method.

Refer known issue Workflow (for email) and DML in same transaction can cause System.CalloutException

Workaround 2

If this issue is occurring only from test class then create a boolean variable like blSendEmail which is by-default TRUE and from test class make it FALSE.

Incorporate this blSendEmail in your workflow rule criteria to send emails.

So, from test class, stop sending workflow email alert.

It will definitely solve the issue.

3
  • I tried your suggestion, but it didn't work. I've updated my question to show the results.
    – Gup3rSuR4c
    Commented Aug 11, 2016 at 3:01
  • Sorry for the late reply. I went with your second work around. I added a WorkflowBypass checkbox field to the Opportunity, false by default. When the test runs and instances a new Opportunity I set it to true. Now the exceptions no longer trigger. After getting the test classes to finally save, I'm happy to report that everything works. Sad that it took five days to get Salesforce to accept the test classes without issues. Thank you for the help!
    – Gup3rSuR4c
    Commented Aug 12, 2016 at 21:17
  • nice to hear that. Commented Aug 12, 2016 at 21:21

You must log in to answer this question.

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