You need to sign in to do that
Don't have an account?
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);
}
}
}
That test class is fine for your data model
All Answers
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/
@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;
}
below is the sample for your test class.
Test Class:
Thank You,
Hitesh Patel
SFDC Certified Developer & Administrator
My Blog:- http://mrjavascript.blogspot.in/
it showing " Error: unexpected token: '=' " at the line record.Current_Year_Rep__c='kalai test';
Try this
its showing the same error :(
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.
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.
That test class is fine for your data model
Hey thank you so much.
Its working now.
64% test coverage.
But can u just explian what was my mistake?.
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
Thank you :)
Hello asish,Can you please help me on below class.
https://developer.salesforce.com/forums/ForumsMain?id=9062I000000DKdMQAW