You need to sign in to do that
Don't have an account?
Steve Cox 9
Can you sort an SObject Collecton
Hi,
I hava a flow that - for any contact - constructs an SObject collection of related records.
However, this collection is not sorted. I'm looping through this collection to construct a text field from the record names, but I would like to have this in alphabetical order. The only facility appears to be the 'ascending' or 'descending' option for the loop, but that appears to be for the index, not values within the objects.
Any suggestions?
I hava a flow that - for any contact - constructs an SObject collection of related records.
However, this collection is not sorted. I'm looping through this collection to construct a text field from the record names, but I would like to have this in alphabetical order. The only facility appears to be the 'ascending' or 'descending' option for the loop, but that appears to be for the index, not values within the objects.
Any suggestions?
You can refer this link for sorting in sobjects:
https://developer.salesforce.com/docs/atlas.en-us.apexcode.meta/apexcode/apex_list_sorting_sobject.htm
However, I still have an issue and wonder if you can help?
In the flow, I have various sObject variables. One is called 'subscription' and is for processing a single custom Subscription object in a loop. There is also an sObject Collection variable called 'subsciptions' - which is the one I want to sort.
Here is the simple code for the Apex class:
When dropping the new apex action only the flow designer, the only possible choice for the target parameter is theseSubs - which is correct. However, wth this selected, the only options for the source is the 'subscription' sObject variable (and other single sObject variables). For some reason, my definition of theseSubs in the Apex class is coming across as a collection - only an individual sObject variable.
Can you see what I may have done wrong?
Thanks,
Steve
public class SessionWrapper implements Comparable
{
// This wrapper class was written to do a custom sorting operation on a list of Session__c sObjects
public Session__c s;
// Constructor
public SessionWrapper(Session__c innerS)
{
s = innerS;
}
// Compare sessions based on session_date__c
public Integer compareTo(Object oFromOutside)
{
// Cast argument to SessionWrapper
SessionWrapper swFromOutside = (SessionWrapper) oFromOutside;
System.debug('Comparing: [' + s.Session_Date__c + '] and [' + swFromOutside.s.Session_Date__c + ']');
// The return value of 0 indicates that both elements are equal.
Integer returnValue = 0;
if (s.Session_Date__c > swFromOutside.s.Session_Date__c)
{
// Set return value to a positive value.
returnValue = 1;
}
else if (s.Session_Date__c < swFromOutside.s.Session_Date__c)
{
// Set return value to a negative value.
returnValue = -1;
}
System.debug('Return value is [' + returnValue +']');
return returnValue;
}
// This method sortBySessionDate is used in a flow to sort a list of sessions by Session_Date__c
//
@InvocableMethod(label='Sort Sessions' description='Sorts the list of Session__c sObjects by Session_Date__c')
public static List<List<Session__c>> sortBySessionDate(List<List<Session__c>> theSessions)
{
List<List<Session__c>> lolos = new List<List<Session__c>>();
List<SessionWrapper> sw = new List<SessionWrapper>();
for(Session__c oneS: theSessions[0])
{
sw.add(new SessionWrapper(oneS));
}
sw.sort();
// List-of-List of sObjects to return back to flow
theSessions[0].clear();
for(SessionWrapper oneSW: sw)
{
theSessions[0].add(oneSW.s);
}
return theSessions;
}
}