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
Abhishek PurohitAbhishek Purohit 

I just started with apex triggers directly..I Want to know how can I write a Helper class. Please help

trigger OpportunityCreator on Account (after insert) {
 
 if(Trigger.isInsert)
 {
   List<Opportunity> oCreate=new List<Opportunity>();
   List<Activity_Plan__c> actplanList=new List<Activity_Plan__c>();
   for(Account acc :Trigger.New)
   {
    
    Opportunity op=new Opportunity();
    Activity_Plan__c apobj =new  Activity_Plan__c();
    
   //op.accountId=acc.Id; 
    op.Name=acc.Name;
    op.CloseDate=Date.today();
    op.StageName='Prospecting';
    apobj.name=acc.Name;
    apobj.Object__c=acc.Name;
   
   oCreate.add(op);  
   actplanList.add(apobj);
 } 
 
 insert oCreate;
 insert actplanList;
  
 }
 

}

 
Best Answer chosen by Abhishek Purohit
Nagendra ChinchinadaNagendra Chinchinada
Hi Abhishek,
Here is the example for helper class for ur scenario.
public class AccountHelper{

public static void opportunityCreate(List<Account> accList) {

List<Opportunity> oCreate=new List<Opportunity>();
List<Activity_Plan__c> actplanList=new List<Activity_Plan__c>();
for(Account acc :accList){

Opportunity op=new Opportunity();
Activity_Plan__c apobj =new  Activity_Plan__c();

//op.accountId=acc.Id; 
op.Name=acc.Name;
op.CloseDate=Date.today();
op.StageName='Prospecting';
apobj.name=acc.Name;
apobj.Object__c=acc.Name;

oCreate.add(op);  
actplanList.add(apobj);
} 

insert oCreate;
insert actplanList;

}

Changed Trigger code is,
 
trigger OpportunityCreator on Account (after insert) {
 
 if(Trigger.isInsert){
 AccountHelper.opportunityCreate(Trigger.New);
 }
 }


Please let me know if it helps.
 

All Answers

Nagendra ChinchinadaNagendra Chinchinada
Hi Abhishek,
Here is the example for helper class for ur scenario.
public class AccountHelper{

public static void opportunityCreate(List<Account> accList) {

List<Opportunity> oCreate=new List<Opportunity>();
List<Activity_Plan__c> actplanList=new List<Activity_Plan__c>();
for(Account acc :accList){

Opportunity op=new Opportunity();
Activity_Plan__c apobj =new  Activity_Plan__c();

//op.accountId=acc.Id; 
op.Name=acc.Name;
op.CloseDate=Date.today();
op.StageName='Prospecting';
apobj.name=acc.Name;
apobj.Object__c=acc.Name;

oCreate.add(op);  
actplanList.add(apobj);
} 

insert oCreate;
insert actplanList;

}

Changed Trigger code is,
 
trigger OpportunityCreator on Account (after insert) {
 
 if(Trigger.isInsert){
 AccountHelper.opportunityCreate(Trigger.New);
 }
 }


Please let me know if it helps.
 
This was selected as the best answer
Abhishek PurohitAbhishek Purohit
Thank you sir...can you please suggest some topics in order to achieve excellence in trigger and helper classes?