0

Maybe a simple question: is there a way I can use the same method to create/update records in a before trigger and in a button?

I have an Apex class. Let's call it Stuff.apxc.

public class Stuff {
    public static void doStuff(List<SObject> newObjs) {
        List<Custom_Object__c> customObj = (List<Custom_Object__c>) newObjs;
        // do things like update related records and created other objects, etc. etc.
    }
}

I currently call this method in a before insert trigger.

trigger myTrigger on Custom_Object__c (before insert) {
    Stuff.doStuff(Trigger.new);
}

So far so good. Everything works.

Now, there are scenarios where Salesforce users will need to manually review the information in the Custom_Object__c and perhaps fill in some fields or create some Contact or Account records, and for very specific reasons, we cannot automate this.

Once users have finished their review, I want to enable users to re-run the doStuff code by means of a button. However, there are update statements that write information to the Custom_Object__c record would need to be explicitly used in the button that are not used in the before insert trigger.

So is this possible? Is there a prescribed workaround for this?

5
  • 1
    Welcome! I'm not quite clear on this quote there are insert statements that would need to be explicitly used in the button that are not used in the before insert trigger? Are you just referring to the fact that your method relies on the custom object to be inserted? Jul 12 '21 at 22:48
  • Does "button" mean Quick Action or Custom Button here?
    – cropredy
    Jul 12 '21 at 22:56
  • @KrisGoncalves - I updated the wording, so hopefully it's clearer. I am referring to how in a before insert trigger I do not need to explicitly update the record being inserted by means of an update statement, while if I call this method with a custom button/Lightning Component, I would need to use update statements.
    – d-fws
    Jul 13 '21 at 16:14
  • @cropredy - I mean a custom button/Lightning Component, not a quick action.
    – d-fws
    Jul 13 '21 at 16:14
  • 1
    LWC can invoke apex, apex can do update work, then call Stuff.doStuff.
    – cropredy
    Jul 13 '21 at 16:33
0
  1. Create a Visualforce extension class that includes a method that calls your Stuff.doStuff method. The method can then return a pagereference back to the calling page.
  2. Create a visualforce page that implements the <apex:page extensions="yourExtensionClass" standardController="Your_Object__c" action="yourExtensionMethodHere"> tag.
  3. Create a custom button to your Visualforce page and add to your layout

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.