1

I am trying to loop over a returned list of records from a query and set several variable once I find the correct criteria. The struggle I'm having is that the criteria I'm want to find can exist over multiple records in the resultset.

For example, to meet my condition, I'd need to find a record where the type is "start" and "end time" is not null and then I need to look for another record with type "next" and the "end time" is null. In that scenario I'd set my variable to the appropriate value. There are more than just "start" and "next" records in the resultset. So, I have multiple evaluations I need to do in the loop. There can also be more than one record with the same type. So, there could be one or more records of type "start" and all of them could have an "end time", but it could never be completed until there is a record of type "next" that does not have an "end time" yet. So, the logic I'm trying to build would look over all the records that have a "start" type and an "end time" and a record with a type of "next" that doesn't have an "end time".

In the following loop, I'm checking for the type and end time values.

for(var i=0; i < logLength; i++) {
    if(logs[i].Type__c == 'start' && logs[i].End_Time__c != null) {
        component.set("v.isStartComplete", true);
        continue;
    }
    if(logs[i].Type__c == 'next' && logs[i].End_Time__c != null) {
        component.set("v.isNextComplete", true);
        continue;
    }
}

The correct logic eludes me. Any help with a lifeline?

2
  • 1
    Your written description doesn't match the logic. The second if statement reads "where type is next and end date is not null", but your text description says "where type is next and end date is null". Which do you mean to say?
    – sfdcfox
    Mar 29, 2023 at 19:19
  • that was my original attempt at the loop. So, my written description of the logic is the goal. The example code was originally checking for a record of type "start" and "end time" and setting the start attribute to "complete". The second if was the same thing except looking for a record of type "next" and setting its attribute to "complete". Mar 29, 2023 at 19:26

1 Answer 1

0

I'd define some intermediate variables and then evaluate them outside of the loop. Something like this:

let started = false;
let next = false;
let complete = false;

//should be able to use a forEach here
logs.forEach(log => {
  if(log.Type__c == 'start' && log.End_Time__c != null) {
      started = true;
      return;
  }
  if(log.Type__c == 'next' && log.End_Time__c == null) {
      next = true;
      return;
  }
  if(log.Type__c == 'complete' && log.End_Time__c != null) {
      complete = true;
      return;
  }
});

//evaluate in reverse order as we can hopefully
//assume that if finished is set, the others will be 
//set also
if (complete === true){
  component.set("v.isFinished", true);
  component.set("v.isNextComplete", true);
  component.set("v.isStartComplete", true);
}
else if (next === true){
  component.set("v.isNextComplete", true);
  component.set("v.isStartComplete", true);
}
else if (start === true){
  component.set("v.isStartComplete", true);
}

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .