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
mshroyermshroyer 

Can you compare two results from a SOQL query?

Disclaimer, new to this stuff. I have a task trigger that updates custom object records related to the Account based on task type. In order to get the values for the custom object I query for the custom records where the Account on the record = the account on the Task that was just completed, then I put the results of the query in a list/map:

 

for

(CRM_Cadence_Checkpoint__c CadC : [

Select Id, CRM_Activity_Type__c, CRM_Last_Cadence_Activity_Date__c, CRM_Active__c, CRM_Account__c, CRM_Next_Cadence_Activity_Date__c

 FromCRM_Cadence_Checkpoint__c

 where CRM_Account__c = :t.WhatId

 AND CRM_Active__c = True]) {

 

update_cadence_if_nec(t, CadC);

     

  }

 

I then iterate through that list later on and compare the task type to the cadence activity type which decides if it actually gets updated or not.

 

I now have a new requirement where I need to compare two different custom records before I put them in the list. The custom object is called Cadence Checkpoints. The Cadence Checkpoint records have a field called Activity Type which could equal "1" or "2". They also have a Last Activity Date which is what the trigger currently updates. This is what I want to do:

 

Query the fields

If the Date for record with Activity Type = "1" is before or equal to the Date for record with Activity type = "2" then add the record with "1" to the list.

If the Date for record with Activity Type = "1" is after the Date for record with Activity type = "2" then add the record with "2" to the list.

 

The problem is that the query iterates through the records one at a time. So I can't figure out how to compare the two without going outside the for loop for the query.

Peter_sfdcPeter_sfdc
You say you have to compare two different custom records before picking between them and putting them in a list. What is the common factor that those records share?

In other words, presumably you could have lots of CRM_Cadence_Checkpoint__c records related to the same account, right? So when you're looking for the "two" records, what is the thing that makes you decide that it is these two Checkpoint records? Or is it the account?
mshroyermshroyer

Cadence Checkpoints have a master detail relationship with Accounts and are autocreated via trigger based on values for certain fields on the account. For specific accounts, only two cadence checkpoints are created. These two records  will have the same account and the Cadence Acitvity Type on one will = "1' and the other will = "2". I believe the problem I'm having is that I'm not passing these into a map or list, they are being passed into a method that I call later on. However in that method we only iterate through one record at a time, so I can't compare two records. This trigger has been working great for about a year because we only needed to iterate through the records one at a time. But recently the requirement has changed and now I need compare the two records whose Activity Type is 1 or 2.

 

This is what I'm trying to do:

If a task is created, look for Cadence Checkpoints whose Account matches the Account on the Task. If the Activity Type for the Cadence is 1 or 2, (in this case, if there is a cadence whose Activity Type is 1 then there will always be a second with actvity type of 2), find which one has the oldest due date and only update that specific Cadence.

 

But I'm trying to do this without completely re-writing my trigger. I think I'm going to have to write an if statement after my query and if the Activity Type is 1 or 2, add to a list or map, then later on compare the date values for the records in that list or map and only add the record with the oldest date to the current method that is being called at the end of my trigger.