0

I have written the below trigger for inserting the new account. There is no error but when i checked the accounts tab, new record was not there(My trigger is in active stage).

My doubt is, when this trigger will run and the new record will get added?

And also please let me know where i went wrong

My code:

 trigger createNewAcc on Account (After insert) {

      Account acc =new Account();
      acc.Name = 'AccountCreatedThruTrigger';
      acc.Industry = 'Education';
      insert acc ;
 }

Note: I have written this in the Setup--> Accounts-->Triggers

3 Answers 3

0

As the Name itself suggest it is "Trigger", so you need to perform some action here so that it will be Triggerd, clicking on the tab will not perform any action, your Trigger is on After Insert event so it will be fired after an Account record is inserted. There must be some Action needs to be happen on record like create,update,delete etc so that it will trigger the Apex Trigger based on respective events like After Insert,After Update, After Delete etc.

Thanks,
Nachiket

4
  • Is there anyother way that i could insert a new Account record through code?
    – Sree
    Commented Feb 1, 2017 at 6:08
  • What is your exact requirement ? there must need to be some condition on which you want to create new Account ? please share your exact requirement so that I can help you better. Commented Feb 1, 2017 at 6:18
  • Actually i was from Lotus notes. Now I am into the Salesforce project. I am very new to salesforce. An assignment was given to me to create an account record through code. Please help
    – Sree
    Commented Feb 1, 2017 at 6:43
  • I would suggest go through this trail trailhead.salesforce.com/en/apex_triggers/… you will get better idea. Commented Feb 1, 2017 at 6:45
2

Triggers run when you create, edit, delete, or undelete a record. Unfortunately, this trigger will run recursively, causing an error message to occur. You'll want to consider creating a related type of record, such as a Contact record if you're just trying to learn triggers. Also, you might want to visit the Trailhead to learn more about programming Apex Code.

0

I will just add a few words to clarify the recursion part of sfdcfox's answer:

This trigger will run whenever an account is inserted. It will create and insert a new account. The insertion of this new account is an event for this trigger to fire. The trigger will then create and insert a new account. This new account insertion will trigger the trigger again. The trigger will again create and insert a new account. And so on, and so on, and so on...

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .