I want to transfer a trigger code into apex class, and the trigger code happens to have trigger.isinsert and trigger.new attributes. Can you suggest me how to do it?

  • These two can be the helpful links that can be referred: salesforce.stackexchange.com/questions/61237/… salesforce.stackexchange.com/questions/41591/… Oct 24 '17 at 6:31
  • This question really does need additional details for anyone to be able to provide more than general advice. From other comments, it appears that you may want to transfer your code into a Visualforce controller. That would be good information to include in your question, as it does change the approach that you'll need to take. Including the code for the trigger you're trying to transfer into a class would also help (and any code for the class that you already have/tried). You can edit your question to include these details.
    – Derek F
    Oct 24 '17 at 14:49
  • What it boils down to, in the end, is that we have no idea what the code you're looking at is. Right now, we're basically driving a car blindfolded, and you're asking us to drive you somewhere. We could try to make some turns, but being blindfolded, there's a high chance we'll end up crashing into another vehicle, a building, or maybe driving off of a bridge. You need to remove our blindfold, and you do that by including as much detail as you can. What is the code you're working with? What are the details about the problem you're trying to solve?
    – Derek F
    Oct 24 '17 at 14:57
  • With the information you've provided so far, the only thing I can say is, "then why don't you simply remove the trigger context variables from your code?" If your rebuttal to that is "ok, but then my code doesn't compile" or "but how will I know which records to insert/update?" how do you expect us to know that if you don't provide details?
    – Derek F
    Oct 24 '17 at 15:01

These variables are accesssible in the Trigger's Context.

You can refer below links, which explains using a handler along with a Apex Trigger.



  • I don't want to call it from a trigger.. I just want to create a method, instead of a trigger.
    – Trishagni
    Oct 24 '17 at 7:15
  • @Trishagni Well, if you don't plan on using a Trigger, then where do you plan on invoking your method ? A batch class ? VF Page ? API ? Oct 24 '17 at 7:42
  • @JitenderPadda: I have a controller class for that object. I want to implement the logic in a method in that class instead using a trigger.
    – Trishagni
    Oct 24 '17 at 8:24
  • @Trishagni Trigger context variables can be used outside of a trigger, so long as the piece of code using a trigger context variable was called (directly or indirectly) as part of a trigger. Additional caution is due when using trigger context variables outside of a trigger body though.
    – Derek F
    Oct 24 '17 at 14:42
  • @DerekF: Thanks for reverting back, the problem is solved . i am really new in salesforce and started coding about 3 months back, i am still learning, sorry for not providing you with all the info!
    – Trishagni
    Oct 25 '17 at 6:27

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.