+ Start a Discussion
Luke Higgins 22Luke Higgins 22 

Query a list of records with equal fields

Hi, 

I am trying to query a list of the latest 10 records from the same object and look to see if they are all equal. I'm struggling to find how to get a list of 10 placements with the same client name. Here is the code that I have come up with so far - 

@AuraEnabled
    public static ts2__Placement__c getPlcClient(Id recordId) {
        List<ts2__Placement__c> plc = new List<ts2__Placement__c> ();
        plc = [
               SELECT Id, ts2__Client__r.Name 
               FROM ts2__Placement__c
               WHERE Id =:recordId
           	];
      
        List<ts2__Placement__c> plcClient = new List<ts2__Placement__c> ();
        plcClient = [
                SELECT Id FROM ts2__Placement__c
                WHERE ts2__Client__r.Name = :plc
                ORDER BY createdDate DESC LIMIT 10
        ];

        return plcClient;
    }
Raj VakatiRaj Vakati
Change it as below
 
@AuraEnabled
    public static ts2__Placement__c getPlcClient(Id recordId) {
        List<ts2__Placement__c> plc = new List<ts2__Placement__c> ();
        plc = [
               SELECT Id, ts2__Client__r.Name 
               FROM ts2__Placement__c
               WHERE Id =:recordId
           	];
      
        List<ts2__Placement__c> plcClient = new List<ts2__Placement__c> ();
        plcClient = [
                SELECT Id FROM ts2__Placement__c
                WHERE ts2__Client__r.Name = :plc.ts2__Client__r.Name
                ORDER BY createdDate DESC LIMIT 10
        ];

        return plcClient;
    }