You need to sign in to do that
Don't have an account?
Shruthi GM 4
hi,how to write test class for the below trigger:-
trigger Intervieee on Interviewer__c (after Update)
{
Set<Decimal> setPhoneNumber = new Set<Decimal>();
for(Interviewer__c i:trigger.new)
{
if(i.Mobile_no__c != null)
{
// As per your current logic you are checking old value not new
Interviewer__c invSet=Trigger.oldMap.get(i.Id);
setPhoneNumber.add(invSet.Mobile_no__c);
}
}
if( setPhoneNumber.size() > 0 )
{
List<Candidates__c> canList = [select Name,Phone_Number__c,Qualification__c,Rating__c,Review_comments__c,Result__c,Mobile_no__c
from Candidates__c where Mobile_no__c = :setPhoneNumber ];
Map<Decimal ,Candidates__c> mapPhoneWiseCand = new Map<Decimal ,Candidates__c>();
for( Candidates__c cand : canList )
{
mapPhoneWiseCand.put( cand.Mobile_no__c , cand );
}
list<Candidates__c> updatedcand=new list<Candidates__c>();
for(Interviewer__c i:trigger.new)
{
if(i.Mobile_no__c != null)
{
Interviewer__c invSet=Trigger.oldMap.get(i.Id);
if( mapPhoneWiseCand.containsKey( invSet.Mobile_no__c ) )
{
Candidates__c canList1 = mapPhoneWiseCand.get(invSet.Mobile_no__c);
canList1.Name=i.CName__c;
canlist1.Phone_Number__c=i.Phone_Number__c;
canlist1.Qualification__c=i.Candidate_s_qualification__c;
canlist1.Rating__c=i.Rating__c;
canlist1.Review_comments__c=i.Review_Comments__c;
canlist1.Result__c=i.Result__c;
canlist1.Mobile_no__c=i.Mobile_no__c;
updatedcand.add(canList1);
}
}
}
if(updatedcand.size() > 0 )
{
try{
system.debug('Before update');
update updatedcand;
system.debug('After update');
}
catch(Exception e){}
}
}
}
Please help.
{
Set<Decimal> setPhoneNumber = new Set<Decimal>();
for(Interviewer__c i:trigger.new)
{
if(i.Mobile_no__c != null)
{
// As per your current logic you are checking old value not new
Interviewer__c invSet=Trigger.oldMap.get(i.Id);
setPhoneNumber.add(invSet.Mobile_no__c);
}
}
if( setPhoneNumber.size() > 0 )
{
List<Candidates__c> canList = [select Name,Phone_Number__c,Qualification__c,Rating__c,Review_comments__c,Result__c,Mobile_no__c
from Candidates__c where Mobile_no__c = :setPhoneNumber ];
Map<Decimal ,Candidates__c> mapPhoneWiseCand = new Map<Decimal ,Candidates__c>();
for( Candidates__c cand : canList )
{
mapPhoneWiseCand.put( cand.Mobile_no__c , cand );
}
list<Candidates__c> updatedcand=new list<Candidates__c>();
for(Interviewer__c i:trigger.new)
{
if(i.Mobile_no__c != null)
{
Interviewer__c invSet=Trigger.oldMap.get(i.Id);
if( mapPhoneWiseCand.containsKey( invSet.Mobile_no__c ) )
{
Candidates__c canList1 = mapPhoneWiseCand.get(invSet.Mobile_no__c);
canList1.Name=i.CName__c;
canlist1.Phone_Number__c=i.Phone_Number__c;
canlist1.Qualification__c=i.Candidate_s_qualification__c;
canlist1.Rating__c=i.Rating__c;
canlist1.Review_comments__c=i.Review_Comments__c;
canlist1.Result__c=i.Result__c;
canlist1.Mobile_no__c=i.Mobile_no__c;
updatedcand.add(canList1);
}
}
}
if(updatedcand.size() > 0 )
{
try{
system.debug('Before update');
update updatedcand;
system.debug('After update');
}
catch(Exception e){}
}
}
}
Please help.
Please follow below salesforce Best Practice for Test Classes always
Regards,
Soundar.
Hi,
insert the Candidate object record first,
then Insert the interviewer record
then update the Mobile_no__c of the interview records
Note: the Mobile_no__c of the candidate should match the Mobile_no__c of interviewer record when first entered.
But when updating the interviewer, u just need to update Mobile_no__c in already inserted interviewer records and your trigger will be covered
this is how I have written the test class:-
@istest
public class Intervieeetest
{
public static testmethod void testvalidate()
{
list<Candidates__c> candis=new list<Candidates__c>();
//insertion of Candidate record
Candidates__c cand=new Candidates__c();
cand.Name='shruthi';
cand.Qualification__c='BE';
cand.Review_comments__c='Keep it up';
cand.Rating__c = 'A';
cand.Result__c='Cleared';
cand.Mobile_no__c= 0123456789;
candis.add(cand);
insert candis;
//insersion of Interview record
Interviewer__c interview = new Interviewer__c ();
interview.Name='Testing';
interview.Result__c='Pass';
interview.Mobile_no__c = 0123456789;
interview.CName__c='preethi';
insert interview;
Test.StartTest();
interview.Name='Testing';
interview.CName__c='preethi';
interview.Mobile_no__c = 0231456987;
update interview;
Test.StopTest();
}
}
But facing this error!!!!!
System.DmlException: Insert failed. First exception on row 0; first error: DUPLICATE_VALUE, duplicate value found: <unknown> duplicates value on record with id: <unknown>: []
The error message isnt sufficiant to trace the exact error
try by just updating the mobile no
comment these lines
interview.Name='Testing';
interview.CName__c='preethi';
and kindly let me know the line number from where the error is generated.
Do report the code in the line please
try taking dummy values