+ Start a Discussion
ajitvermaajitverma 

Query to fetch Unique records


how can i create a soql to fetch unique ownerid from Account Object. i don't want to handle this in the code.

ShamSham
It is not possible in SOQL
You will have to carry out this in code
ajitvermaajitverma
Thanks Sam,

but i need to do it in S-Control, how could i do it.

ShamSham
you can do something like this.

Code:
<script>

Array.prototype.contains = function (element)
  {
          for (var i = 0; i < this.length; i++)
       {
              if (this[i] == element)
          {
                      return true;
              }
          }
          return false;
  };


var uniqueArr = new Array();

var result = sforce.connection.query("SELECT OwnerId from Account");
var record = result.getArray("record");

for(var i = 0; i < record.length;i++) {
    if(!uniqueArr.contains(record[i].OwnerId)) {
        uniqueArr.add(record[i].OwnerId);
    }
}

</script>

 


//Now unique Arr contains all unique ownerIDs.
ajitvermaajitverma
hi sham,

Thank u very much dear, now i am able to solve my problem.
but in java script array we can't add a element using add();
so i need to define another prototype for adding the element in array.
Code:
Array.prototype.add = function (element){
this[this.length] = element;
};

 
Again thanks :smileyhappy:
asish1989asish1989
Hi
You can write aggreerate querry for this. Below is the link 

lIST<AggregateResult> listofAccountsAggregateResult = 
                            [SELECT Ownerid
                              FROM  Account
                              GROUP BY Ownerid];

http://salesforceworld4u.blogspot.in/2013/12/fetching-unique-record-in-salesforce.html