+ Start a Discussion
parth jollyparth jolly 

Create a task in Apex class related to Account

Here is the situation if Account's Laptop delivered lapp_c is not null then create a task related to it in the account through apex class.

Any idea ?
 
Arvind KumarArvind Kumar
Hi parth,

Use below class & Trigger.

Apex Class:
public class Accounthelper
{   
    List<Task> tskList = new List<Task>();

       public void createTask(List<Account> accList)
	   {
	          for(Account accObj : accList)
			  {
			      if(accObj.field ! = null)
				  {
				     Task tsk = new Task();
					 tsk.Subject = 'Test Task';
					 tsk.Status = 'Open';
					 tsk.Priority = 'Normal';
					 tsk.WhatId = accObj.ID;
					 tsk.OwnerId = accObj.OwnerId;
					 tskList.add(tsk);
				  }
			  }
			        if(tskList!=null && tskList.size()>0)
			  
			           insert tskList;
	   }
}

Apex Trigger:
Trigger accountTrigger on Account(after insert, after update)
{
         AccountHelper helper;
		 helper.createTask(Trigger.new);
}

Thanks
RamakantRamakant
Hi Parth,
Use below code:
Trigger:
Trigger TriggerName on Account(after insert, after update)
{
         
		 AccountHandler.createTaskforLapp(Trigger.new);
}
Class:
public class Accounthelper{   
	List<Task> taskList = new List<Task>();

	public void createTaskforLapp(List<Account> accountList){
		if( accountList.size()>0){	
			for(Account acc : accountList)
			{
			    if(acc.lapp_c ! = null)
			    {
					Task taskRecord = new Task();
					taskRecord.Subject = 'Task subject';
					//Add your field mappings
					//give whatid = acc.id;
					taskList.add( taskRecord );
				}
			}
				if(taskList.size()>0)
				{
					insert taskList;
				}	
		}		   
	}
}

PLease let me now if helped.

Regards,
Ramakant​