function readOnly(count){ }
Starting November 20, the site will be set to read-only. On December 4, 2023,
forum discussions will move to the Trailblazer Community.
+ Start a Discussion
RudrAbhishekRudrAbhishek 

i need to create a new task with every new account via Apex Trigger

Hello All,

Please help me out with suggesting that how i need to proceed with Apex Trigger for creating new task on successful creation of new Account.

 

Regards,

RudrAbhishek

desmoquattrodesmoquattro
Abhishek - You can actually do this declaratively with a simple workflow rule. No code needed.

If you *must* use a trigger, it's a pretty simple one. There are a few examples on Developerforce:
http://www.salesforce.com/us/developer/docs/apexcode/Content/apex_qs_HelloWorld.htm
http://developer.force.com/cookbook/category/triggers/recent
Satish_SFDCSatish_SFDC
Yes,
This is possible with a Workflow rule.
Goto Setup > Create > Workflow & Approvals > Workflow Rules.
Create a new Workflow rule.

Object: Account.
Rule Name: anything
Evaluation Criteria: Created
Rule Criteria : 
                       Run the rule if the following: 'Formula evaluates to true'
In the formula box just type true (this fires the rule everytime an account is created)\

'Save & Next'

Add A Workflow Action:
     New Task

The fields are self explanatory. Activate the Workflow rule and you are done.



However, if you want it done through a trigger the following code should help.

<pre>
trigger AccountTrigger on Account (after insert) {
    List<Task> lstTasks = new List<Task>();
    for(Account a : Trigger.new){       
        Task t = new Task();
        t.OwnerID = a.OwnerID;
        t.Subject = 'Any Subject';
        t.WhatID=a.Id;
        t.Status = 'Not Started';
        //Add any other fields you need.
        lstTasks.add(t);      
    }
    insert lstTasks;
}
</pre>

Hope this helps.

Regards,
Satish Kumar