0

Need help. I'm new to Apex (with decent exp on Java) and Im working on callout class to get data from external system. Below code works fine, however while making a callout to larger data set, I get:

Error: System.LimitException: Too many DML rows: 10001

I'm assuming this is due to large data being returned from webservice. Is there a way to process this data in batches? I know Batch class is used for operations like this however I`m not able to get it work. How can I process JSON reponse in batches? Can someone please give me a working example?

            HttpRequest obj = new HttpRequest();
        HttpResponse res = new HttpResponse();
        Http http = new Http();
        String reqBody = '{ "user": "user_name", "pswd": "user_password" }';
        obj.setMethod('POST');
        obj.setHeader('Content-Type','application/json');
        obj.setEndPoint('https://test.samplepoint.com/api/UserSrvs.svc/Login');
        obj.setBody(reqBody);
        obj.getheader('Auth-Token');
        res = http.send(obj);

                authtoken objAuthenticationInfo = (authtoken)JSON.deserialize(res.getbody(), authtoken.class);
                System.debug('objAuthenticationInfo: '+objAuthenticationInfo);


                                String token = res.getHeader('Auth-Token');
                                system.debug('token: '+token);    


        Http h1 = new Http();
        HttpRequest req1 = new HttpRequest();
        String reqBody2 = '{"Accountype" : "workforce"}'; 
        req1.setHeader('Auth-Token', token);
        req1.setHeader('Content-Type','application/json');
        req1.setMethod('POST');
         req1.setBody(reqBody2);

        req1.setEndpoint('https://test.samplepoint.com/api/accservices.svc/accountfeed');
        system.debug('======req1========'+req1);
        HttpResponse res1 = h1.send(req1);
        system.debug('==========res1============'+res1.getBody());



    DataParser deserializeRes =  new DataParser();
    deserializeRes = (DataParser)System.JSON.deserialize(replaceJson, DataParser.class);
     List <DataParser.cls_account> advisorList = new List<DataParser.cls_account>();
    advisorList = deserializeRes.rut.accounts.account;



List<Funtional_Account__c> lstAccount = new List<Funtional_Account__c>();
for(DataParser.cls_account c : advisorList){
    Funtional_Account__c PFA = New Funtional_Account__c();
    PFA.payment_Unique_ID__c =  c.account_id;
   PFA.Advisor_ID__c = c.advisor_id;   
    PFA.Unique_ID__c =  c.account_id;
    PFA.Financial_Account_Number__c =  c.account_num;
    PFA.Account_Type__c =  c.account_type;
    PFA.Client__c =  '0015C000003VqWh';

    lstAccount.add(PFA);
}


Boolean isUpsertfirstTime = true;
try {
    upsert lstAccount Financial_Account_Number__c;
}catch (DMLException e) {
                System.debug('Re-trying');
                if(isUpsertfirstTime){
                        upsert lstAccount Financial_Account_Number__c;
                                isUpsertfirstTime = false;
              }
}
1
  • You can also run into other limits such as HTTP response size or Apex heap size if you try to process an open ended number of records. The best solution is if the service you are calling has a mechanism to limit the response size so you can make multiple requests and get a limited volume of data back in each request but still get all the data. (If you are writing that service you can design that in.)
    – Keith C
    Oct 17 '16 at 11:39
0

You're upserting too big of a list, refactor those last ten lines into a class that implements Database.Batchable.

https://developer.salesforce.com/docs/atlas.en-us.apexcode.meta/apexcode/apex_batch_interface.htm

"Using an Iterable in Batch Apex to Define Scope" in the previous link gives you a close enough example to get you going.

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.