0

I'm having a small doubt.

In a class there are 2 constructors. From each constructor we are calling a same Private method loadPriceMap() (But with different conditions).

public class PriceCalculator { 

   List<Account> accList;
   List<Product2> prodList;

   public PriceCalculator(List<Account> accListParm) {
        accList=  accListParm;
        loadPriceMap();
    }

    public PriceCalculator(List<Product2> productListParm) {
        prodList= productListParm;      
        loadPriceMap();
    }

    private void loadPriceMap() {
        // TO DO : If the class was instantiated with a list of Product2 objects,
        // then do something. else If the class was instantiated with a list of 
        //Account objects, then do other thing
    }

My question is, can we differnitate that from which constrouctor this private method is being called?

2
  • Apex doesn't have any cool language features to help you do this. In you example, assuming when the constructors are used their arguments are not null, you can just check which of the lists is not null.
    – Keith C
    May 28, 2014 at 14:43
  • Namely, we can't access the stack, at least not directly. You have no idea how much I'd love to have access to the stack so we can figure out things like "how much more stack do I have before I have a stack overflow", etc. Instead, you just have to "know" where you are. In this simple case, it's easy, but I could imagine having a dozen entry points...
    – sfdcfox
    May 28, 2014 at 16:20

2 Answers 2

2

This would be an easy way. Note that I'm not using using accList.size() or accList.isEmpty() because they will throw a NullPointerException

private void loadPriceMap() {
    if (accList != null) {
        // Process Accounts 
    else if (prodList != null) {
        // Process Products
    } else {
        // Handle invalid case
    }
}
1
  • 1
    Don't handle an invalid case that can't be handled. Bad for test coverage. Since there are two constructors, we know there is no "default" constructor, so this third case can never happen.
    – sfdcfox
    May 28, 2014 at 16:10
1

You can either pass a parameter in the method declaring the Object type, or you can retrieve the first object in the list and use describe to get the type.

//If Account
If (lSObjects[0].getsObjectType() == Account.sObjectType)
{
}

The question is do you really need this ? From your code I would expect that either of the 2 lists will be null...

2
  • Yes either 1 of this list will be null. private void loadProductMap() { if(!accList.isEmpty() || accList!= null) { //Do Something } // Do something else } May 28, 2014 at 15:07
  • 1
    If you were using lSObjects as a Sobject[], but the list were empty, you'd get an exception. However, getSobjectType works on the native list itself, so you can say if(lSObjects.getSobjectType() == Account.SObjectType) ...
    – sfdcfox
    May 28, 2014 at 16:22

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .