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
Divya Samudrala 4Divya Samudrala 4 

Test class for after insert event


Hi Everyone,
I need test class for my trigger which copies details from obj1(Loaner Request Form) to obj2(Equipment Request) after insert on obj1(Loaner Request form).Kinldy help me on this one.Thanks in advance.

trigger InsertToEquipmentRequest on Loaner_Requests_Form__c (after insert) {
List <ELTON__Equipment_Request__c> EQRINSERT = new List <ELTON__Equipment_Request__c>();
Map<Id,List<String>> mapEquipmentTypes = new Map<Id,List<String>>();
Set<String> setType = new Set<String>();
Map<String,Id> mapEquipmentType = new Map<String,Id>();
for (Loaner_Requests_Form__c LRF:Trigger.new)
{
    List<String> lstEquipmentTypes = LRF.Equipment_Type_Multi__c.split(';');
    mapEquipmentTypes.put(LRF.id,lstEquipmentTypes);
    for(String s : lstEquipmentTypes){
       setType.add(s); 
    }
    //setType.add(LRF.Equipment_Type__c);
}

System.debug('Equipment TYpe = ' + setType);

If(setType != Null && !setType.isEmpty())
{
    for(ELTON__Equipment_Type__c e : [Select Id,name from ELTON__Equipment_Type__c where Name In :setType])
    {
        mapEquipmentType.put(e.name,e.id);
    }
    
}
System.debug('mapEquipmentType = ' +mapEquipmentType);

for (Loaner_Requests_Form__c LRF:Trigger.new)
 {
     for(String s :mapEquipmentTypes.get(LRF.Id)) {
        System.debug('Equipment TYpe 1 = ' +LRF.Equipment_Type__c);
        ELTON__Equipment_Request__c EQR = new ELTON__Equipment_Request__c ();
        EQR.Full_Name__c= LRF.Full_Name__c;
        EQR.Department__c= LRF.Department__c;
        EQR.Office_Location__c= LRF.Office_Location__c;
        EQR.Mission_Name__c= LRF.Mission_Name__c;
        EQR.Location__c= LRF.Travel_Location__c;
        EQR.ELTON__Request_Start_Date__c= LRF.Travel_Start_Date__c;
        EQR.ELTON__Request_Return_Date__c= LRF.Travel_End_Date__c;
        EQR.Request_Sent_By__c= LRF.Request_Sent_By__c;
        EQR.Requesting_for__c= LRF.Requesting_for__c;
        EQR.ELTON__User__c= LRF.Requesting_for__c;
        EQR.Requested_User__c= LRF.Loaner_Requester_Name__c;

        EQR.Pick_up_Location__c= LRF.Pick__c;
        EQR.Name__c= LRF.Name__c;
        EQR.Email_Address__c= LRF.Email_Address__c;
        EQR.Date__c= LRF.Requested_Date__c;
        EQR.Phone__c= LRF.Phone__c;
        EQR.ELTON__Request_Note__c=LRF.Note__c;        
        
        EQR.Equipment_Type_Multi__c =LRF.Equipment_Type_Multi__c;
        //EQR.ELTON__Equipment_Type__c=mapEquipmentType.get(LRF.Equipment_Type__c);
        EQR.ELTON__Equipment_Type__c=mapEquipmentType.get(s);
        EQRINSERT.add(EQR);
     }
     
}
    insert EQRINSERT ;
 
}
Best Answer chosen by Divya Samudrala 4
GauravGargGauravGarg
Hi Divya,

Please try below code:
@isTest
(seeAllData = true)
Public Class test_InsertToEquipmentRequest{
	static testMethod void createData(){
	Loaner_Requests_Form__c LRF = new Loaner_Requests_Form__c;
	LRF.Equipment_Type_Multi__c = 'abc,xyz';
	LRF.Equipment_Type__c = 'testclass';
	LRF.Full_Name__c = 'Test Class Coverage';
	LRF.Department__c = 'createTestData';
	LRF.Office_Location__c = 'Salesforce';
	LRF.Mission_Name__c = 'Test class';
	LRF.Travel_Location__c = 'test.salesforce.com';
	LRF.Travel_Start_Date__c = Date.Today();
	LRF.Travel_End_Date__c = Date.Today();
	LRF.Request_Sent_By__c = UserInfo.getUserId();
	LRF.Requesting_for__c = UserInfo.getUserId();
	LRF.Loaner_Requester_Name__c = UserInfo.getUserId();
	LRF.Pick__c = 'test';
	LRF.Name__c = 'Test coode coverage';
	LRF.Email_Address__c = 'test@salesforce.com';
	LRF.Requested_Date__c = Date.Today();
	LRF.Phone__c = 099999999
	LRF.Note__c = 'Test class coverage';
	insert LRF;
	
	ELTON__Equipment_Type__c eqt = new ELTON__Equipment_Type__c();
	eqt.name = 'abc,xyz';
	insert eqt;
	
	
	}
}
Let me know if you still face the issues, or contact me via email: gauravgarg.nmim@gmail.com, skype: gaurav62990

Thanks,
Gaurav
 

All Answers

GauravGargGauravGarg
Hi Divya,

Please try below code:
@isTest
(seeAllData = true)
Public Class test_InsertToEquipmentRequest{
	static testMethod void createData(){
	Loaner_Requests_Form__c LRF = new Loaner_Requests_Form__c;
	LRF.Equipment_Type_Multi__c = 'abc,xyz';
	LRF.Equipment_Type__c = 'testclass';
	LRF.Full_Name__c = 'Test Class Coverage';
	LRF.Department__c = 'createTestData';
	LRF.Office_Location__c = 'Salesforce';
	LRF.Mission_Name__c = 'Test class';
	LRF.Travel_Location__c = 'test.salesforce.com';
	LRF.Travel_Start_Date__c = Date.Today();
	LRF.Travel_End_Date__c = Date.Today();
	LRF.Request_Sent_By__c = UserInfo.getUserId();
	LRF.Requesting_for__c = UserInfo.getUserId();
	LRF.Loaner_Requester_Name__c = UserInfo.getUserId();
	LRF.Pick__c = 'test';
	LRF.Name__c = 'Test coode coverage';
	LRF.Email_Address__c = 'test@salesforce.com';
	LRF.Requested_Date__c = Date.Today();
	LRF.Phone__c = 099999999
	LRF.Note__c = 'Test class coverage';
	insert LRF;
	
	ELTON__Equipment_Type__c eqt = new ELTON__Equipment_Type__c();
	eqt.name = 'abc,xyz';
	insert eqt;
	
	
	}
}
Let me know if you still face the issues, or contact me via email: gauravgarg.nmim@gmail.com, skype: gaurav62990

Thanks,
Gaurav
 
This was selected as the best answer
Divya Samudrala 4Divya Samudrala 4
Hi Gaurav,
Thank you so much..
GauravGargGauravGarg
Welcome Divya :)