Single line code to creates and delete record in apex

 Sometimes we need to write a code to create some test records and to delete some test records once its' testing is over. so here we will show you how easily to create and delete records in apex. 

A single line creates a record

// multi line 
Account accountObj = new Account();
accountObj.Name='test Account';
accountObj.phone='1234569870';
insert accountObj;

// now in single line 
insert (new Account(Name='test Account', phone='1234569870'));

Query and delete test records:

// multi line if else
List<Account> accountLst = [Select Id From Account];
Delete accountLst;

// now in single line if-else
 delete [Select Id From Account];
Labels:
Join the conversation