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
newbiewithapexnewbiewithapex 

how to write test class for method that fetches recommendations?

Below method fetch recommendations and I am not sure how to write test class for this method. Can someone please help me with this? 
@AuraEnabled
public static string getRecommendations(string recId) { 
	string errorMessage;
	Recommendation__c rec = new Recommendation__c();
	rec = [select Id, Name, Account__c, 
		   Account__r.Name, Account__r.RecordTypeId, 
		   Sub_Type__c, Opportunity__c, Description__c, 
		   Record_Type_Name__c 
		   from Recommendation__c 
		   where Id = :recId limit 1];
	if (String.IsNOTBlank(rec.Opportunity__c)) {
		AuraHandledException e = new AuraHandledException(CS_UtilConstants__c.getValues('OPPTY_EXISTS_RECOMMENDATIONS_INFO').Value__c);
		e.setMessage(CS_UtilConstants__c.getValues('OPPTY_EXISTS_RECOMMENDATIONS_INFO').Value__c);
		throw e;
	}
	else if (rec.Record_Type_Name__c == CS_UtilConstants__c.getInstance('CONVERSION').Value__c && (rec.Description__c == Null || rec.Description__c == '') && (rec.Sub_Type__c == Null || rec.Sub_Type__c == '')) {
		AuraHandledException e = new AuraHandledException(CS_UtilConstants__c.getValues('CONVERSION ERROR MESSAGE').Value__c);
		e.setMessage(CS_UtilConstants__c.getValues('CONVERSION ERROR MESSAGE').Value__c);
		throw e;
	}
	return null;
}

 
Raj VakatiRaj Vakati
Use this code
 
@isTest
private class TestRecommendations{

static testMethod void testprofile(){
	
CS_UtilConstants__c utils = new CS_UtilConstants__c() ;
utils.name = 'CONVERSION ERROR MESSAGE' ; 
utils.value__c = 'CONVERSION ERROR MESSAGE' ; 
insert utils ; 

CS_UtilConstants__c utils1 = new CS_UtilConstants__c() ;
utils1.name = 'CONVERSION' ; 
utils1.value__c = 'CONVERSION ERROR MESSAGE' ; 
insert utils1 ; 

	
	Account a = new Account();
	a.Name = 'Test';
	a.Industry = 'Retail';
	insert a;
	
	
	Opportunity o = new Opportunity();
	o.name = 'Test';
	o.AccountId = a.Id;
	o.StageName = 'Closed Won';
	o.CloseDate = Date.today();
	o.Type = 'New Customers';
	
	insert o;
	
	Recommendation__c  rec = new Recommendation__c() ;
	rec.Name ='Demo'
	rec.Account__c =a.Id ;
	rec.Opportunity__c = o.Id ;
	insert rec ; 
		Try{
		YOURCLASSNAME.getRecommendations(rec.id);
		}catch(Exception e){
			
		}
		
			
	Recommendation__c  rec1 = new Recommendation__c() ;
	rec1.Name ='Demo'
	rec1.Account__c =a.Id ;
 
	insert rec1 ; 
		Try{
		YOURCLASSNAME.getRecommendations(rec1.id);
		}catch(Exception e){
			
		}
	
}

}

 
newbiewithapexnewbiewithapex
I tried your code and I am getting "Field is not writeable: Recommendation__c.Name" error at 2 places(rec.Name ='Demo' and rec1.Name = 'Demo'). Also if I comment those 2 lines, it doesn't cover the els if loop
Raj VakatiRaj Vakati
You need to set the record type name for else
 
Recommendation__c  rec1 = new Recommendation__c() ;
	 
	rec1.Account__c =a.Id ;
 rec1.Record_Type_Name__c ='CONVERSION';
 rec1.Description__c =null ; 
 rec1.Sub_Type__c =null ; 
	insert rec1 ; 
		Try{
		YOURCLASSNAME.getRecommendations(rec1.id);
		}catch(Exception e){
			
		}

Complete code
 
@isTest
private class TestRecommendations{

static testMethod void testprofile(){
	
CS_UtilConstants__c utils = new CS_UtilConstants__c() ;
utils.name = 'CONVERSION ERROR MESSAGE' ; 
utils.value__c = 'CONVERSION ERROR MESSAGE' ; 
insert utils ; 

CS_UtilConstants__c utils1 = new CS_UtilConstants__c() ;
utils1.name = 'CONVERSION' ; 
utils1.value__c = 'CONVERSION ERROR MESSAGE' ; 
insert utils1 ; 

	
	Account a = new Account();
	a.Name = 'Test';
	a.Industry = 'Retail';
	insert a;
	
	
	Opportunity o = new Opportunity();
	o.name = 'Test';
	o.AccountId = a.Id;
	o.StageName = 'Closed Won';
	o.CloseDate = Date.today();
	o.Type = 'New Customers';
	
	insert o;
		Recommendation__c  rec = new Recommendation__c() ;
	rec.Account__c =a.Id ;
	rec.Opportunity__c = o.Id ;
	insert rec ; 
		Try{
		YOURCLASSNAME.getRecommendations(rec.id);
		}catch(Exception e){
			
		}
		
			
	Recommendation__c  rec1 = new Recommendation__c() ;
	 
	rec1.Account__c =a.Id ;
 rec1.Record_Type_Name__c ='CONVERSION';
 rec1.Description__c =null ; 
 rec1.Sub_Type__c =null ; 
	insert rec1 ; 
		Try{
		YOURCLASSNAME.getRecommendations(rec1.id);
		}catch(Exception e){
			
		}
	
}

}

 
newbiewithapexnewbiewithapex
Still getting "Field is not writeable" error on line rec1.Record_Type_Name__c = 'CONVERSION';
Raj VakatiRaj Vakati
Use this
@isTest
private class TestRecommendations{

static testMethod void testprofile(){
	
CS_UtilConstants__c utils = new CS_UtilConstants__c() ;
utils.name = 'CONVERSION ERROR MESSAGE' ; 
utils.value__c = 'CONVERSION ERROR MESSAGE' ; 
insert utils ; 

CS_UtilConstants__c utils1 = new CS_UtilConstants__c() ;
utils1.name = 'CONVERSION' ; 
utils1.value__c = 'CONVERSION ERROR MESSAGE' ; 
insert utils1 ; 

	
	Account a = new Account();
	a.Name = 'Test';
	a.Industry = 'Retail';
	insert a;
	
	
	Opportunity o = new Opportunity();
	o.name = 'Test';
	o.AccountId = a.Id;
	o.StageName = 'Closed Won';
	o.CloseDate = Date.today();
	o.Type = 'New Customers';
	
	insert o;
		Recommendation__c  rec = new Recommendation__c() ;
	rec.Account__c =a.Id ;
	rec.Opportunity__c = o.Id ;
	insert rec ; 
		Try{
		YOURCLASSNAME.getRecommendations(rec.id);
		}catch(Exception e){
			
		}
		
			
	Recommendation__c  rec1 = new Recommendation__c() ;
	 
	rec1.Account__c =a.Id ;
 rec1.RecordtypeId =Schema.Sobjecttype.Recommendation__c.getRecordTypeInfosByName().get('CONVERSION').getRecordTypeId();
 rec1.Description__c =null ; 
 rec1.Sub_Type__c =null ; 
	insert rec1 ; 
		Try{
		YOURCLASSNAME.getRecommendations(rec1.id);
		}catch(Exception e){
			
		}
	
}

}

 
newbiewithapexnewbiewithapex
I am so sorry but now I am getting "Attempt to de-reference a null object" after trying your last code
Raj VakatiRaj Vakati
Check this line  and whihc line you are getting error 

 rec1.Account__c =a.Id ;
  rec1.RecordtypeId=Schema.Sobjecttype.Recommendation__c.getRecordTypeInfosByName().get('CONVERSION').getRecordTypeId();
 
And See what is the correct record type name