1

I need use structure (or wrapper class) in Apex Enterprise Pattern.

public with sharing class Structure {
        
    public String name { get; set;}
    public Integer limit {get; set;}
    private void methodName(){
        this.name = SelectorLayerName.selectByLimit(this.limit);
}
    

When I use method from this class

private void methodName(){
    this.name = SelectorLayerName.selectByLimit(this.limit);
}

I have an error

Non static method cannot be referenced from a static context: List<sObjectName> SObjectNameSelector.selectByLimit(Integer)

Selector layer method is down.

query from SObjectNameSelector

public List<sObjectName> selectByLimit (Integer limit){
        fflib_QueryFactory sObjectNamesQueryFactory = newQueryFactory();
        return (List<sObjectName>) Database.query(
            sObjectNamesQueryFactory 
            .selectFields(new List<String>{'Id'})
            .setLimit(limit)
            .toSOQL()
        );
        //must return 
        //List<sObjectName> listSON = [SELECT Id FROM sObjectName LIMIT :limit]
    }

How can I use this helper class in my layers or do I need create it as Apex Class and that's it? How can I resolve this problem?

2 Answers 2

1

It is enough to make instance of SObjectNameSelector class (so the method selectByLimit will not be executed in a static context):

private void methodName(){
    SelectorLayerName tmp = new SelectorLayerName();
    this.name = tmp.selectByLimit(this.limit);
}
4
  • Thanks @Mariia Illarionova for your response, but I tried to do this and caught the same error Non static method cannot be referenced from a static context: fflib_QueryFactory fflib_SObjectSelector.newQueryFactory(). This method got from fflib library so I can't change it
    – oldgunner
    Jul 9, 2021 at 11:46
  • @oldgunner newQueryFactory() should be static, too. Jul 9, 2021 at 11:50
  • this method is in Apex Enterprise Pattern library files. Should I change it in my org? I think I can't.
    – oldgunner
    Jul 9, 2021 at 11:52
  • @oldgunner I have changed the approach and updated the answer. Please, check if it works for you. Jul 9, 2021 at 12:09
1

If using the fflib-recommended Application class which provides a factory for creating selector, domain, unit of work, and service layer objects, the pattern would be (assuming a selector on Account):

private void methodName(){
    Account[] accounts = AccountsSelector.newInstance().selectByLimit(this.limit);
}

If not using the fflib-recommended Application class

private void methodName(){
    Account[] accounts = 
      new AccountsSelector().selectByLimit(this.limit);
}

You must log in to answer this question.

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