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
GeetuGeetu 

Apex test class - Recordtype

can somebody pls help me to write test class for below code in apex class
//Get RecordTypes name from Account Object
    @AuraEnabled
    public static string findRecordTypes(string objName){
        string returnString='';
        string queryString='Select id,name from RecordType where sobjectType =: objName  
       
        List<sobject> recordList= Database.query(queryString);
        List<RecordTypeWrapper> wrapperList=new List<RecordTypeWrapper>();
        for(sobject sb : recordList)  {
            RecordTypeWrapper rw=new RecordTypeWrapper();
            rw.recordTypeLabel=string.valueof(sb.get('name'));
            rw.recordTypeId=string.valueof(sb.get('id'));
            wrapperList.add(rw);
        }
        returnString= JSON.serialize(wrapperList);
       
        return returnString;
    }
 
 
Raj VakatiRaj Vakati
This will work 
 
@isTest 
private class AccControllerTest{
    
    
    static testMethod void testMethodRT(){
       
        
        YOURCLASSNAME.findRecordTypes('Account');
        
        
        
    }
    
}

 
GeetuGeetu
thanks Raj...and how can i insert a test record for Record type  in above test class, so my apex class requirement is to - find record type and then insert a account. 
 
Raj VakatiRaj Vakati
Record type you can't insert and you can query
Raj VakatiRaj Vakati
Id accRecor= Schema.SObjectType.Account.getRecordTypeInfosByName().get('recordtypename').getRecordTypeId();

Account a = new Account() ;
a.Name = 'Test ' ;
a.RecordTypeId =accRecor ;
insert a ; 

 
GeetuGeetu
ok Raj, i have been asked to create a test record first.. can u explain why we cannot create it
Raj VakatiRaj Vakati
Record types are avaible in test class with out insert and you need to insert the same record type with same name it will fail 
GeetuGeetu
thanks Raj. Can i please have your email address. i have few issues in writing my test class. Would be great if you can help me