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
Pranav Mandlik 16Pranav Mandlik 16 

task not creating under accounts

I am new to salesforce i am writing trigger test class, i want to create task under account but after execution of this trigger is passes the test but task is not creating under account i have written test code for a trigger
 
Account account      = [select Id,ownerId from Account limit 1];

            Task t       = new Task();
            t.whatId     = account.Id;
            t.subject    = 'Email';
            t.OwnerId    = account.ownerId;
            t.status     = 'In progress';
            t.priority   = 'Normal';
            insert t;      

            String type = [select Id,Type from task where Id =:t.Id].type;
            system.assertEquals('Email', type);

the task is not creating under that account i dont know whats the problem
please share any ideas over this
Abdul KhatriAbdul Khatri
You first need to insert an Account
Account account = new Account (Name = 'Test Account'); 
Insert account;

Task t = new Task(); 
t.whatId = account.Id; 
t.subject = 'Email'; 
t.OwnerId = account.ownerId; 
t.status = 'In progress'; 
t.priority = 'Normal'; 
insert t; 

String type = [select Id,Type from task where Id =:t.Id].type; 
system.assertEquals('Email', type);

 
Raj VakatiRaj Vakati
try this code
 
trigger AccountTrigger on Account (after insert) {
List<Task> taskList = new List<Task>(); //Creating a list for the tasks that are created once an account is created.
	for(Account a : Trigger.new) {
		
		 Task t       = new Task();
		t.whatId     = a.Id;
		t.subject    = 'Email';
		t.OwnerId    = account.ownerId;
		t.status     = 'In progress';
		t.priority   = 'Normal';
		insert t;      
		
		
			taskList.add(t);
		
	}
	insert taskList;

}

 
Raj VakatiRaj Vakati
Use this code pls
 
trigger AccountTrigger on Account (after insert) {
List<Task> taskList = new List<Task>(); //Creating a list for the tasks that are created once an account is created.
	for(Account a : Trigger.new) {
		
		 Task t       = new Task();
		t.whatId     = a.Id;
		t.subject    = 'Email';
		t.OwnerId    = a.ownerId;
		t.status     = 'In progress';
		t.priority   = 'Normal';
		
		
			taskList.add(t);
		
	}
	insert taskList;

}