0
public with sharing class CalloutWebservice {


    public Account headAccount;

    private Boolean ApiEnabled;

    private List<Account> accountList;


    public CalloutWebservice(ApexPages.StandardController stdController) {
        this.headAccount = (Account) stdController.getRecord();
    }

    public PageReference sendAccountStructure() {

        this.checkPreconditionsForSync(this.headAccount);
        this.ApiEnabled = false;
        List<APIControl__mdt> cfgList = [SELECT IsEnabled__c FROM APIControl__mdt];
        if (cfgList.size() == 1) {
            this.ApiEnabled = cfgList.get(0).IsEnabled__c;
        }

        System.debug(' Api enabled: ' + this.ApiEnabled);


        if (!ApexPages.hasMessages(ApexPages.Severity.ERROR)) {
            if (this.ApiEnabled) {
                // Need to delete all Integration__c records referencing the head Account
                List<Integration__c> referencedAccountIntegrationStatusrecords = [SELECT Id FROM Integration__c 
                WHERE Account__c = :this.headAccount.Id];
                if(referencedAccountIntegrationStatusrecords != null && referencedAccountIntegrationStatusrecords.size() > 0) {
                    delete referencedAccountIntegrationStatusrecords;
                }
                System.debug('Integration__c records referenced to headAccount are :' + referencedAccountIntegrationStatusrecords);
                this.doCallout(this.headAccount.Id); 

            } else {
                ApexPages.addMessage(new ApexPages.Message(ApexPages.Severity.INFO, 'API ist nicht aktiviert.'));
                this.addManualCreationNotification();
            }
        }

        return null;
    }

So, I am trying to delete the Integration records associated with the account before doing the callout but I am getting this error. I have however googled it and I see that I have split up into two tasks , one for DML and other for callout. But I am not sure how do I do that.. Any pointers are appreaciated ... Thank you

0

There's no need to split it into two separate tasks. This issue occurs only when you make a DML operation and after that a callout. The reason behind is that after performing the DML you are locking the database until the commit is done (last step in order of execution). If you switch the order in which you perform those operations everything will be fine.

You haven't posted all of your code so it's hard to say if it's possible in this case but if not then you should probably use @Future annotated method to make a callout.

  • Thanks for the update but the problem is if I do the deletion after the callout, it won't be functionally correct as per the functionality I need to delete the records before the callout. Can you please eloborate on the future annotated method to make the callout ? – user3920709 Jun 14 '19 at 10:44
  • You can annotate doCallout method as @Future and static. See developer.salesforce.com/docs/atlas.en-us.apexcode.meta/… . It performs method asynchronously - in separate transaction. – Bartosz Śliwiński Jun 14 '19 at 10:50
  • I believe that your method całej doCallout is a method thats doing a callout annotate it with @future(callout=true) it Will run in separate transaction. Annotating means putting this Line above method definition – user1974566 Jun 14 '19 at 10:52
  • You are right, the method doCallout is doing the callout. Ok, will annotate with @future and try it and will update it. Thank you – user3920709 Jun 14 '19 at 11:23
  • I am getting at this.callout(this.headAccount.Id) stating 'Static methods cannot be referenced from a non static context: void CalloutWebservice.doCallout(Id) – user3920709 Jun 14 '19 at 11:45

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.