Stumped using if() and date math


(Corey Panno) #1

Hi all,

I can’t figure out where I am going wrong. I have a simple need: I want to set a Flags color for all notes with $EndDate set. Notes must for True for the following:

  • based on Prototype “p-Project”
  • are not True for user attribute $IsDone
  • do not have $EndDate set

The Edict for the p-Project prototype note is:

if ($Prototype=="p-Project" & !$IsDone & $EndDate!="never")
	{
		if ($EndDate-date(today)>=3) {Flags="blue"} else
		{
			if ($EndDate-date(today)>=1) {Flags="green"} else
			{
				if ($EndDate-date(today)==0) {Flags="black"} else
				{
					if($EndDate-date(today)<0) {Flags="red"}
				}
			}
		}
	} else {Flags=""};

Using an Agent to test results, I get the following:

!

What am I not seeing or doing?

Thank you.

-Corey

[edit: by mod to clarify code]


(Corey Panno) #2

Staring at it some more, I do see that my if ($EndDate-date(today)>=1) {Flags=“green”}
will knock out my blue flags - I have to fix that, but still do not see why the rest do not flag according to the results


(Mark Anderson) #3

OK, I tidied up the code a bit (though I don’t think that’s the real issue):

var dateDiff;
dateDiff=$EndDate-date("today");
$MyNumber=dateDiff;
if ($Prototype=="p-Project" & !$IsDone & $EndDate!="never"){
   if (dateDiff>=3) {
      $Flags="blue";
   } else {
      if (dateDiff<3 & dateDiff>0) {
         $Flags="green";
      } else {
         if (dateDiff==0) {
            $Flags="black";
         } else {
            if(dateDiff<0) {
               $Flags="red";
            };
         };
      };
   };
} else {
   $Flags="";
};

I get:

tab-exp1

It also shows that your code looks to be graduating positive difference values but your test data is for negative values. Anyway, as shown above the code seems to work. The whitespace/indenting is only for clarity. Do note I’ve added the $-prefix for attribute references, e.g. $Flags. Your old-style usage of Flags (just the attribute name) is now deprecated and is supported only on a legacy basis.

Anyway, I hope that helps.


(Corey Panno) #4

Thank you Mark - very much appreciated . For the fix and especially for all that I now understand from your coding for the solution. This really helpful.


(eastgate) #5

Suggestion: do this action first as either a rule or an agent:

Query: $Prototype==“p-Project” & !$IsDone & $EndDate!=“never”

Action: (your action, though no need for the outer test)

One reason to move the outer test to an agent is that you’ll know whether the agent is finding the notes you expect. If all notes fail to match the outer if() in your action, then $Flags will never be changed


Suggestion: since the edict is inherited from p-Project, you probably don’t need to test the prototype.


(eastgate) #6

Also, whenever you find yourself with deeply nested or complicated if() statements, it’s good to ask whether the task might be simpler if you used some additional agents, or stored some intermediate results.

This is a stylistic or aesthetic preference, but it’s hard to understand nested if() statements at a glance, and easy to cause headaches by forgetting a closing brace.


(Corey Panno) #7

Thanks, Mark. Now that you point it out, I recall seeing this approach in my early getting started reading. I’ll circle back to read how to do it.