You need to sign in to do that
Don't have an account?
Arun 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.
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())
All Answers
Hi,
Please see the below link:
http://boards.developerforce.com/t5/Apex-Code-Development/select-distinct-in-apex-soql/td-p/177611
Regards,
Rajesh.
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 ;
}
"
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.
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
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;
}
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;
}
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())
Thanks Sean Tan
That solved my problem
Now I could get the Unique fields.
Thanks for the replies SRS8
Arun K