本文介绍了如何对日期字段值的列表项进行排序(Apex、Salesforce)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

使用 Salesforce 的 Apex,有没有办法对日期字段值的列表项进行排序.请参考以下代码的 TODO 部分,谢谢.

With Salesforce's Apex, is there any way to sort list items, for Date field value. Please refer the TODO section of the following code, thanks.

/**
 * Select list of jobOccurrences belongs to particular list of jobs
 */
private List<Job_Occurrence__c> getJobsByJobCode(List<Job__c> jobList) {

 // Select relevant Job Occurrence objects
    List<Job_Occurrence__c> jobOccuList = new List<Job_Occurrence__c>();

    for (Job__c job : jobList) {

         Job_Occurrence__c jobOccurrence = [SELECT Id, Job__c,
                                                   Schedule_Start_Date__c
                                            FROM Job_Occurrence__c
                                            WHERE Job__c =: job.Id];

         if((jobOccurrence != null) && (jobOccurrence.Id != null)) {
            jobOccuList.add(jobOccurrence);
         }
    }

    if((jobOccuList != null) && (jobOccuList.size() > 0)) {

       // TODO
       // How I sort the 'jobOccuList' with Date field 'Schedule_Start_Date__c',
       // for select the items according to sequence of latest jobOccurrence

       return jobOccuList;

    } else {
       throw new RecordNotFoundException ('Could not found any jobOccurrence for given list of jobs');
    }
}

推荐答案

你真的应该批量化这个代码,即不要在循环中运行查询,这可能会导致调控器限制的潜在问题,因为你只想要组合列出所有 Job__c 记录,这也让您的订购变得简单——您可以在查询中完成!

You really should bulkify this code, i.e. not run a query inside a loop which could cause potentially cause issues with the governor limits, and since you just want the combined list for all Job__c records this makes your ordering easy too — you can do it in the query!

您要更改的代码是这样的:

The code you want to change is this:

// Select relevant Job Occurrence objects
List<Job_Occurrence__c> jobOccuList = new List<Job_Occurrence__c>();

for (Job__c job : jobList) {

     Job_Occurrence__c jobOccurrence = [SELECT Id, Job__c,
                                               Schedule_Start_Date__c
                                        FROM Job_Occurrence__c
                                        WHERE Job__c =: job.Id];

     if((jobOccurrence != null) && (jobOccurrence.Id != null)) {
        jobOccuList.add(jobOccurrence);
     }
}

本质上,我们可以优化它,使其不仅使用一个查询而不是 N(其中 N 是 jobList.size()),并且同时让它们排序.首先我们需要收集 Job__c ID 的列表,然后我们可以在 SOQL 的 WHERE 子句中使用 IN 语句:

Essentially we can optimise this to not only use one query instead of N (where N is jobList.size()) and get them ordered at the same time. First we need to gather the list of Job__c IDs, and then we can use the IN statement in the WHERE clause of the SOQL:

// Select relevant Job Occurrence objects
List<Job_Occurrence__c> jobOccuList;
Set<Id> setJobIds = new Set<Id>();

setJobIds.addAll(jobList);

// get the job occurances starting with the latest, use ASC for reverse!
jobOccuList = [SELECT Id, Job__c, Schedule_Start_Date__c
               FROM   Job_Occurrence__c
               WHERE  Job__c IN : setJobIds
               ORDER BY Schedule_Start_Date__c DESC];

最后,如果您需要能够轻松地从 Job_Occurrence_c 记录映射回 Job_c 记录,您可以使用如下映射替换该集合,尽管您只需要这个list 我认为这里不需要它(只是为了完整性而提供它).

Finally, if you need to be able to easily map back from the Job_Occurrence_c records to Job_c records, you could replace the set with a map as below, though given that you just want this list I don't think it's needed here (just providing it for completeness).

Map<Id, Job__c> mapJobs = new Map<Id, Job__c>();

for (Job__c job : jobList) {
    mapJobs.put(job.Id, job);
}

** snip **

for (Job_Occurrence__c jobOccu : jobOccuList) {
    Job__c theJob = mapJobs.get(jobOccu.Job__c);
    // do stuff with the job
}

所有这些代码都是在浏览器中编写的,所以可能会有一些语法错误,但应该是好的!

All of this code has been written in browser, so there may be some syntax errors but it should be good!

这篇关于如何对日期字段值的列表项进行排序(Apex、Salesforce)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-18 20:57