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
Ad_InfosysAd_Infosys 

Sorting using sets.

I am using following code to sort records in the task object on the basis of activity date. Now I take the result in a set task set. this is pretty sorted. Now I use this set to take out the opporunities related to the tasks. I take result in another list. When I see the result list, the records are not sorted as per the sorting done earlier.. The sorting vanishes.
How to solve this issue.
 
set<id>taskset=new set<id>();
list<id>oppset=new list<id>();
 
 for(task ta_id:[select whatid from task where  DTC_SalesPartner_AdEntId_Txt__c='122' and status!='Completed' order by activitydate asc nulls  last]){
system.debug(ta_id.whatid);             
taskset.add(ta_id.whatid);}
 for(opportunity opp:[select id from opportunity where id in: taskset ]){
system.debug(opp.id);
 oppset.add(opp.id);
system.debug('add');
}
IspitaIspita

Hi,
If you just want the Ids what the need of the other query. Secondly if you want the field of opportunity object you can do that using single query like the following:-
1. Select   t.What.Id, t.WhatId, t.Id, t.Description, t.ActivityDate From Task t order by t.ActivityDate asc
but you will not be able to access other fields of opportunity object.


Thanks

Ad_InfosysAd_Infosys

Thanks for reply.

 

I understand your point but ya I to access fields from opportunity so thats why I am using such code. Now issue is

In my second query I am not able to retain the order which I attain in the taskset....

How can I do that in second query. 'In' statement is bringing the curse. How shd I solve.

SuperfellSuperfell
Sets have no defined order, if you want an ordered collection, use an array. (aka list)