+ Start a Discussion
Yoni LegacyYoni 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.

apex trigger 

Class
User-added image

Thank you
Marion
Best Answer chosen by Yoni Legacy
sandeep@Salesforcesandeep@Salesforce
Hi Yoni, 

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: 
 
trigger createContact_trg on Account (after Insert) {

    List<Account> newAcct_lst = new List<Account>();

    for(Account a: Trigger.New) {
        newAcct_lst.add(a);
    }

    createContact_Helper_cls helper = new createContact_Helper_cls ();
    helper.createCont(newAcct_lst);
}

Thanks 
Sandeep Singhal
http://www.codespokes.com

All Answers

Geo GeorgeGeo George

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

 

sandeep@Salesforcesandeep@Salesforce
Hi Marion, 

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/
Amit Chaudhary 8Amit Chaudhary 8
Issue is that your CreateCont method is not retruning any List of Account. Update your line 11 like below
 
helper.CreateCont(newAcct_list);
Let us know if this will helo you

or return list of Contact from your helper and update insertCont to list of Contact

 
Yoni LegacyYoni Legacy
Can somebody tell me the correct code for this and if there is explanation, I would greately appreciate it.
sandeep@Salesforcesandeep@Salesforce
Hi Yoni, 

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: 
 
trigger createContact_trg on Account (after Insert) {

    List<Account> newAcct_lst = new List<Account>();

    for(Account a: Trigger.New) {
        newAcct_lst.add(a);
    }

    createContact_Helper_cls helper = new createContact_Helper_cls ();
    helper.createCont(newAcct_lst);
}

Thanks 
Sandeep Singhal
http://www.codespokes.com
This was selected as the best answer
Yoni LegacyYoni Legacy
Thank you Sandeep for the help and explanation.