+ Start a Discussion
Mel LisauMel Lisau 

How can I populate required fields to creata test for SObjects ?

If i need to creat an Order , I need to first creat an Account and then assign the AccountId to the Order, like the following
 Account a = new Account();
 a.Name = 'Test';
 insert a;    

 Order order = new Order(     
        AccountId = a.Id,
        Status='Draft',
        EffectiveDate = Date.today());
 insert order;

Is there a way to create an Order and the required fields will be populated including the AccountId that is linked to the Account?
Reason is that I am looking at dynamically crating tests for my SObjects.
So if i have a trigger on Order (after insert) , I want to create a test for this but do it through a dynamic templating that I can use for al SObjects .

Thanks
Best Answer chosen by Mel Lisau
Kumaresan.ManickamKumaresan.Manickam
You can try pulling the mandatory field schema information via Scheme.fieldDescribeResults of any object you want and dynamically iterate each fields of object and check if its mandatory in run time and check its field type and assign the dummy test values and do a dynamic dml. Do you have any questions and let me know if I understokd your question well.

All Answers

Mel LisauMel Lisau
I was looking to dynamically create the class . So i dont know if i need to creat Accounts or Contacts or whatever.
I basically read a list of objects and determine what i need to create so if i have something like:
[0]
     [0] "Product"
     [1] "Status"
     [2] "String"
     [3] null
[1]
    [0] "Product"
    [1] "Effective Date"
    [2] "String" 
    [3] null
[2]
    [0] "Product"
    [1] "AccountId"
    [2] "Guid"
    [3] "Account"
[3]
     [0] "Product"
     [1] "Name"
     [2] "String"
     [3] null
[4]
     [0] "Account"
     [1] "FirstName" 
     [2] "String"
     [3] null
[5]
     [0] "Account"
     [1] "LastName" <
     [2] "String"
     [3] null

I will need to ceate the following:

For Account:

@isTest
public class TestRTT_Account_I 
{
    public static testMethod void Test_Account_I()
    {    
        List<Account> genericList = new List<Account>();
        Account a = new Account(FirstName = 'TestFirstName', LastName = 'TestLastName');
        Account acc = new Account(FirstName = 'TestFirstName01', LastName = 'TestLastName01');
        genericList.add(a);
         genericList.add(acc);
         insert genericList;
    }
}

and the samer for product. 
Sol its dynamically creating the test ?
Kumaresan.ManickamKumaresan.Manickam
You can try pulling the mandatory field schema information via Scheme.fieldDescribeResults of any object you want and dynamically iterate each fields of object and check if its mandatory in run time and check its field type and assign the dummy test values and do a dynamic dml. Do you have any questions and let me know if I understokd your question well.
This was selected as the best answer
Mel LisauMel Lisau
Perfect .. thankyou. I was under the impression that there may be a generic test builder . But this will work .