fflib-apex-common icon indicating copy to clipboard operation
fflib-apex-common copied to clipboard

CurrencyIsoCode being added to subselect query on Who relationship for tasks

Open costonb opened this issue 3 years ago • 7 comments

We have a Task Selector class and one of its methods builds up a subquery on the Who relationship. We noticed in multi currency orgs it is not only adding CurrencyIsoCode to the task query but also adding Who.CurrencyIsoCode. This causes it to throw an error since that fields does not exist.

Here's an example, I tried to trim it down as much as I could, so hopefully I didn't cut out anything important.

public with sharing class TasksSelector extends fflib_SObjectSelector {
    public static TasksSelector newInstance() {
        return (TasksSelector) Application.Selector.newInstance(Task.SObjectType);
    }

    public override String getOrderBy() {
        return Task.WhatId.getDescribe().getName();
    }

    public List<Schema.SObjectField> getSObjectFieldList() {
        return new List<Schema.SObjectField>{
            Task.Id,
            Task.OwnerId,
            Task.Subject,
            Task.ActivityDate,
            Task.Status,
            Task.WhatId,
            Task.CreatedById,
            Task.IsClosed
        };
    }

    public Schema.SObjectType getSObjectType() {
        return Task.sObjectType;
    }

    public List<Task> selectByWhatIdsWithOwnersAndContacts(
        Set<Id> idSet
    ) {

        fflib_QueryFactory taskQueryFactory = newQueryFactory();
        ContactSelector.newInstance().configureQueryFactoryFields(taskQueryFactory, 'WhoId');

        String whereClause = 'WhatId IN :idSet';

        List<Task> result = new List<Task>();
        result = Database.query(
            taskQueryFactory
                .selectField('Owner.Name')
                .selectField('What.Name')
                .setCondition(whereClause)
                .addOrdering(Task.ActivityDate, fflib_QueryFactory.SortOrder.ASCENDING, true)
                .addOrdering(Task.Subject, fflib_QueryFactory.SortOrder.ASCENDING, true)
                .toSOQL()
        );

        return result;
    }
}

costonb avatar Jun 15 '21 22:06 costonb

@ImJohnMDaniel Since you and I regularly debate matters of pattern implementations, do you have observations about the general pattern existing here?

stohn777 avatar Jun 29 '21 16:06 stohn777

I'm getting a test failure upon running all tests after an initial deploy to our org: fflib_QueryFactory.InvalidFieldException: Invalid field 'CurrencyIsoCode' for object 'Group'

Class.fflib_QueryFactory.getFieldPath: line 131, column 1 Class.fflib_QueryFactory.selectField: line 224, column 1 Class.fflib_SObjectSelector.configureQueryFactoryFields: line 388, column 1 Class.fflib_SObjectSelectorTest.testPolymorphicSelectWithRelatedType: line 486, column 1

It appears to be related to this issue, checking for CurrencyIsoCode as a field on an Object where it doesn't exist. I thought I'd bump this thread back up and see if anyone else has experienced this.

zkilburn86 avatar Oct 17 '22 21:10 zkilburn86

@zkilburn86 -- thanks for reaching out. Is this error on an established codebase or is this brand new code/work?? If it is established, when did you first notice this issue?

ImJohnMDaniel avatar Oct 17 '22 21:10 ImJohnMDaniel

Hi @ImJohnMDaniel thanks for the quick response! It is an existing code base, I did an initial deploy of the Apex Mocks and Apex Core master branches into a sandbox today, then ran the all tests to make sure everything checked out.

zkilburn86 avatar Oct 17 '22 21:10 zkilburn86

Ok. Let me check into it and see if I can replicate it

ImJohnMDaniel avatar Oct 17 '22 22:10 ImJohnMDaniel

@zkilburn86 -- I have checked the codebase against the reference scratch org config that we use and nothing out of the ordinary is occurring. Is there any more information that you can provide that would help us reproduce the issue over here??

ImJohnMDaniel avatar Oct 18 '22 02:10 ImJohnMDaniel

@ImJohnMDaniel sure thing, just to eliminate any conflicts with existing code I created a new scratch org and pushed only the Apex Mocks and Apex Core components.

Here's the project-scratch-def.json I used to generate the org: { "orgName": "zkilburn company", "edition": "Developer", "features": ["EnableSetPasswordInApi"], "settings": { "lightningExperienceSettings": { "enableS1DesktopEnabled": true }, "mobileSettings": { "enableS1EncryptedStoragePref2": false } } }

I ran all tests in the org after push and they all passed as expected. Then I enabled multiple currencies from Setup > Company Information > Activate Multiple Currencies. I ran the tests again and got the failure from above Class.fflib_SObjectSelectorTest.testPolymorphicSelectWithRelatedType: line 486, column 1 - fflib_QueryFactory.InvalidFieldException: Invalid field 'CurrencyIsoCode' for object 'Group'

zkilburn86 avatar Oct 18 '22 02:10 zkilburn86