EDIT:
Sharing code for a scenario where on creation of an account with rating as 'Hot' a contact gets created under it automatically. A process builder with entry condition of rating equals 'Hot' had been written which calls a class with invocable method.
Class:
public with sharing class example{
//Using Invocable annotation so that method can be used by Process builder flow
@InvocableMethod(label = 'Insert Contact records' description='Insert Contact records by default on creation of Account record ')
public static void insertContact(List<Id> accountId){
List<Contact> updatedContactList = new List<Contact>();
List<Account> updatedAccountList = new List<Account>();
for(Account acc : [SELECT Id FROM Account WHERE Id IN:accountId LIMIT : Limits.getLimitQueryRows() - Limits.getQueryRows()] ){
Contact con = new Contact();
con.AccountId = formula.Id;
con.LastName = 'test';
updatedContactList.add(formulaSpec);
updatedAccountList.add(formula);
}
Savepoint sp = Database.setSavepoint();
try{
if(updatedAccountList.size() > 0){
Database.upsert(updatedAccountList);
}
if(updatedContactList.size() > 0){
Database.insert(updatedContactList);
}
}
catch(Exception e){
Database.rollback(sp);
system.debug('The exception is '+e);
}
}
}
Test class block:
Test.startTest();
//Creating account
Account account = new Account();
Account.Name = 'test account';
Account.Rating = 'Hot';
Database.insert(formula);
Account accountInserted = [SELECT Id FROM Account LIMIT 1];
List<Id> accountIdList = new List<Id>();
accountIdList.add(accountInserted.Id);
example.insertContact(accountIdList);
Test.stopTest();
Contact contList=[SELECT Id FROM Contact];
System.assertEquals(1,contList.size()); // this would return 2 if flow is active causing test class to fail; if flow is inactive then this would pass
This is just a hypothetical scenario i'm taking to explain my problem. With process builder it is possible to insert a record without writing any code.