0

I have created two rest API methods in the schedulable class when I called in the execute function my first method is called and worked fine in my First response my second method is not triggered. how would I invoke the second function to be called? Thanks in Advance.

global class BCIScheduler implements Schedulable, Database.AllowsCallouts
{
    global void execute(SchedulableContext sc) 
    {
        
        AMethod();
    }
    
    
    
    public  void AMethod()
    {
        List<object> AList = new List<object>();
        try
        {
            Http http = new Http();
            HttpRequest request = new HttpRequest();
            System.debug('Roles Entered: '+ roles);
            request.setEndpoint('http://api.json.com/rest/a.json?Fields='+Fields);
            request.setMethod('GET');
            request.setTimeout(120000);
            request.setHeader('Content-Type','application/json'); 
            HttpResponse response = http.send(request);
            
            if (response.getStatusCode() == 200)
            {
                
                List<object> Ajson = (List<object>) JSON.deserialize(response.getBody(), List<AClass>.class);
                AList.addAll(Ajson);
                
            }
            
        }
        catch(Exception exp){                     
            system.debug(exp.getMessage()+ exp.getLineNumber());
            
        }
        
    }
}


if(AList.size()>0)
{
    
    batch_upsert upst = new batch_upsert(AList);
    database.executeBatch(upst,100);
    BMethod();
}





}

public  void BMethod()
{
    
    try
    {
        Http http = new Http();
        HttpRequest request = new HttpRequest();
        System.debug('Roles Entered: '+ roles);
        request.setEndpoint('http://api.json.com/rest/b.json?Fields='+Fields);
        request.setMethod('GET');
        request.setTimeout(120000);
        request.setHeader('Content-Type','application/json'); 
        HttpResponse response = http.send(request);
        
        if (response.getStatusCode() == 200)
        {
            
            List<object> Bjson = (List<object>) JSON.deserialize(response.getBody(), List<Bclass>.class);
            BList.addAll(Bjson);
            
        }
        
    }
    catch(Exception exp){                     
        system.debug(exp.getMessage()+ exp.getLineNumber());
        
    }
    
}

}

}
0

It is being called. It's throwing an exception that you cannot see because you have suppressed all exceptions. The exception being thrown is

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

because you performed a DML-equivalent operation (enqueueing a batch) prior to performing a callout in method B.

You should never use this structure:

    catch(Exception exp){                     
        system.debug(exp.getMessage()+ exp.getLineNumber());
        
    }

Exceptions happen for a reason. Don't swallow and hide them. Silent failures produce terrible code that is very difficult to debug.

You can solve this problem by restructuring your code to enqueue batch classes only after you have made all of the callouts you need.

2
  • Hi @David Reed can you provide some example ? Feb 4 at 16:17
  • If you search SFSE for "You have uncommitted work pending" you will find dozens of examples of people working with this issue.
    – David Reed
    Feb 4 at 16:35

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.