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

What's the current state of reflective lookups in Unit of Work?

Open loganm opened this issue 9 years ago • 6 comments

I've read that it's not handled yet. Is that still the case? Any hacks to get around it?

loganm avatar Jul 02 '15 01:07 loganm

Looks like it doesn't, as per the following unit test. I'll have a play and see if I can make something work.

I'm thinking, because reflective lookups can't be required, the UnitOfWork could do two DML passes on those records. One to insert them and get Ids, and another to backfill the reflective lookups and update if required.

@IsTest
private static void reflectiveLookups()
{
    fflib_ISObjectUnitOfWork uow = Application.UnitOfWork.newInstance();
    Product_Category__c productCategory1 = TestObjectFactory.createProductCategory( uow );
    Product_Category__c productCategory2 = TestObjectFactory.createProductCategory( uow, productCategory1 );
    Product_Category__c productCategory3 = TestObjectFactory.createProductCategory( uow, productCategory2 );
    uow.commitWork();
    productCategory1 = [SELECT Id, Parent_Category__c FROM Product_Category__c WHERE Id = :productCategory1.Id];
    productCategory2 = [SELECT Id, Parent_Category__c FROM Product_Category__c WHERE Id = :productCategory2.Id];
    productCategory3 = [SELECT Id, Parent_Category__c FROM Product_Category__c WHERE Id = :productCategory3.Id];
    System.assertEquals(null, productCategory1.Parent_Category__c);
    System.assertEquals(productCategory1.Id, productCategory2.Parent_Category__c, 'The category should be related to a parent category');
    System.assertEquals(productCategory2.Id, productCategory3.Parent_Category__c, 'The category should be related to a parent category');
}

System.AssertException: Assertion Failed: The category should be related to a parent category: Expected: a0KO0000004aLS0MAM, Actual: null

loganm avatar Jul 02 '15 02:07 loganm

@afawcett I've added support for reflective lookups to my fork. Can you turn this into a feature request, and I'll do a pull request for review.

loganm avatar Jul 02 '15 05:07 loganm

It's maybe because its been quite a long week, but can you elaborate more on what a 'reflective lookup' is?

afawcett avatar Jul 03 '15 17:07 afawcett

Phew, tough sprint. Finally got some head space back.

By reflective lookup, I mean a self lookup, or a self relationship. A lookup that points to the same SObjectType on which it's created.

I've patched my fflib fork to handle self lookups and it's working well. Sure there is a performance hit, but I've found doing an insert followed by an update is much friendlier than trying to chain DML inserts within an SObjectType.

loganm avatar Jul 21 '15 04:07 loganm

Thanks @loganm, sounds interesting, looking forward to taking a look at this.

In the meantime have you seen this long standard PR from @adtennant?

https://github.com/financialforcedev/fflib-apex-common/pull/23

afawcett avatar Jul 25 '15 12:07 afawcett

My solution for cyclic dependancies has been in play on my org for a while now, and hasn't spat the dummy once. So I'll open a pull request of its current state for review.

loganm avatar Aug 27 '15 00:08 loganm