Code Monkey home page Code Monkey logo

Comments (11)

afawcett avatar afawcett commented on June 30, 2024

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.

dhuckins avatar dhuckins commented on June 30, 2024

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.

afawcett avatar afawcett commented on June 30, 2024

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.

dhuckins avatar dhuckins commented on June 30, 2024

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.

afawcett avatar afawcett commented on June 30, 2024

Yeah for sure, have you tried passing this through Salesforce SUpport?

from fflib-apex-common.

dhuckins avatar dhuckins commented on June 30, 2024

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.

tfuda avatar tfuda commented on June 30, 2024

@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.

cropredyHelix avatar cropredyHelix commented on June 30, 2024

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.

KalpeshChande avatar KalpeshChande commented on June 30, 2024

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!

"How to call the methods of this class from another class?"

from fflib-apex-common.

ImJohnMDaniel avatar ImJohnMDaniel commented on June 30, 2024

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.

cropredyHelix avatar cropredyHelix commented on June 30, 2024

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)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo 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.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.