0

I have a Product selector class and would like to enforce a mandatory check on a Product field for every query on products. I'm assuming it has something to do with the QueryFactory, but since there is no reference to a QueryFactory being maintained in the SObjectSelector class, I can't figure out how to add this condition to every QueryFactory instance of my Productselector class.

What would be the best way to achieve this?

0

Here's one approach:

Given a Product2 selector class something like the following:

public virtual class ProductsSelector  implements IProductsSelector {

    public override String getOrderby() {return 'ProductCode';}

    public List<Schema.SObjectField> getSObjectFieldList() {
        return new List<Schema.SObjectField> {  
                Product2.ID,
                Product2.Description,
                ...  
        };
    }

    public static IProductsSelector newInstance()    {
        return (IProductsSelector) 
           Application.Selector.newInstance(Product2.SObjectType);
    }

    public Schema.SobjectType getSObjectType() {return Product2.SObjectType;}

    public virtual List<Product2> selectById(Set<ID> ids){
        return (List<Product2>) selectSObjectsById(ids);
    }

    public virtual List<Product2> selectByProductCode(set<String> productCodes) {
        fflib_QueryFactory pQueryFactory    = newQueryFactory()                                                      
             .setCondition('ProductCode IN :productCodes ' + getMandatoryCondition())
                                                ;
        return Database.query(pQueryFactory.toSOQL());
    } 

    public virtual List<Product2> selectByName(set<String> productNames) {
       fflib_QueryFactory pQueryFactory = newQueryFactory()                                                      
                 .setCondition('ProductCode IN :productCodes ' + getMandatoryCondition() )
                                                    ;
      return Database.query(pQueryFactory.toSOQL());
    } 

   private String getMandatoryCondition() {
     return ' AND MyMandatory_Field__c != null ';
   }
}

you can add a mandatory condition into each of your setCondition calls

| 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.