3

I have a basic trigger that creates a child record when a parent record is created, and it works perfectly in the Sandbox. But I can't get the Test Class syntax right in order to push to production.

Here's the trigger:

trigger CreateChild on Parent__c (after insert) {
    List<Child__c> Childs = new List<Child__c>();

    for(Parent__c a : trigger.new)
    {
       Child__c Child = new Child__c ();
       Child.Parent__c = a.id;
       Child.Name = 'testName'; 

       Childs.add(Child);      
    }

    insert Childs;
}

And here's test class so far:

@isTest 
private class CreateChildwithParent {

    static testMethod void validateChildCreated() {
       Parents__c p = new Parents__c(Name='Test Parent');
       System.debug('Children created with parent: ' + p.child.Name__c);

       // Insert parent
       insert p;

       // Retrieve the new child
       e = [SELECT Child.Name__r FROM Parents__c WHERE Id =:p.Id];
       System.debug('Children saved on parent: ' + p.child.Name__c);

       // Test that the trigger correctly created the children
       System.assertEquals('Test Child', p.child.Name__r);
    }
}
2
  • 1
    The field names in your SOQL queries look wrong. You need something like Select Child__c.Name__c From Parents__c ... and System.assertEquals('testName', p.Child__r.Name__c); May 15, 2015 at 2:22
  • 2
    Also, can you be more specific about the syntax problem you are having with the test class. What is the error message you are getting when saving the test class? May 15, 2015 at 2:24

3 Answers 3

1

When querying for the newly-created Child__c records, you need to query that object, not the Parents__c object:

list<Child__c> resultChilds = [select Id, Name__c, Parent__c from Child__c where Parent__c = :p.Id];

system.assertEquals(1, resultChilds.size());
system.assertEquals('Test Child', resultChilds[0].Name__c);
5
  • I've tried to use both of your suggestions but no dice yet...here it is so far in 2 parts, error at bottom: @isTest private class CreateChildwithParent { static testMethod void validateChildCreated() { Parent__c p = new Parent__c(Name='Test Parent'); System.debug('Childs created with parent: ' + p.Child.Name__c); // Insert parent insert p; May 15, 2015 at 13:24
  • // Retrieve the new Child p = [SELECT Name FROM Parent__c WHERE Id =:p.Id]; System.debug('Childs saved on Parent: ' + p.Child.Name__c); // Test that the trigger correctly created the Childs system.assertEquals(1, resultChild.size()); system.assertEquals('Test Child', resultChilds[0].Name__c); } } error at 2nd system assert: Error Error: Compile Error: Variable does not exist: resultFights at line 17 column 39 May 15, 2015 at 13:25
  • Is this solved?
    – navD87
    May 15, 2015 at 15:45
  • No sir, I posted the updated test class above and resulting error code. There are probably still multiple problems with syntax or relationships in my class May 15, 2015 at 15:50
  • 1
    Can you post your current code and error message as an update on your question?
    – navD87
    May 15, 2015 at 21:00
0

Have you considered using the Process Builder instead of a trigger to generate your child records.

It is pretty simple to setup the creation of a child record when a parent record is created.

Process Builder Example

  1. Set it to run when the Parent Record is created.
  2. Set your criteria of when it should run or have it always run.
  3. Have it create the child record setting the related parent field to the Id value from the parent.

Create Record

Select Parent Record Id

8
  • Thanks Jenny! You know, I have tried that a few times, the top row where it says Location and the Id I could never map correctly, whats the syntax?[Parent__c].id? May 17, 2015 at 14:33
  • @DAVIDJANKE It defaults to a pencil, if you click on the pencil that will switch it to value, then you select the field from the parent in a dropdown. It should be Record Id.
    – Jenny B
    May 17, 2015 at 14:35
  • It sounds so simple but for some reason it simply would not work when I was trying it, I'll try again...you build the process on the parent right? May 17, 2015 at 14:36
  • @DAVIDJANKE Yea, I'll add a screen shot of the value dropdown.
    – Jenny B
    May 17, 2015 at 14:37
  • 1
    Hi Jenny, I was thanks for checking! I had a custom event object with same name as the standard Event object, I was just on wrong object...a rookie oversight! Jun 10, 2015 at 15:13
-1

In your test class, it looks like you haven't inserted test child.

In you trigger you attach child to parent as in:

Child.Parent__c = a.id;

So in your test class, after inserting parent, you need to

   Child__c Child = new Child__c ();
   Child.Parent__c = p.id;
   Child.Name = 'Test Child'; 

  insert child;  

Then execute SOQL and assert results.

This was all wrong, your trigger should create child. Funny how I missed this. The problem is with field names.

Also the line (and further down the code) System.debug('Children created with parent: ' + p.child.Name__c); shows that you are using child field name without any underscore. It looks like it's a relationship field, so it should have __r at the end of it.

Your SOQL should look more like

e = [SELECT Child__r.Name__c FROM Parents__c WHERE Id =:p.Id];

2
  • You do not insert the child in the test class. The test class is testing the trigger which is supposed to insert the child. So you would query for the child in the test class......
    – Eric
    May 17, 2015 at 21:55
  • That... Makes sense.
    – dzh
    May 17, 2015 at 22:01

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .