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
ezhil_kezhil_k 

Please guide me to write a test class for the trigger..

trigger getmanager on Opp__c ( before update){
Set<Id> id= new Set<Id>();
Set<String> empid=new Set<String>();
Set<String> mngid=new Set<String>();
List<Opp__c> OppyLocation = new List<Opp__c>();

for (Opp__c opp:trigger.old){
  id.add(trigger.newmap.get(opp.id).Current_Year_Rep__c );
  system.debug('id:'+id);
  }
for (HR__c empnum: [select Employee_Number__c from HR__c where  id  in :id ]){
    empid.add(empnum.Employee_Number__c);
      }
      
    system.debug('id:'+id);
    system.debug('empid:'+empid);
 
 for (Contact con: [select Employee_Id__c,Reports_To_Employee_Id__c from contact where Employee_Id__c in:empid]){
    mngid.add(con.Reports_To_Employee_Id__c );
    }
    system.debug('mngid:'+mngid);
    
 for (HR__c empnu: [select Employee_Number__c,Name from HR__c where Employee_Number__c in: mngid]){

      for (Opp__c opp:trigger.new){
     opp.Cureent_Year_Rep_Manager__c=empnu.id; 
     system.debug('emp:'+ empnu.id );
     system.debug('emp:'+ empnu.Name );

     OppyLocation.add(opp);
     
      
         }
             }

  
  
      
      
}

Best Answer chosen by Admin (Salesforce Developers) 
asish1989asish1989

That test class is fine for your data model

@istest
Public Class  getmanagerTest {
public static testmethod void test() {
	Contact c = new Contact(Employee_Id__c='11110',Reports_To_Employee_Id__c='11111');
	insert c;
	
	List <HR__c> rec=new List<HR__c>();
		rec.add(new HR__c( Name='kalai',Employee_Number__c='11110'));
		rec.add(new HR__c(Name='Raj',Employee_Number__c='11111'));
	
	insert rec;
	
	Opp__c record = new Opp__c(Current_Year_Rep__c=rec[0].Id);
	
	insert record;
	
	record.Current_Year_Rep__c=rec[1].Id;  
	update record;
 }	
	
}

All Answers

hitesh90hitesh90

Hi ezhil,

 

For building a test class you have to insert dummy record of following object( HR__c, Opp__c, contact ) using code in your test class.

and lastly update the Opp__c recourd to call your trigger from test class.

 

Important :
Hit Kudos if this provides you with useful information and if this is what you where looking for then please mark it as a solution for other benefits.
 
Thank You,
Hitesh Patel
SFDC Certified Developer & Administrator
My Blog:- http://mrjavascript.blogspot.in/

ezhil_kezhil_k

@istest
Public Class  getmanagerTest {
public static testmethod void test() {
    Contact c = new Contact(Employee_Id__c='11110',Reports_To_Employee_Id__c='11111');
    insert c;
   List <HR__c> rec=new List<HR__c>();
      rec.add(new HR__c( Name='kalai',Employee_Number__c='11110'));
      rec.add(new HR__c(Name='Raj',Employee_Number__c='11111'));
           }
     Opp__c record = new Opp__c(Current_Year_Rep__c='kalai');
     record.Current_Year_Rep__c='kalai';  

     update record;
          
}

hitesh90hitesh90

below is the sample for your test class.

 

Test Class:

@istest
Public Class  getmanagerTest {
public static testmethod void test() {
	Contact c = new Contact(Employee_Id__c='11110',Reports_To_Employee_Id__c='11111');
	insert c;
	
	List <HR__c> rec=new List<HR__c>();
		rec.add(new HR__c( Name='kalai',Employee_Number__c='11110'));
		rec.add(new HR__c(Name='Raj',Employee_Number__c='11111'));
	}
	insert rec;
	
	Opp__c record = new Opp__c(Current_Year_Rep__c='kalai');
	record.Current_Year_Rep__c='kalai';  
	insert record;
	
	record.Current_Year_Rep__c='kalai test';  
	update record;
	
	record.Current_Year_Rep__c='kalai';  
	update record;
          
}

 

Thank You,
Hitesh Patel
SFDC Certified Developer & Administrator
My Blog:- http://mrjavascript.blogspot.in/

 

ezhil_kezhil_k

it showing " Error: unexpected token: '=' " at the line record.Current_Year_Rep__c='kalai test'; 

asish1989asish1989

Try this 

@istest
Public Class  getmanagerTest {
public static testmethod void test() {
	Contact c = new Contact(Employee_Id__c='11110',Reports_To_Employee_Id__c='11111');
	insert c;
	
	List <HR__c> rec=new List<HR__c>();
		rec.add(new HR__c( Name='kalai',Employee_Number__c='11110'));
		rec.add(new HR__c(Name='Raj',Employee_Number__c='11111'));
	
	insert rec;
	
	Opp__c record = new Opp__c(Current_Year_Rep__c=rec[0].Id);
	record.Current_Year_Rep__c='kalai';  
	insert record;
	
	record.Current_Year_Rep__c=rec[1].Id;  
	update record;
 }	
	
}

 

ezhil_kezhil_k

its showing the same error  :(

asish1989asish1989

Try this ,It will not show same error, If you again face same error then share your data model, I mean the object and relationship that you have used in this.

@istest
Public Class  getmanagerTest {
public static testmethod void test() {
	Contact c = new Contact(Employee_Id__c='11110',Reports_To_Employee_Id__c='11111');
	insert c;
	
	List <HR__c> rec=new List<HR__c>();
		rec.add(new HR__c( Name='kalai',Employee_Number__c='11110'));
		rec.add(new HR__c(Name='Raj',Employee_Number__c='11111'));
	
	insert rec;
	
	Opp__c record = new Opp__c(Current_Year_Rep__c=rec[0].Id);
	
	insert record;
	
	record.Current_Year_Rep__c=rec[1].Id;  
	update record;
 }	
	
}
ezhil_kezhil_k

My Data Model:

 

Object 1: HR(custom object)

fields: hrname,employee number,job code,job title,company,email,phone.

 

Object  2:Opportunity locationHR(custom object)

fields :name,current year rep(look up on hr),current year rep manager(look up on hr)

 

Oject 3: Contact (Std object)

additional custom fields : Employee Id,Reports to employee Id.

 

Requirement:

The value in current year rep is populated manually.As soon as the data is populated in that field,corresponding current year rep manager should be populated automatically.

 

Suggested method:

To get the current year manager, go to the Rep’s HR table and look at the Employee Number field on the HR table. Grab the employee Number and locate the matching contact record where Employee Number on the HR table = Employee Id on the Contact Table. Once the Contact is found, look at the Reports_To_Employee_Id field on the contact record, This is the employee id of their manager. Find a HR record where Employee Number = Reports_To_Employee_Id. Get the reference to the HR and populate in the Manager field on the Opporunity Location.

asish1989asish1989

That test class is fine for your data model

@istest
Public Class  getmanagerTest {
public static testmethod void test() {
	Contact c = new Contact(Employee_Id__c='11110',Reports_To_Employee_Id__c='11111');
	insert c;
	
	List <HR__c> rec=new List<HR__c>();
		rec.add(new HR__c( Name='kalai',Employee_Number__c='11110'));
		rec.add(new HR__c(Name='Raj',Employee_Number__c='11111'));
	
	insert rec;
	
	Opp__c record = new Opp__c(Current_Year_Rep__c=rec[0].Id);
	
	insert record;
	
	record.Current_Year_Rep__c=rec[1].Id;  
	update record;
 }	
	
}
This was selected as the best answer
ezhil_kezhil_k

Hey thank you so much.

Its working now.

64% test coverage.

But can u just explian what was my mistake?.

asish1989asish1989

record.Current_Year_Rep__c='kalai';

This wasour  mistake, This is a lookup field to Opp_c ,But we are giving text value , We should give like below.

In my previous post code was also correct, you might not be noticed correctly, Anyways error get solved that's fine , As you are happy now give kudos for this post also

Current_Year_Rep__c=rec[0].Id
ezhil_kezhil_k

Thank you :)

Akshay MhetreAkshay Mhetre

Hello asish,Can you please help me on below class.

https://developer.salesforce.com/forums/ForumsMain?id=9062I000000DKdMQAW