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
Madevala JithenderMadevala Jithender 

Kindly helpme out from this code to Test class

public class PropertyUtility {
    public static void newListedProperties(){
        List<Property__c> newproplist = new list<Property__c>();
        newproplist = [select id,name,Broker__r.Email__c,Days_on_market__c from Property__c where Days_on_market__c<=30];
        
        for(Property__c P:newproplist){
            string propEmail = p.name + ':' + p.Broker__r.Email__c;
            system.debug(propEmail);
        }
    }

}
AnkaiahAnkaiah (Salesforce Developers) 
Hi Jithender,

try with below code.
@istest
public class Test_PropertyUtility {

private static testmethod void TestProperty(){

//insert mandatory fields for Broker__c
Broker__c br=new Broker__c();
br.Name='testName';
br.Email__C = 'test@abc.com';

insert br;

//insert mandatory fields for Property__c
Property__c  prty=new Property__c();
prty.Broker__c=br.Id;
prty.Name='Testprty';
prty.Days_on_market__c = 30;

insert prty;

PropertyUtility.newListedProperties();

}

If this helps, Please mark it as best answer.

Thanks!!​​​​​​​
 
mukesh guptamukesh gupta
Hi Jithender,

As per salesforce best practice we always use @testSetUp method for Insert All reacord on one place, So it's a good approach for each test class
 
@isTest	
	public class PropertyUtilityTest{
	
	@testSetup static void createRecord(){
		Broker__c brokr=new Broker__c();
		brokr.Name='testBroker';
		brokr.Email__C = 'testbroker@gmail.com';
		insert brokr;
		
		Property__c  prop=new Property__c();
		prop.Broker__c=brokr.Id;
		prop.Name='Testprty';
		prop.Days_on_market__c = 30;
		insert prop;
	}
	
	 testMethod static void getAccount(){
        List<Property__c> newproplist = [select id,name,Broker__r.Email__c,Days_on_market__c from Property__c where Days_on_market__c<=30];
	    PropertyUtility.newListedProperties();
	 }

if you need any assistanse, Please let me know!!

Kindly mark my solution as the best answer if it helps you.

Thanks
Mukesh