lukethacoder / fflib.dev

⚡ Unofficial and semi-opinionated fflib documentation - Apex Enterprise Patterns
https://fflib.dev
MIT License
6 stars 0 forks source link

Unit of Work: Register Relationship during insert fails if both records are of the same SObjectType #7

Closed ChrisGottlieb closed 1 month ago

ChrisGottlieb commented 1 month ago

How to recreate:

`fflib_SObjectUnitOfWork uow = new fflib_SObjectUnitOfWork(MY_SOBJECTS, mockDML);

Account parent = new Account(Name = 'Parent'); Account child = new Account(Name = 'Child'); uow.registerNew(parent); uow.registerNew(child); uow.registerRelationship(child, Account.ParentId, Parent); uow.commitWork();

child = [SELECT ParentId FROM Account WHERE Id =:child.Id]; Assert.isNotNull(child.ParentId); Assert.areEqual(parentId, child.ParentId); `

Expected behaviour is that this should pass, but it doesn't. This code might need to be fixed, I'm typing this freely. The solution we found is through an extension class to:

` public void registerRelationship(SObject record, Schema.SObjectField relatedToField, SObject relatedTo){ if (record.Id == null && relatedTo.Id == null && record.getSObjectType() == relatedTo.getSObjectType()){ record.Id = fflib_IDGenerator.generate(record.getSObjectType()); uow.registerRelationship(record, relatedToField, relatedTo); uow.registerDirty(record); record.Id = null; }else{ uow.registerRelationship(record, relatedToField, relatedTo); } }

`

I think it would make a nice addition to the framework

ChrisGottlieb commented 1 month ago

sorry I added this in the wrong repo, I'll move it to the main one