+ Start a Discussion
shaqib ahmadshaqib ahmad 

Create a Relationship Query

Create a query and assign the query results to the list
Get this information:
The name of the property
The broker’s email address
How long the property has been on market
(Hint: Use API names, not field names or field labels)
Object: Property

I WANT TO KNOW HOW TO WRITE BELOW QUERY FOR PROPERTY LISTED IN LAST 30 DAYS....

Condition: The property was listed within the last 30 days

public class PropertyUtility {
    public static void newListedProperties(){
        //list<Property__c> newPropList= new List<Property__c>();
        List<Property__c> newPropList=[select name,  broker__r.email__c from Property__c where CreatedDate= LAST_N_DAYS:30];
        //newPropList.add(pro);
        
        for(Property__c p:newPropList){
             String propEmail=  +p.name+ ':' +p.broker__r.email__c;
            system.debug(propEmail);
            
        }
        
    }

}
ANUTEJANUTEJ (Salesforce Developers) 
Hi Shaqib,

So are you getting any error while querying the records, you can use the query editor on the developer console to see if the query is returning records.

I hope this helps and in case if this comes handy can you please choose this as best answer so that it can be used by others in the future.

Regards,
Anutej
shaqib ahmadshaqib ahmad
Challenge not yet complete in My Trailhead Playground 1
We can’t find the correct SOQL query in the PropertyUtility class.
Sravana Sandhya SuruSravana Sandhya Suru
Hi Shaqib,
Please try this:

public class PropertyUtility {
    public static void newListedProperties(){
        List<Property__c>  newPropList = [Select Name,Days_On_Market__c,Broker__r.Email__c from Property__c where Days_On_Market__c < 31];
        for(Property__c p : newPropList){
            string  propEmail = p.Name +':'+ p.Broker__r.Email__c ;
            system.debug(propEmail);
        }
    }
}
 
smriti sharan19smriti sharan19
Please try this

public class PropertyUtility {
    
    public static void newListedProperties(){
        List<Property__c> newPropList = [Select Name,id,Days_On_Market__c,Broker__r.Email__c from Property__c where Days_On_Market__c <31];
        for(Property__c proplist :newPropList){
            String propEmail = proplist.name+':'+proplist.Broker__r.Email__c;  
            system.debug(propEmail);
        }
    }
    
}
Pankaj MoulekhiPankaj Moulekhi
Indexing in list is important . 
String propEmail = newPropList[0].Name + ':' + newPropList[0].Broker__r.Email__c ;

Here is the complete code. It should work fine.


public class PropertyUtility {
    public static void newListedProperties(){
        List<Property__c> newPropList = [SELECT Name , Broker__r.Email__C , Days_On_Market__c FROM Property__c WHERE Days_On_Market__c<31] ;
                                         for(Property__c Result : newPropList){
                                             String propEmail = newPropList[0].Name + ':' + newPropList[0].Broker__r.Email__c ;
                                             System.Debug(propEmail);
                                             
                                             
                                         }
        
        
        
        
        
    }

}