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
medemazamedemaza 

How to not SOQL Distinct

 

public class LastOfferPrice {
	public ID OpportunityID{get; set;}
	
	
	List<OpportunityLineItem> Item;
	public List<OpportunityLineItem> getItem(){
        if(Item==null){
        Item = [Select Id,PricebookEntry.Product2.Name,ListPrice,UnitPrice,Opportunity.CloseDate,LastModifiedDate From OpportunityLineItem Where OpportunityId=:OpportunityID ORDER BY PricebookEntry.Product2.Name DESC]; 
       }
        return Item;
    }

    List<OpportunityLineItem> LastOffer;
    
    public List<OpportunityLineItem> getLastOffer(){
    	
        for(OpportunityLineItem I:Item){
        	
	      LastOffer = [Select Id,distinct (PricebookEntry.Product2.Name,ListPrice),UnitPrice,LastModifiedDate 
	      From OpportunityLineItem Where LastModifiedDate <= YESTERDAY and PricebookEntry.Product2.Name=:I.PricebookEntry.Product2.Name ORDER BY ID DESC ]; 

	       }
	      return LastOffer;
    }

}

 Is error How to query not duplicate name of product

 

Best Answer chosen by Admin (Salesforce Developers) 
Chamil MadusankaChamil Madusanka

Hi,

 

As Bob and Sharma said, there is no Distinct in SOQL. You have to do that in your code manually. I have a code example for eleminate duplicates. I hope that will help you to do your job.

 

 

    public static List<String> getDistinctLastnames(List<String> duplicates){

    List<String> distinctLastnames = new List<String>();

    for(String lastname: duplicates){

    Boolean found = false;

    for(Integer i=0; i< distinctLastnames.size(); i++){

    if(lastname.equalsIgnoreCase(distinctLastnames[i])){ //Check if current lastname has been added yet

    found=true;

    break;

    }

    }

    if(!found)

    distinctLastnames.add(lastname);

    }

    return distinctLastnames;

    }

 If a reply to a post answers your question or resolves your problem, please mark it as the solution to the post so that others may benefit.

 

Chamil's Blog

 

All Answers

bob_buzzardbob_buzzard

Unfortunately there is no equivalent of distinct in SOQL.  You'll have to pull process the results of the query and carry out your own duplicate detection I'm afraid.

Shashikant SharmaShashikant Sharma

No distinct available in SOQL, Do this

1)Get All the records

2)Use for loop to eliminate duplicate records from the list.

Chamil MadusankaChamil Madusanka

Hi,

 

As Bob and Sharma said, there is no Distinct in SOQL. You have to do that in your code manually. I have a code example for eleminate duplicates. I hope that will help you to do your job.

 

 

    public static List<String> getDistinctLastnames(List<String> duplicates){

    List<String> distinctLastnames = new List<String>();

    for(String lastname: duplicates){

    Boolean found = false;

    for(Integer i=0; i< distinctLastnames.size(); i++){

    if(lastname.equalsIgnoreCase(distinctLastnames[i])){ //Check if current lastname has been added yet

    found=true;

    break;

    }

    }

    if(!found)

    distinctLastnames.add(lastname);

    }

    return distinctLastnames;

    }

 If a reply to a post answers your question or resolves your problem, please mark it as the solution to the post so that others may benefit.

 

Chamil's Blog

 

This was selected as the best answer
Sarath PMSarath PM

Using a Set instead of a List will make sure that duplicates are removed by default as Set do not allow duplicate entries. So, for processing, we can put list of strings into a Set, which will remove duplicates.