function readOnly(count){ }
Starting November 20, the site will be set to read-only. On December 4, 2023,
forum discussions will move to the Trailblazer Community.
+ Start a Discussion
sreekanth reddysreekanth reddy 

Test class for parent record

Hi All,

How to write test class for below trigger.

rigger UpdateAmount on Training_Enquiry_Details__c (after insert, after update) { 
  Map<ID, Lead> parentOpps = new Map<ID, Lead>(); 
  List<Id> listIds = new List<Id>();

  for (Training_Enquiry_Details__c childObj : Trigger.new) {
    listIds.add(childObj.Lead__c);
  }

  parentOpps = new Map<Id, Lead>([SELECT id, Opted_Batch__c,(SELECT ID, Batch_Selected__c FROM Training_Enquiry_Details__r) FROM Lead WHERE ID IN :listIds]);

  for (Training_Enquiry_Details__c quote: Trigger.new){
     Lead myParentOpp = parentOpps.get(quote.Lead__c);
     myParentOpp.Opted_Batch__c = quote.Batch_Selected__c;
  }

  update parentOpps.values();
}

Thanks
Sai
buggs sfdcbuggs sfdc
hi,
 
@isTest
private class TheTestClass {
public static testMethod void testocc(){
  Training_Enquiry_Details__c tc = new Training_Enquiry_Details__c();
  tc.field1 = ’test1’;
  tc.field2 = ’test2’;
insert tc;

Lead ld = new lead();
  ld.field1 = ’test1’;
  ld.field2 = ’test2’;
insert ld; 
  }

}

 
Rohit B ☁Rohit B ☁
Just create a lead record first and all dependent records which are required to create lead record.
Now create records for object Training_Enquiry_Details__c by populating value of lead record (which u created above).

It will work for you :)
Amit Chaudhary 8Amit Chaudhary 8
Please try below code
@isTest
private class UpdateAmountTest 
{
	public static testMethod void testocc()
	{
		Lead ld = new lead();
		ld.LastName = 'test1';
		ld.FirstName = 'test2';
		ld.Company = 'BlueWave';
        ld.Status = 'contacted';
		insert ld; 

		
		Training_Enquiry_Details__c tc = new Training_Enquiry_Details__c();
		tc.Lead__c = ld.id ;
		// add all required field
		insert tc;
		
	}
}