You need to sign in to do that
Don't have an account?
Yoni Legacy
Apex trigger with Helper Class not working
Hello,
I'm very new to apex coding. I was informed that the proper way of creating trigger is to create a trigger then call apex class methods. Is that right?
I tried it on my end with a very simple trigger but I can't make it work.
Basically, what I want is that when I create Account, it will automatically create a Contact under the Account with Account is equal to Account id and contact last name is equal to account last name.
Attached is my trigger and class.
Class
Thank you
Marion
I'm very new to apex coding. I was informed that the proper way of creating trigger is to create a trigger then call apex class methods. Is that right?
I tried it on my end with a very simple trigger but I can't make it work.
Basically, what I want is that when I create Account, it will automatically create a Contact under the Account with Account is equal to Account id and contact last name is equal to account last name.
Attached is my trigger and class.
Class
Thank you
Marion
As we have explained that you have made mistake only in line number 11 so you should only replace this line as Amit suggested above. Only for your reference I am re-writing trigger code:
Thanks
Sandeep Singhal
http://www.codespokes.com
All Answers
Hi
This is how you should write a trigger Handler (Best Practice)
http://developer.force.com/cookbook/recipe/trigger-pattern-for-tidy-streamlined-bulkified-triggers
And, you can also call your handler class directly.
Try this for getting a better idea.
http://www.embracingthecloud.com/2010/07/08/ASimpleTriggerTemplateForSalesforce.aspx
The error was because you were trying to assign the value of a void method (NULL) to List<Account>
Thanks
Geo
This is a good try but I would like to provide you reason of error you are getting is:
method 'CreateCont' does not return any thing but in trigger at line number 11 you are trying to assign method's output in to list of Account which should not be.
one more thing In trigger you should not use try catch because in case if error occurs user will not be able to know what happens behind the screen.
If my answer helped you please mark this as best asnwer so that it can be useful for others.
Thanks
Sandeep Singhal
http://www.codespokes.com/
Let us know if this will helo you
or return list of Contact from your helper and update insertCont to list of Contact
As we have explained that you have made mistake only in line number 11 so you should only replace this line as Amit suggested above. Only for your reference I am re-writing trigger code:
Thanks
Sandeep Singhal
http://www.codespokes.com