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
sarvesh001sarvesh001 

Soql Query

Hi,

I am quering the custom objects records based on ownerid and logindate field. Now i want to query the record based on order by ownerid and order by loginate field.

My requirement is i want to get the records for every user  in ASC order of records for every user.


Can any one help me....


Thanks,
Sarvesh.
Best Answer chosen by sarvesh001
rohitsfdcrohitsfdc
Hello Sarvesh,
As you said, you want to sort the query using two fields, you can follow the query below. Let me know if that doesnt helps you

SELECT OwnerId, LoginDate__c FROM <customObject>  ORDER BY Owner.createddate DESC,logindate__c DESC


All Answers

Darshan FarswanDarshan Farswan
Hi Sarvesh,

It is fairly simple to implement ORDER BY in a SOQL. Conside the following example without SOQL and with SOQL :

Without ORDER BY:
SELECT OwnerId, LoginDate__c FROM <customObject> where OwnerId = 'xxxxx' AND LoginDate = 'xxxxx'


With ORDER BY:
SELECT OwnerId, LoginDate__c FROM <customObject> where OwnerId = 'xxxxx' AND LoginDate = 'xxxxx' ORDER BY OwnerId ASC

This is all my understanding of your question. If you have a different requirement, please elaborate.

sarvesh001sarvesh001
Hi Darshan Farswan,

I have used the above  query
SELECT OwnerId, LoginDate__c FROM <customObject> where OwnerId = 'xxxxx' AND LoginDate = 'xxxxx' ORDER BY OwnerId ASC
In this query query recodrs like  1st record of 1st user then  1st record of 2nd user then 2nd record of 1st user then 2nd record of 2nd user ..................................


But i want to get records like 1st record of 1st user then 2nd record of 1st user ...........then 1st record of 2nd user then 2nd record of 2nd user..................




Darshan FarswanDarshan Farswan
Hi Sarvesh

Can you please the same query. Because, you should not be getting the result as you suggested. The result should always have all the records of first user and then all the records of second user. The query is correct. Can you please check the data again.
rohitsfdcrohitsfdc
Hello Sarvesh,
As you said, you want to sort the query using two fields, you can follow the query below. Let me know if that doesnt helps you

SELECT OwnerId, LoginDate__c FROM <customObject>  ORDER BY Owner.createddate DESC,logindate__c DESC


This was selected as the best answer
sarvesh001sarvesh001
Hi Darshan Farswan,

isuue is sloved.
i have used the query  SELECT OwnerId, LoginDate__c FROM <customObject> where OwnerId = 'xxxxx' AND LoginDate = 'xxxxx' ORDER BY OwnerId ,LoginDate ASC