Discussions
- General Development
- Schema Development
- Apex Code Development
- Visualforce Development
- Formulas & Validation Rules
- Security
- Mobile
- Force.com Sites & Site.com
- Chatter Development
- Java Development
- .NET Development
- Perl, PHP, Python & Ruby
- Desktop Integration
- APIs and Integrations
- Visual Workflow
- Apple, Mac and OS X
- VB and Office Development
- AppExchange Directory & Packaging
- Salesforce Labs & Open Source Projects
- Other Salesforce Applications
- Jobs Board
- Force.com Discussion Boards
- :
- Developer Boards for Force.com and Database.com
- :
- General Development
- :
- trigger apex test - Auto create record
- Subscribe to RSS Feed
- Mark Topic as New
- Mark Topic as Read
- Float this Topic to the Top
- Bookmark
- Subscribe
- Printer Friendly Page
trigger apex test - Auto create record
- Mark as New
- Bookmark
- Subscribe
- Subscribe to RSS Feed
- Highlight
- Email to a Friend
- Report Inappropriate Content
01-04-2013 04:42 AM
How do we write a test apex for a trigger that creates a child record when a parent record is created?
trigger AutoCreateInterviewer on Position__c (after insert) {
List<Interviewer__c> interviewers = new List<Interviewer__c>();
//For each position processed by the trigger, add a new
//interviewer record for the specified hiring manager.
//Note that Trigger.New is a list of all the new positions
//that are being created.
for (Position__c newPosition: Trigger.New) {
if (newPosition.Hiring_Manager__c != null) {
interviewers.add(new Interviewer__c(
Name = '1',
Position__c = newPosition.Id,
Employee__c = newPosition.Hiring_Manager__c,
Role__c = 'Managerial'));
}
}
insert interviewers;
}
Re: trigger apex test - Auto create record
- Mark as New
- Bookmark
- Subscribe
- Subscribe to RSS Feed
- Highlight
- Email to a Friend
- Report Inappropriate Content
01-04-2013 05:55 AM
Hi
Try this code:-
@isTest
private class MYTestClass {
static testMethod void AutoCreateInterviewer() {
Interviewer__c i=new Interviewer__c();
i.Name='123';
//put all your mandatory fields.
Insert i;
}
}
Anil.B
Re: trigger apex test - Auto create record
- Mark as New
- Bookmark
- Subscribe
- Subscribe to RSS Feed
- Highlight
- Email to a Friend
- Report Inappropriate Content
01-04-2013 06:33 AM
thank you but unfortunately running the test fails.
Re: trigger apex test - Auto create record
- Mark as New
- Bookmark
- Subscribe
- Subscribe to RSS Feed
- Highlight
- Email to a Friend
- Report Inappropriate Content
01-04-2013 08:29 AM
The test coverage does not include this part
interviewers.add(new Interviewer__c(
How can I test it?
Re: trigger apex test - Auto create record
- Mark as New
- Bookmark
- Subscribe
- Subscribe to RSS Feed
- Highlight
- Email to a Friend
- Report Inappropriate Content
01-04-2013 09:22 AM
try this:
@isTest
private class MYTestClass {
static testMethod void AutoCreateInterviewer() {
//Set up the test data
Profile p = [SELECT Id FROM Profile WHERE Name='Standard User'];
User u = new User(Alias = 'standt', Email='standarduser@testorg.com',
EmailEncodingKey='UTF-8', LastName='Testing', LanguageLocaleKey='en_US',
LocaleSidKey='en_US', ProfileId = p.Id,
TimeZoneSidKey='America/Los_Angeles', UserName='noonespecial@testorg.com');
insert u;
//create a new position with a hiring manager
System.Debug('Create new position - validate single insert');
Position__c newposition = new Position__c();
newposition.Name = 'Position1';
newposition.Hiring_Manager__c=u.Id;
//call the trigger
insert newposition;
System.Debug('New Position Hiring Manager is ' + u.id);
Interviewer__c i =[SELECT Position__c, Employee__c, Role__c FROM Interviewer__c
WHERE CreatedDate = TODAY and Role__c !=NULL
LIMIT 1];
//Validate positive test case
System.debug('Role is ' + i.Role__c);
System.assert(i.Role__c=='Managerial');
}
}
Re: trigger apex test - Auto create record
- Mark as New
- Bookmark
- Subscribe
- Subscribe to RSS Feed
- Highlight
- Email to a Friend
- Report Inappropriate Content
01-04-2013 09:54 AM
Hi
@isTest
private class MYTestClass {
static testMethod void AutoCreateInterviewer() {
Position__c newposition = new Position__c();
newposition.Name = 'Position1';
newposition.Hiring_Manager__c='rest';
insert newposition;
list<Interviewer__c >ld =new list<Interviewer__c>{
Interviewer__c i=new Interviewer__c();
i.Name='123';
//put all your mandatory fields.
ld.add(i);
}
insert ld;
}
}
Anil.B
Re: trigger apex test - Auto create record
- Mark as New
- Bookmark
- Subscribe
- Subscribe to RSS Feed
- Highlight
- Email to a Friend
- Report Inappropriate Content
01-04-2013 10:04 AM
Thank you guys.
I will see if this works
Re: trigger apex test - Auto create record
- Mark as New
- Bookmark
- Subscribe
- Subscribe to RSS Feed
- Highlight
- Email to a Friend
- Report Inappropriate Content
01-07-2013 01:39 AM
I was using the initial trigger to create this one :
trigger AutoCreateReview on Phases__c (before insert, before update) {
List< File__c > F=new List< File__c >();
List< Review__c > R =new List< Review__c >();
for (Phases__c newP: Trigger.New) {
if (newP.Phase_Number__c == 'I' && newP.State_Review__c == 'In progress' ) {
R.add(new Review__c(
Phases__c = newP.Id,
File__c = newP. File__c,
Review_Title__c = 'BP',
Description__c = 'BPD',
Validation__c = 'BP Approved.'));
}
if (newP.Phase_Number__c == 'I'&& newP.State_Review__c == 'In progress') {
R.add(new Review__c(
Phases__c = newP.Id,
File__c = newP. File__c,
Review_Title__c = 'Diagnosis',
Description__c = 'DiagnosisD',
Validation__c = 'Diagnosis Approved'));
}
if (newP.Phase_Number__c == 'I'&& newP.State_Review__c == 'In progress'&& (newP File__r.Capital__c<100000)) {
R.add(new Review__c(
Phases__c = newP.Id,
File__c = newP. File__c,
Review_Title__c = 'Certificate',
Description__c = 'Document sent',
Validation__c = 'Certificate approved'));
}
}
insert R;
}
The goal is to create a number of reviews depending on which phase we are in. Reviews and Phases are linked by a master-detail relationship, Phases being the parent. These two are both children to the master grandfather object File__c.
My test is at 66% coverage. I used the same test you guys provided. It still does not cover the adding part. What do you recommend?
Thank you for your help.
Re: trigger apex test - Auto create record
- Mark as New
- Bookmark
- Subscribe
- Subscribe to RSS Feed
- Highlight
- Email to a Friend
- Report Inappropriate Content
01-07-2013 07:04 PM
Hey salesforcee, your trigger looks like it is inserting two records here: your first two if statements are the same. Is that what you are meaning to do?
Re: trigger apex test - Auto create record
- Mark as New
- Bookmark
- Subscribe
- Subscribe to RSS Feed
- Highlight
- Email to a Friend
- Report Inappropriate Content
01-08-2013 12:55 AM
There are four phases and for each phase there is a number of records to create. Would that be a problem?

