1

Request Body represents the id and name of the records that should be updated. I am giving more than 100 Records in Request Body via Postman using a POST method. When I do so I get an error response:

[ { "errorCode": "APEX_ERROR", "message": "System.LimitException: Too many SOQL queries: 101\n\nClass.restresource100.updateCase1: line 30, column 1\nClass.restresource100.updateCase: line 16, column 1" } ]

For that I have tried the following Apex Class:

@RestResource(urlMapping='/Account100/')
global  with sharing class  restresource100
{
    public list<account1> account1= new list<account1>();
    @Httpput
    global static void updateCase(){
         RestRequest req = RestContext.request;
         RestResponse res = RestContext.response;
         system.debug(req);
         Blob body = req.requestBody;
         system.debug(body);
         string body1=body.tostring();
         system.debug(body1);
         restresource100 obj = restresource100.parse(body1); 
         system.debug(obj);
         updateCase1(obj);

         }
   public static list<account> updateCase1(restresource100 iu)
     {
         list<account> fg = new list<account>();


         for(account1 i :iu.account1){

         system.debug(i.id);
         list<account> acc =[select id,name from account where id = :i.id];




         system.debug(acc);
         for(account cd:acc)
         {

         cd.Name=i.name;
         fg.add(cd);
         }
         }

        update fg;


       return null;

    }
 public class account1
 {
  public string name;
  public id id;
 }
   public static restresource100 parse(String json)
    {
        return(restresource100)System.JSON.deserialize(json, restresource100.class);
    }  

    }
4
  • How to Make the List outside the for loop , any one share the Answer
    – user31717
    Jul 12, 2016 at 12:14
  • your question is bit confusing .. you should ask like how to resolve soql 101 error.. This way others easily notice the issue
    – Ratan Paul
    Jul 12, 2016 at 12:17
  • @ Ratan yes-[ { "errorCode": "APEX_ERROR", "message": "System.LimitException: Too many SOQL queries: 101\n\nClass.restresource100.updateCase1: line 30, column 1\nClass.restresource100.updateCase: line 16, column 1" } ]
    – user31717
    Jul 12, 2016 at 12:17
  • check Adrian Larson's Answer that way you can solve the soql issue
    – Ratan Paul
    Jul 12, 2016 at 12:27

1 Answer 1

2

This question may be the most basic possible bulkification question. Never query in a loop. In this instance, you do not need to query at all. If you have a record's Id, you can update it without ever running a query.

You will find your code to work if you simplify your code to:

List<Account> records = new List<Account>();
for (Account1 wrapper : iu.account1)
{
    records.add(new Account(Id=wrapper.id, Name=wrapper.name));
}
update records;

If you are worried about duplicate Id values, use a Map instead.

Map<Id, Account> records = new Map<Id, Account>();
for (Account1 wrapper : iu.account1)
{
    records.put(wrapper.id, new Account(Id=wrapper.id, Name=wrapper.name));
}
update records.values();
3
  • where i want to do these Modifications in My Code, share the Full answer
    – user31717
    Jul 12, 2016 at 12:29
  • Replace the entire method body except for the last line (return null).
    – Adrian Larson
    Jul 12, 2016 at 12:30
  • After Replacing I am Getting this Error:[ { "errorCode": "APEX_ERROR", "message": "System.ListException: Duplicate id in list: 0012800000iCOmGAAW\n\nClass.restresource100.updateCase1: line 26, column 1\nClass.restresource100.updateCase: line 16, column 1" } ]
    – user31717
    Jul 12, 2016 at 12:32

You must log in to answer this question.