0

I really need help with this error. I am trying to call a global method of a managed package in my apex class. I am calling this method in the Process builder on creation of some X record. Whenever my Workflow executes (on X creation), I end up receiving the error: "An Apex error occurred: zqu.RestRequest.RestRequestException: The callout was unsuccessful after 4 attempts: You have uncommitted work pending. Please commit or rollback before calling out". The same code works perfectly when I execute in Anonymous window.

global class executeSendToZbilling {
    @InvocableMethod global static void method() {
        List<zqu.zQuoteUtil.ZBillingQuoteCollection> quotes = new        List<zqu.zQuoteUtil.ZBillingQuoteCollection>();
        zqu__Quote__c cancQuote= [SELECT ID, zqu__Account__c, zqu__ZuoraAccountID__c, zqu__Status__c FROM zqu__Quote__c WHERE zqu__Status__c= 'New' AND zqu__SubscriptionType__c = 'Cancel Subscription' AND CreatedDate = TODAY];
        zqu.zQuoteUtil.ZBillingQuoteCollection quote = new zqu.zQuoteUtil.ZBillingQuoteCollection();
        quote.sfdcAccountId = cancquote.zqu__Account__c; // SFDC CRM Account ID
        quote.zAccountId = cancQuote.zqu__ZuoraAccountID__c; // Zuora Billing Account ID
        quote.quoteRequests = new List<zqu.zQuoteUtil.ZBillingQuoteRequest>();
        zqu.zQuoteUtil.ZBillingQuoteRequest req = new    zqu.zQuoteUtil.ZBillingQuoteRequest();
        req.sfdcQuoteId = cancquote.ID; // SFDC Quote ID
        quote.quoteRequests.add(req);

        quotes.add(quote);

        List<zqu.zQuoteUtil.zBillingResult> results =   zqu.zQuoteUtil.sendToZBilling(quotes); /*The above mentioned error is here*/
        for ( zqu.zQuoteUtil.zBillingResult result : results ) {
            System.debug( 'Result: QuoteId = ' + result.sfdcQuoteId + ', Success = ' + result.success + ', message = ' + result.message );
        }
    }
}

I am not able to figure out whats the error. Any suggestions please. Thanks in advance!!

1 Answer 1

2

You can't perform a callout at any point in the Triggers and Order of Execution life cycle. This includes trying to perform callouts from triggers and process builder. You must perform the callout either before or after a transaction.

The easiest way to do this would be to simply make a future method:

global class executeSendToZbilling {
    @InvocableMethod global static void method() {
        doFuture();
    }
    @future(callout=true) global static void doFuture() {
        List<zqu.zQuoteUtil.ZBillingQuoteCollection> quotes = new        List<zqu.zQuoteUtil.ZBillingQuoteCollection>();
        zqu__Quote__c cancQuote= [SELECT ID, zqu__Account__c, zqu__ZuoraAccountID__c, zqu__Status__c FROM zqu__Quote__c WHERE zqu__Status__c= 'New' AND zqu__SubscriptionType__c = 'Cancel Subscription' AND CreatedDate = TODAY];
        zqu.zQuoteUtil.ZBillingQuoteCollection quote = new zqu.zQuoteUtil.ZBillingQuoteCollection();
        quote.sfdcAccountId = cancquote.zqu__Account__c; // SFDC CRM Account ID
        quote.zAccountId = cancQuote.zqu__ZuoraAccountID__c; // Zuora Billing Account ID
        quote.quoteRequests = new List<zqu.zQuoteUtil.ZBillingQuoteRequest>();
        zqu.zQuoteUtil.ZBillingQuoteRequest req = new    zqu.zQuoteUtil.ZBillingQuoteRequest();
        req.sfdcQuoteId = cancquote.ID; // SFDC Quote ID
        quote.quoteRequests.add(req);

        quotes.add(quote);

        List<zqu.zQuoteUtil.zBillingResult> results =   zqu.zQuoteUtil.sendToZBilling(quotes); /*The above mentioned error is here*/
        for ( zqu.zQuoteUtil.zBillingResult result : results ) {
            System.debug( 'Result: QuoteId = ' + result.sfdcQuoteId + ', Success = ' + result.success + ', message = ' + result.message );
        }
    }
}
0

You must log in to answer this question.

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