Comments (11)
Sounds reasonable. I've flagged this as an enhancement, can you share a snippet as an example to be clear i understand what you mean?
from fflib-apex-common.
Sure;
In this case the org has some form data comming in from external sources, while having some fairly complex duplicate matching rules.
The requirements were to allow all form submissions to go through, bypassing the duplicate rules, and then they will handle the possible duplicates after the fact.
This can be done by using the Database.DMLOptions.DuplicateRuleHeader
(docs) and passing a new fflib_SObjectUnitOfWork.IDML to the uow.
public class MyDML implements fflib_SObjectUnitOfWork.IDML
{
private Database.DMLOptions dmlOptions;
public MyDML()
{
dmlOptions = new Database.DMLOptions();
dmlOptions.DuplicateRuleHeader.allowSave = true;
}
public void dmlInsert(List<SObject> objList)
{
Database.insert(objList, dmlOptions);
}
public void dmlUpdate(List<SObject> objList)
{
Database.update(objList, dmlOptions);
}
public void dmlDelete(List<SObject> objList)
{
delete objList;
}
}
Right now, MyDML
can be passed in to construct a new uow, but is not a parameter in fflib_Application.UnitOfWorkFactory
Should it even be an option?
Let me know what you think.
thanks!
from fflib-apex-common.
Maybe an overload to the fflib_Application.UnitOfWorkFactory constructor so you can do...
public static final fflib_Application.UnitOfWorkFactory UnitOfWork =
new fflib_Application.UnitOfWorkFactory(
new List<SObjectType> {
Invoice__c.SObjectType,
InvoiceLine__c.SObjectType,
Opportunity.SObjectType,
Product2.SObjectType,
PricebookEntry.SObjectType,
OpportunityLineItem.SObjectType },
new MyDML());
Then when you use the factory it creates a new instance with the DML impl given...
fflib_IUnitOfWork uow = Application.UnitOfWork.newInstance();
This would ensure its always applied across your app.
The other thought if its uow instance specific....
fflib_IUnitOfWork uow = Application.UnitOfWork.newInstance(new MyDML());
from fflib-apex-common.
Requesting to close this issue with some words of warning to anyone trying the same (or at least bring up a possible discussion point).
Using Database.DMLOptions.DuplicateRuleHeader.allowSave
will indeed let you save over duplicate rules in place; however, if the record fails a validation rule it will fail silently
Example: There is a validation rule on Account, that throws an error when 'Fail_Me__c' = true.
Database.DMLOptions dmlOptions = new Database.DMLOptions();
dmlOptions.DuplicateRuleHeader.allowSave = true;
List<SObject> toInsert = new List<SObject>();
Account iPass = new Account();
iPass.Name = 'I Pass validation';
iPass.Fail_Me__c = false;
toInsert.add(iPass);
Account iFail = new Account();
iFail.Name = 'I Fail validation';
iFail.Fail_Me__c = true; // should get an error
toInsert.add(iFail);
Database.insert(toInsert, dmlOptions);
/*shouldn't get here but we do*/
System.debug('*** iPass.Id: ' + iPass.Id);
System.debug('*** iFail.Id: ' + iFail.Id);
Completes successfully, no errors, with a result of:
USER_DEBUG|[15]|ERROR|*** iPass.Id: 00136000002X7U5AAK
USER_DEBUG|[17]|ERROR|*** iFail.Id: null
Maybe I'm not using it correctly, but something seems off here right?
from fflib-apex-common.
Yeah for sure, have you tried passing this through Salesforce SUpport?
from fflib-apex-common.
Yes, long story short, there was no solution offered.
Due to the strict deadline on the project we discarded the requirement (at the expense of the users unfortunately)
Just posted the problem on stackexchange.
Thanks for your support @afawcett
from fflib-apex-common.
@daniel-huckins What if you specify dmlOptions.optAllOrNone = true? By default, that option is false. And you then have to check the Database.SaveResult object to determine which, if any of your records has errors.
from fflib-apex-common.
One would think the solution would be to simply setdmlOptions
on the registered sobject as the Apex documentation says should be possible. However, Known Issue prevents this. See also stackexchange post on issue (motivated by trying to use registerDirty on an sobject with dmlOptions applied via setDmlOptions)
from fflib-apex-common.
Sure; In this case the org has some form data comming in from external sources, while having some fairly complex duplicate matching rules. The requirements were to allow all form submissions to go through, bypassing the duplicate rules, and then they will handle the possible duplicates after the fact. This can be done by using the
Database.DMLOptions.DuplicateRuleHeader
(docs) and passing a new fflib_SObjectUnitOfWork.IDML to the uow.public class MyDML implements fflib_SObjectUnitOfWork.IDML { private Database.DMLOptions dmlOptions; public MyDML() { dmlOptions = new Database.DMLOptions(); dmlOptions.DuplicateRuleHeader.allowSave = true; } public void dmlInsert(List<SObject> objList) { Database.insert(objList, dmlOptions); } public void dmlUpdate(List<SObject> objList) { Database.update(objList, dmlOptions); } public void dmlDelete(List<SObject> objList) { delete objList; } }Right now,
MyDML
can be passed in to construct a new uow, but is not a parameter infflib_Application.UnitOfWorkFactory
Should it even be an option? Let me know what you think.
thanks!
"How to call the methods of this class from another class?"
from fflib-apex-common.
G'day @KalpeshChande. Is your question "how would you access the IDML implementation that was passed to the UOW?" or "If you are using the fflib_Application.UnitOfWorkFactory, how do you pass a custom IDML?"
from fflib-apex-common.
The class that implements IDML has public methods but why would you want to call those directly from some other class? They are intended to be called from the UnitOfWork object as an alternate implementation of the OOTB SimpleDml inner class
from fflib-apex-common.
Related Issues (20)
- Run AEP Apex Sample Code tests against the core AEP Commons library on each push
- Unable to Deploy - Unhandled Exception : java.lang.RuntimeException HOT 1
- Unit test failure in multi-currency org HOT 4
- Deploy button doesn't deploy because there are test failures HOT 1
- Expected a QueryException due to read only user not having access to Opportunity HOT 7
- Switch to Inherited Sharing on SObjectDescribe and SObjectSelector HOT 2
- With latest fflib, do TriggerHandlers become service class consumers? If so what happens to UoW? HOT 6
- Selected tests in fflib_SObjectSelectorTest fail in an org with encryption enabled on Account.Name HOT 2
- Selector Mocks to Include SOQL Query Retrieve Check HOT 1
- fflib_SObjectUnitOfWork doCommitWork executes all dml for all registered types even if there are no changes
- Update README as session recordings are not available HOT 2
- fflib_SObjectDescribe.cls fails to resolve cross-object field paths for Person Accounts
- Aggregate SOQL support. HOT 2
- Disable Savepoint in tests HOT 4
- Inconsistent Code Coverage and Test Failures in fflib-apex-common HOT 1
- fflib_SObjectSelector and fflib_SObjectUnitOfWork have insufficient test code coverage. HOT 2
- Add queryWithBinds to Selector layer HOT 2
- Unit test failing fflib_SecurityUtilsTest & sysadmin_objectAndField_access HOT 8
- Coverage of UnitOfWork class HOT 1
- Versioning HOT 1
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
D3
Bring data to life with SVG, Canvas and HTML. 📊📈🎉
-
Recommend Topics
-
javascript
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
-
web
Some thing interesting about web. New door for the world.
-
server
A server is a program made to process requests and deliver data to clients.
-
Machine learning
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from fflib-apex-common.