I'm trying to loop account ids through a SAQL query and store the results from the query into a list. However in the loop, the query executes for only the first id in the list fetched, for the second one in the loop i get an exception saying "You have uncommitted work pending. Please commit or rollback before calling out".

The following is the code I used:

///////build classes to consume JSON result
    public class SuperClass
        public String action;
        public String responseId;
        public Results results;
    public class Results 
        public List<Records> records;
    public class Records
        public String A;

    List<String> invtotal = new List<String>();
///////Get list of Ids
    List<Rebate__c> rebateaccids = [SELECT Id,Account__c FROM Rebate__c WHERE Status__c = 'Signed' AND Rebate_Year__c = '2018'];
///////Loop ids through SAQL query and store results in list invtotal 

        for (Rebate__c ra: rebateaccids)
            String query2 = 'q = load "0Fb0K000000TcysSAC/0FcO0000000kjmMKAQ"; q = filter q by \'Account__c\' == "' + ra.Account__c + '"; result = group q by all; result = foreach result generate sum(\'Total.TotalPrice\') as \'A\';';
            ConnectApi.LiteralJson queryresult =  ConnectApi.Wave.executeQuery(query2);
            String response = queryresult.json;

            SuperClass sc = (SuperClass)JSON.deserialize(response, SuperClass.class);


    catch (Exception e){}

  • Is this your entire code? are you performing any dml in the process? – Hemant Jain Jun 8 '18 at 9:55
  • Yes this is the entire code i'm executing in the Execute Anonymous Window. In my logs I can see that the code has performed 1 out of 150 DMLs but I don't know which line is doing that as all I'm doing is querying Analytics. – Sharvil Popli Jun 10 '18 at 17:33

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Browse other questions tagged or ask your own question.