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
Arun KArun K 

Distinct valuesin SOQL

I have table where I am getting records from method called foo

 

Now I want to get only distinct values.

 

How to get that.

 

Below is my code where I am getting records

"

public list<footprint__c> getfoot()
{


foo=[select id,name ,Territory__c,User__c from footprint__c where AxtriaCallPlan__Account__c=:ApexPages.currentPage().getParameters().get('id') Order By User__c ];

return foo;
}

"

 

How to get that.

 

 

Best Answer chosen by Admin (Salesforce Developers) 
Sean TanSean Tan

You're second re-query won't really filter those records out (since multiple records may match the name).

 

Try something like this which just maintains a map based off the name value of your Footprint__c object. The logic will take the "last" record that matches the name (I've made it case insensitive... if you want it to be case sensitive remove the toLowerCase())

 

public list<footprint__c> getfoot()
{        
    Map<String, Footprint__c> footprintMap = new Map<String, Footprint__c>{};
    
    for (Footprint__c item : [select id,name ,User__c,Account__c,AxtriaCallPlan__Call_String_Name__c from footprint__c where AxtriaCallPlan__Account__c=:ApexPages.currentPage().getParameters().get('id') and AxtriaCallPlan__User__c!=:totalCalls Order By User__c ])
    {
        //Case insensitive?? If not remove the toLowerCase
        footprintMap.put(item.Name.toLowerCase(), item);
    }
    
    return footprintMap.values();    
}

 

All Answers

Arun KArun K

thanks for that.

 

That made me to go in right direction

But i couldnt get unique records

 

Am I missing anything

Here is my updated code

 

"

public list<footprint__c> getfoot()
{

List<footprint__c> lFINAL = New List<footprint__c>();
Set<String> sNames = New Set<String>();
lFINAL =[select id,name ,Territory__c,User__c from footprint__c where AxtriaCallPlan__Account__c=:ApexPages.currentPage().getParameters().get('id') and AxtriaCallPlan__User__c!=:totalCalls Order By User__c ];
// foo=[select id,name ,AxtriaCallPlan__Account__r.ownerid from footprint__c where AxtriaCallPlan__Account__c=:ApexPages.currentPage().getParameters().get('id') ];


for (Integer i = 0; i< lFINAL .size(); i++)
{
sNames .add(lFINAL [i].Name); // contains distict accounts
}
return lFINAL ;
}

"

Rajesh SriramuluRajesh Sriramulu

HI,

 

Try this

 

U need to return Snames not IFinal.

 

public list<footprint__c> getfoot()
{

List<footprint__c> lFINAL = New List<footprint__c>();
Set<String> sNames = New Set<String>();
lFINAL =[select id,name ,Territory__c,User__c from footprint__c where AxtriaCallPlan__Account__c=:ApexPages.currentPage(

).getParameters().get('id') and AxtriaCallPlan__User__c!=:totalCalls Order By User__c ];
// foo=[select id,name ,AxtriaCallPlan__Account__r.ownerid from footprint__c where AxtriaCallPlan__Account__c=:ApexPages.currentPage().getParameters().get('id') ];


for (Integer i = 0; i< lFINAL .size(); i++)
{
sNames .add(lFINAL [i].Name); // contains distict accounts
}
return sNames;
}

 

 

Regards,

Rajesh.

Arun KArun K

When I try to return sNames

 

It is giving the error and not saving

 

 Return value must be of type: LIST<Footprint__c> at line 28 column 9

Rajesh SriramuluRajesh Sriramulu

Hi,

 

sorry try this,

 

 

 

public list<footprint__c> getfoot()
{

List<footprint__c> lFINAL = New List<footprint__c>();
Set<String> sNames = New Set<String>();
lFINAL =[select id,name ,Territory__c,User__c from footprint__c where AxtriaCallPlan__Account__c=:ApexPages.currentPage(

).getParameters().get('id') and AxtriaCallPlan__User__c!=:totalCalls Order By User__c ];
// foo=[select id,name ,AxtriaCallPlan__Account__r.ownerid from footprint__c where AxtriaCallPlan__Account__c=:ApexPages.currentPage().getParameters().get('id') ];


for (Integer i = 0; i< lFINAL .size(); i++)
{
sNames .add(lFINAL [i].Name); // contains distict accounts
}

 


List<footprint__c> lFINAL1= New List<footprint__c>();

lFINAL1 =[select id,name ,Territory__c,User__c from footprint__c where AxtriaCallPlan__Account__c=:ApexPages.currentPage().getParameters().get('id') and AxtriaCallPlan__User__c!=:totalCalls and name In:Snames Order By User__c ];


return lFINAL1;
}

Arun KArun K

Thanks for the reply.

 

But still I couldnt get the unique values

 

In Vf page Am writing

as

<apex:pageblocktable value="{!foot}" var="foo" >

 

 

 

here is updated code in apex

 

 

public list<footprint__c> getfoot()
{

List<footprint__c> lFINAL = New List<footprint__c>();
Set<String> sNames = New Set<String>();
lFINAL =[select id,name ,User__c,Account__c,AxtriaCallPlan__Call_String_Name__c from footprint__c where AxtriaCallPlan__Account__c=:ApexPages.currentPage().getParameters().get('id') and AxtriaCallPlan__User__c!=:totalCalls Order By User__c ];
// foo=[select id,name ,AxtriaCallPlan__Account__r.ownerid from footprint__c where AxtriaCallPlan__Account__c=:ApexPages.currentPage().getParameters().get('id') ];


for (Integer i = 0; i< lFINAL .size(); i++)
{
sNames .add(lFINAL [i].Name); // contains distict accounts
}
List<footprint__c> lFINAL1= New List<footprint__c>();
lFINAL1 =[select id,name ,Territory__c,User__c,Account__c,AxtriaCallPlan__Call_String_Name__c from footprint__c where AxtriaCallPlan__Account__c=:ApexPages.currentPage().getParameters().get('id') and AxtriaCallPlan__User__c!=:totalCalls and name In:Snames Order By User__c ];

return lFINAL1;
}

Sean TanSean Tan

You're second re-query won't really filter those records out (since multiple records may match the name).

 

Try something like this which just maintains a map based off the name value of your Footprint__c object. The logic will take the "last" record that matches the name (I've made it case insensitive... if you want it to be case sensitive remove the toLowerCase())

 

public list<footprint__c> getfoot()
{        
    Map<String, Footprint__c> footprintMap = new Map<String, Footprint__c>{};
    
    for (Footprint__c item : [select id,name ,User__c,Account__c,AxtriaCallPlan__Call_String_Name__c from footprint__c where AxtriaCallPlan__Account__c=:ApexPages.currentPage().getParameters().get('id') and AxtriaCallPlan__User__c!=:totalCalls Order By User__c ])
    {
        //Case insensitive?? If not remove the toLowerCase
        footprintMap.put(item.Name.toLowerCase(), item);
    }
    
    return footprintMap.values();    
}

 

This was selected as the best answer
Arun KArun K

Thanks Sean Tan 

That solved my problem 

Now I could get the Unique fields.

 

 

Thanks for the replies S