0

I have Class A which calls Class B multiple times. Class B has a method with SOQL which returns only one row each time it gets called. Will it hit Governor limit ? or it will be resetting the governor limit each time the class B gets called.

class A{
  string xyz = class B.method();
}

class B{
 method{
    SOQL with LIMIT 1;
    return something;
 }
}
0

Governor limits are cumulative throughout an entire transaction. They do not reset when you return from a method.

If this is the only thing you're running in a transaction (which is highly unlikely), and you end up running it less than 101 times, then you'd be fine. The far better approach for the Salesforce platform, however, is to work on collections instead of individual records.

You should take a long, hard look at class B and try to have it return more than a single row. It'll probably mean that you need to do some additional processing of the results, but the CPU limit is a lot harder to run into than the SOQL limit.

| improve this answer | |
0

The simplest answer is Yes, Every call to class B method will be considered in governer limit.

| improve this answer | |
0

It depends on how you are calling the class A. If the method calls in a constructor of class A, then it will be called each time a new instance is created and you may hit the limit on the number of SOQL queries in a transaction which would be 100 assuming this is the only code executing in the transaction.

In your case, the query will be called when you create an instance any try to access xyz.

Class A

public with sharing class ClassA {
    public Integer a = ClassB.method();
}

Class B

public with sharing class ClassB {
    public static Integer method(){
        return [SELECT count() FROM Contact];
    }
}

Test Case 1: Query executed 1 time only.

ClassA a = new ClassA();
System.debug(a.a);
System.debug(a.a);

Test Case 1: Query executed 2 times.

ClassA a = new ClassA();
System.debug(a.a);
System.debug(a.a);

a = new ClassA();
System.debug(a.a);
System.debug(a.a);
| improve this answer | |

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.