0

I'm facing issue that when I add LastModifiedBy profile name is getting in soql but when query it in fflib then is getting empty list

public List<Order> selectOrderIdsByProfileName(Set<Id> orderIdSet){
    return Database.query(
        newQueryFactory()
            .selectField('Id')
            .selectField('Approval_Status__c')
            .selectField('LastModifiedBy.ProfileId')
            .selectField('LastModifiedBy.Profile.Name')
            .setCondition('Id IN : orderIdSet')
            .toSOQL()
    );
}
3
  • 2
    Please stop adding random tags to your questions. Tags are meant to help convey what a question contains, what it's about. Tags also help people with knowledge find questions they may be able to answer. This doesn't appear to have anything to do with triggers or sfdx.
    – Derek F
    Jun 15, 2022 at 13:45
  • welcome to SFSE! Please see How to Ask on getting the most from the Community. Are you saying that adding the line .selectField('LastModifiedBy.Profile.Name') -- the query returns no rows but if that line is omitted, the query returns rows?
    – cropredy
    Jun 15, 2022 at 15:22
  • @cropredy ,Yes ,the query returns no rows but if that line is omitted, the query returns rows Jun 15, 2022 at 16:23

1 Answer 1

0

First of all, your selector method is misnamed per fflib conventions

You have:

 public List<Order> selectOrderIdsByProfileName(Set<Id> orderIdSet){..} 

You want:

 public List<Order> selectByOrderId(Set<Id> orderIdSet){...}

If you don't want the Queryfactory to use the fields defined in the selector class concrete method getSobjectFieldList() method, you need to tell the query factory to ignore these:

public List<Order> selectByOrderId(Set<Id> orderIdSet){
    return Database.query(
        newQueryFactory(false) // <= false - ignore selector default field names
            .selectField('Id')
            .selectField('LastModifiedBy.ProfileId')
            .selectField('LastModifiedBy.Profile.Name')
            .setCondition('Id IN : orderIdSet')
            .toSOQL()
    );
}

With this, when I execute:

 Order[] results = new OrdersSelector().selectByOrderId
                        (new Map<Id,SObject> (
                            [SELECT Id FROM Order Limit 2]).keySet());
 for (Order o: results) {
   System.debug(LoggingLevel.INFO,o.Id + ' ' + o.LastModifiedBy.Profile.Name);
 }

yields in the debug log:

SOQL_EXECUTE_BEGIN|[97]|Aggregations:0|SELECT Id, LastModifiedBy.Profile.Name, LastModifiedBy.ProfileId 
       FROM Order 
       WHERE Id IN : orderIdSet ORDER BY OrderNumber ASC NULLS FIRST
SOQL_EXECUTE_END|[97]|Rows:2

INFO|80103000001nd61AAA System Administrator
INFO|80103000001nd62AAA System Administrator

Query is constructed properly.

So, your problem lies elsewhere ...

  • Are you sure you are passing in an non-empty collection of OrderIds to the selector when the line .selectField('LastModifiedBy.Profile.Name') is included in your test?
  • Does the running user have visibility to those orders (check Sharing Settings)? (most likely reason)
  • Are you using the latest fflib repo code's fflib_SObjectSelector as super class to your selector class? Mine is at v51.0

You must log in to answer this question.

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