Incremental number attributes


(Simon Smailus) #1

If I wanted to keep track of how many projects and tasks I complete a year and created $ProjectNumber and $TaskNumber attributes. Is there a way to get each new project to have its number added automatically and incremented +1 from the last $ProjecNumber? Then also apply this to the $TaskNumber. This would actually be useful in tracking projects on timesheets. Each task can have the parent $ProjectNumber added automatically too.

I’m at a loss in how to achieve this? Also the numbers need to be read only so they don’t accidentally get deleted. In terms of number I was thinking of the format P1 and T1 and then then number increments so it probably needs to be a string rather than a number.


(Mark Anderson) #2

I don’t think it’s an exact solution, but be aware of the ‘sequential’ option for Number-type attributes. The problem there is that whilst numbers are not re-used, the allocation goes to every note and in your case thus probably to notes that shouldn’t have a value for that attribute.

A hack comes to mind. Make a note whose role is to be the counter, with $MyNumber (or Number-type attribute of your choice holding the last issued number in the sequence. You then write you code such that the action that allocates a number reads the existing $MyNumber for the note and then also increments the 'counter 'by one. There will doubtless be some edge cases that come out of the woodwork but it might be worth a try. the counter note just need a suitable name you won’t mistake and can be stored off in a corner of the doc as once tested and working you’d never really need to see it - just access it via code from elsewhere in the doc.


(eastgate) #3

In my own notes about Tinderbox, I have a similar situation – a container of user-reported issues. Each has its own number, because it’s sometimes easier to say “Issue 741” than to say “that peculiar thing X reported where Tinderbox gets kind of slow on Tuesdays when…”

Under the hood, we use the sandbox attribute $MyNumber to hold the issue number. The OnAdd action of the container is simply

Prototype=“Issue”; $MyNumber=$MyNumber(previousSibling)+1

and the container is sorted by $MyNumber.

and the $DisplayExpression, inherited from the prototype, is

$MyNumber+". "+$Name

Now this isn’t perfect; if you added new notes to the middle of the list, the numbering would be fouled up. But It’s quick and easy; I set this up without much thought back in 2013 and it’s been fine ever since.

There are lots of other ways to do this. Sequential attributes are guaranteed to be unique for every note, for example. $SiblingOrder is sometimes all you need. Or, you could use a configuration note to keep track of the issues.

OnAdd: if($IssueNumber==0) { $IssueNumber=$IssueNumber(/config); $IssueNumber(/config)=$IssueNumber(/config)+1


(Simon Smailus) #4

Thank you, I’ll give this a try.


(Simon Smailus) #5

Couldn’t get the above to work.

Did manage to get this one to work. Thanks.


(eastgate) #6

“Couldn’t get this to work” doesn’t give people trying to help you much to work with.

  • Do you understand what the OnAdd action is doing?

  • Have you defined the user attribute $IssueNumber?

  • Have you created a top-level note named config to hold the current issue number?


(Simon Smailus) #7

in my TBX are 3 notes. All on the same level.

config
Project
Note

Config and Note have an attribute called $IssueNumber (default is 0)
Project has the following code in Action:

if($IssueNumber==0) { $IssueNumber=$IssueNumber(/config); $IssueNumber(/config)=$IssueNumber(/config)+1;

The Note has an attribute $IssueNumber

If I place the Note inside Project, the $IssueNumber in config does not increment and the Note $IssueNumber remains at 0.

$IssueNumber is a number attribute.


(Mark Anderson) #8

Your action is missing a closing }. But even then it fails - at first. I think the $OnAdd code you want is this:

if($IssueNumber==0) {
  if() {
    $IssueNumber(/config)=1;$IssueNumber=$IssueNumber(/config);$IssueNumber(/config)=$IssueNumber(/config)+1;
  } else {
    $IssueNumber=$IssueNumber(/config); $IssueNumber(/config)=$IssueNumber(/config)+1;
}

This makes the projects number from 1.

[edit: typo & missing $]


(Simon Smailus) #9

Many thanks, but I cannot get this to work. I suspect that there’s a “$” missing on the third “IssueNumber”, but even with this correction, no joy.

Perhaps if you have a demo file that works, that would be most helpful and appreciative.

Many thanks.


(Mark Anderson) #10

OK, I’ve made a demo (link at end). But here’s the rough process…

Make the new Number-type attribute IssueNumber. Remember to set the type (default is string) and set the default to 0. (That should be done for you and is a glitch which will get fixed in due course).

Now add the notes. Set the $IssueNumber for ‘config’ to 1 as this makes the later $OnAdd code less complex as the counter object is preset to start from one1 rather than zero. I’ve also made things more flexible by moving ‘config’ out of the root and addressing it via title not path. Just ensure there is only note of that name and you can then put it anywhere you like without the Project’s on-add code failing. Add this code to $OnAdd for Project:

if($IssueNumber==0) {
$IssueNumber=$IssueNumber("config"); $IssueNumber("config")=$IssueNumber("config")+1;
}

You have something like this:

Note the preset initial counter in ‘config’, changed from the default value (zero). Now drag Note 1 into Project:

Note 1 gets numbered and the counter is incremented ready for the next note. In the demo there’s one supplied (‘Note 2’) ready for you to try.

The demo TBX, built/tested in v6.6.5 is here.