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
Mano sfdcMano sfdc 

Metadata Apex Class - Need Test Class

Best Answer chosen by Mano sfdc
Sampath SuranjiSampath Suranji
Hi,
Try something like below,
@isTest
public class MetadataFieldsTest {
    
    public static testmethod void testAll(){
        try
        {
            MetadataFields objM = new MetadataFields();
            objM.obj='contact';
            
            objM.getobj();
            objM.setobj('contact');
            List<SelectOption> objList = objM.getobjs();
            objM.fetchFields();
        }
        catch(Exception ex){}
        
    }
    
}

Regards
Sampath​

All Answers

Mano sfdcMano sfdc
Hi Experts,

I have created an Apex Class, some one please help me on creating Test Class. Thanks in Advance.


public class MetadataFields{ 

  public String obj;
  public List<String> objFields {get;set;}

  public MetadataFields() 
  { 
  }

  public String getobj()
  {
    return obj;
  }

  public void setobj(String obj)
  {
    this.obj = obj;
  } 

  public List<SelectOption> getobjs()
  {
    List<Schema.SObjectType> gd = Schema.getGlobalDescribe().Values(); 
    List<SelectOption> options = new List<SelectOption>();

    for ( Schema.SObjectType f : Schema.getGlobalDescribe().values()){
      if ( (f.getDescribe().getName() == 'Account') || (f.getDescribe().getName() == 'Contact') || (f.getDescribe().getName() == 'Lead') || (f.getDescribe().getName() == 'Opportunity')){
        options.add(new SelectOption(f.getDescribe().getName(),f.getDescribe().getName()));        
      }
    }

    Options.Sort();
    return options;
  }

  public void fetchFields()
  { 
    List<String> fields = new List<String>();
    Map<String , Schema.SObjectType> globalDescription = Schema.getGlobalDescribe();
    System.debug('Selected Object is ' + obj);
    Schema.sObjectType objType = globalDescription.get(obj); 
    System.debug('Object Type is ' + objType);
    Schema.DescribeSObjectResult r1 = objType.getDescribe(); 

    Map<String , Schema.SObjectField> mapFieldList = r1.fields.getMap(); 

    for(Schema.SObjectField field : mapFieldList.values()) 
  { 
    Schema.DescribeFieldResult fieldResult = field.getDescribe(); 

  if(fieldResult.isAccessible()) 
  { 
    System.debug('Field Name is ' + fieldResult.getName());
    fields.add(fieldResult.getName());
  } 
  }

  List<String> so = new List<String>();
  for(String f : fields)
  {
    so.add(f);
  } 
  objFields = so; 
  } 
}


Regards,
Mano
Sampath SuranjiSampath Suranji
Hi,
Try something like below,
@isTest
public class MetadataFieldsTest {
    
    public static testmethod void testAll(){
        try
        {
            MetadataFields objM = new MetadataFields();
            objM.obj='contact';
            
            objM.getobj();
            objM.setobj('contact');
            List<SelectOption> objList = objM.getobjs();
            objM.fetchFields();
        }
        catch(Exception ex){}
        
    }
    
}

Regards
Sampath​
This was selected as the best answer
Mano sfdcMano sfdc
Hi Sampath,

Thanks for your help. 100% Code Coverage. Can you please let me know your email id.
Sampath SuranjiSampath Suranji
Hi,
sampathjt@gmail.com