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
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);
                                             
                                             
                                         }
        
        
        
        
        
    }

}
Alberto AcostaAlberto Acosta
public class PropertyUtility {
    public static void newListedProperties(){
        //SELECT Id, Days_On_Market__c, Name FROM Property__c
        //SELECT Id, Email__c FROM Broker__c
        //Broker__c is the parent of Property__c, dot natation is used and r for the relationship 
        //SELECT Name, Broker__r.Email__c,Days_On_Market__c FROM Property__c;
        List <Property__c> newPropList = 
        [SELECT Name, Broker__r.Email__c,Days_On_Market__c
        FROM Property__c WHERE Days_On_Market__c <= 30];
       
        for (Property__c prop : newPropList){
        String propEmail = 'Property Name: ' + prop.Name +'Broker Email: '+ prop.Broker__r.Email__c;
        system.debug(propEmail);
        }   
    }
 
Mehmet TozanMehmet Tozan
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 proplist : newPropList){
        String propEmail = 'Property Name: ' + proplist.Name + ', Brokers email: ' + proplist.Broker__r.Email__c;
system.debug(propEmail);  
    }
    }
}
Abhishek Singh 175Abhishek Singh 175
public class PropertyUtility {
    Public Static Void newListedPropertie()
    {
    List<Property__c> newPropList=[SELECT Name, Broker__r.Email__c,Days_On_Market__c FROM Property__c where Date_Listed__c= LAST_N_DAYS:30];    
        for (Property__c Prop: newPropList)
        {
           String propEmail= 'Property Name'+Prop.Name+':' +'Broker Email'+Prop.Broker__r.Email__c;
            System.debug(propEmail);
        }
    }

}
Zamin Ali blteeZamin Ali bltee
The following code will surely work. Here "id" field after Name field has been included (that is not exclusively mentioned in the given challenge).
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);
        }
    }
}
JoniBondoJoniBondo
Try this. - worked for me

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 prop : newPropList){
            String  propEmail = 'Property Name:' + prop.Name + 'Property Name:'  'Broker Email' + prop.Broker__r.Email__C;
                   system.debug(propEmail);
}
}
    }
Manoj Kumar 1040Manoj Kumar 1040
Worked for me.

public class PropertyUtility {
    public static void newListedProperties(){
        List<Property__c> newPropList = new List<Property__c>();
        newPropList = [SELECT Name, 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);            
        }
    }
}

Let me know, if you need help.