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
Shruthi GM 4Shruthi GM 4 

Please help me in the code coverage of the below trigger

Trigger

trigger Intervieee on Interviewer__c (after Update) {


list<Candidates__c> updatedcand=new list<Candidates__c>();
set<ID> ChangedInter = new set<ID>();


Candidates__c can=new Candidates__c();
for(Interviewer__c i:trigger.new){
Interviewer__c invSet=Trigger.oldMap.get(i.Id);
system.debug('123456'+invSet.Mobile_no__c);



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 =: invSet.Mobile_no__c];

if(invSet.Mobile_no__c==canList.Mobile_no__c){

canList.Name=i.CName__c;
canlist.Phone_Number__c=i.Phone_Number__c;
canlist.Qualification__c=i.Candidate_s_qualification__c;
canlist.Rating__c=i.Rating__c;
canlist.Review_comments__c=i.Review_Comments__c;
canlist.Result__c=i.Result__c;
canlist.Mobile_no__c=i.Mobile_no__c;
updatedcand.add(canList);
}
}
try{
system.debug('Before update');
update updatedcand;
system.debug('After update');
}
catch(Exception e){}


test class

@istest

public class Intervieeetest{

public static testmethod void testvalidate(){

test.starttest();


list<Candidates__c> candis=new list<Candidates__c>();

Candidates__c cand=new Candidates__c();
cand.Name='Testing';
cand.Qualification__c='BE';
cand.Review_comments__c='Keep it up';
cand.Rating__c='A';
cand.Result__c='Cleared';
cand.Mobile_no__c=0;

candis.add(cand);



insert candis;

Interviewer__c interview=new Interviewer__c ();
//interview.Name='Testing';
interview.Result__c='Pass';
insert interview;



test.stoptest();







}
}
sfdcMonkey.comsfdcMonkey.com
hi Shruthi GM
use below test class 
@istest

public class Intervieeetest{

public static testmethod void testvalidate(){
list<Candidates__c> candis=new list<Candidates__c>();

Candidates__c cand=new Candidates__c();
cand.Name='Testing';
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;

Interviewer__c interview = new Interviewer__c ();
    interview.Name='Testing';
    interview.Result__c='Pass';
    interview.Mobile_no__c = '0123456789';
    insert interview;
    
    interview.Name='Testingupdate';
    update interview;
}
}

you are not update interview object after insert it in your test class so your trigger (after update) is not fire 
let me inform if it helps you and mark you question solved if it helps you 
thanks 
Shruthi GM 4Shruthi GM 4
I am geting this error:- System.DmlException: Update failed. First exception on row 0 with id a0C2800000NzJbhEAF; first error: CANNOT_INSERT_UPDATE_ACTIVATE_ENTITY, Intervieee: execution of AfterUpdate

caused by: System.QueryException: List has no rows for assignment to SObject

Trigger.Intervieee: line 15, column 1: []
sfdcMonkey.comsfdcMonkey.com
hi
make sure your
interview.Mobile_no__c = '0123456789';
and
cand.Mobile_no__c= '0123456789';
both are same
thanks
Shruthi GM 4Shruthi GM 4
Hi,again am 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>: []
sfdcMonkey.comsfdcMonkey.com
do you have any trigger or validation rule for duplicate on Interviewer__c or Candidates__c ?
Shruthi GM 4Shruthi GM 4
This trigger is present:-


trigger candi on Candidates__c (after Insert) {

 if(trigger.isinsert){

 List<Interviewer__c> inns = new List<Interviewer__c>();
  Interviewer__c i=new Interviewer__c();
  
 for(Candidates__c c:Trigger.new){
 
  i.CName__c=c.Name;
  i.Phone_Number__c=c.Phone_Number__c;
  i.Candidate_s_qualification__c=c.Qualification__c;
  i.Mobile_no__c=c.Mobile_no__c;
  inns.add(i);
 }
 
 try{
   insert inns; 
}
 catch(Exception e){
 system.debug(e);
 }
 }
}
sfdcMonkey.comsfdcMonkey.com
no this trigger not affect to test class
and below test class give me 94% covager in my org with same trigger
@istest

public class Intervieeetest{

public static testmethod void testvalidate(){
list<Candidates__c> candis=new list<Candidates__c>();

   Candidates__c cand=new Candidates__c();
   cand.Name='Testing';
   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;

Interviewer__c interview = new Interviewer__c ();
    interview.Name='Testing';
    interview.Result__c='Pass';
    interview.Mobile_no__c = '0123456789';
    insert interview;
    
    interview.Name='Testingupdate';
    update interview;
}
}
User-added image
 
Amit Chaudhary 8Amit Chaudhary 8
Hi Shruthi GM 4,

I found below issue in your trigger. Please make your trigger as bulky.
1) SOQL inside for loop

Please try below code
trigger Intervieee on Interviewer__c (after Update) 
{

	Set<String> setPhoneNumber = new Set<String>();
	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<String ,Candidates__c> mapPhoneWiseCand = new Map<String ,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 canList = mapPhoneWiseCand.get(invSet.Mobile_no__c);
					
					canList.Name=i.CName__c;
					canlist.Phone_Number__c=i.Phone_Number__c;
					canlist.Qualification__c=i.Candidate_s_qualification__c;
					canlist.Rating__c=i.Rating__c;
					canlist.Review_comments__c=i.Review_Comments__c;
					canlist.Result__c=i.Result__c;
					canlist.Mobile_no__c=i.Mobile_no__c;
					
					updatedcand.add(canList);
				}
			}
		}
		if(updatedcand.size() > 0 )
		{
			try{
				system.debug('Before update');
				update updatedcand;
				system.debug('After update');
			}
			catch(Exception e){}
			
		}	
		
	}	
}


Your test class should be like below
@istest
public class Intervieeetest
{
	public static testmethod void testvalidate()
	{
		list<Candidates__c> candis=new list<Candidates__c>();

		Candidates__c cand=new Candidates__c();
		cand.Name='Testing';
		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;

		Interviewer__c interview = new Interviewer__c ();
		interview.Name='Testing';
		interview.Result__c='Pass';
		interview.Mobile_no__c = '0123456789';
		insert interview;

		Test.StartTest(); 
		
			interview.Mobile_no__c = '0123456799';
			update interview;
			
		Test.StopTest();
		
	}

}
Let us know if this will help you
 
Shruthi GM 4Shruthi GM 4
Hi Amit,
After implimentation of your code,am getting the following 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>: [].
Please help.
Amit Chaudhary 8Amit Chaudhary 8
Can you please share the screen shot (with line number) of error and when you are getting this issue ?
Shruthi GM 4Shruthi GM 4
Hi,
User-added image